Skip to content

[bug]: The code function isn't called. #54

@monolithed

Description

@monolithed

What version of @strapi/blocks-react-renderer are you using?

npm, node, react, strapi-* - the latest versions

What's Wrong?

The code below is likely working, but Strapi returns the data in a different format. The API returns the Code block inside a Paragraph, and the function code is never called.

it('parses code blocks to plain text', () => {

 <BlocksRenderer
          content={[
            {
              type: 'code',
              children: [
                {
                  type: 'text',
                  text: 'const a = 1;',
                },
                {
                  type: 'link',
                  url: 'https://test.com',
                  children: [{ type: 'text', text: 'const b = 2;', bold: true }],
                },
              ],
            },
          ]}
</>

Below, I have attached a screenshot of the server's response:

Screenshot 2024-11-20 at 13 33 05

Here is my workaround:

            <BlocksRenderer content={children}
                            blocks={{
                                paragraph: ({children}) => {
                                    return (
                                        <p>
                                            {
                                                React.Children.map(children, (item) => {
                                                    const {props: {code, text}} = item;

                                                    if (code) {
                                                        return <span className={styles.highlight}>{text}</span>;
                                                    }
                                                    return item;
                                                })
                                            }
                                        </p>
                                    )
                                },

To Reproduce

Open DevTools...

Expected Behaviour

The code function should be called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue: bugIssue reporting a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions