vue-strapi-blocks-renderer icon indicating copy to clipboard operation
vue-strapi-blocks-renderer copied to clipboard

\n to br

Open MarijnFK opened this issue 1 year ago • 7 comments
trafficstars

In the strapi-blocks it is possible to input a shift-enter, which results in a \n I see there is a
option for empty paragraphs, maybe it's possible to replace line-breaks inside the 'text' property to <br> aswell? This way we can place <br> inside a <p> tag

MarijnFK avatar Jul 02 '24 10:07 MarijnFK

I am not sure I understand what you want. Could you give me two code examples? One for before (the current output) and one for the output you want, both in HTML. Also what will the benefit of this be?

To make things simple I am mostly trying the match the official React plugin from Strapi.

niklasfjeldberg avatar Jul 02 '24 15:07 niklasfjeldberg

In the output below, there is a \n in the text-property, inside a paragraph. The \n is not replaced by a
. This results in different display from the strapi-editor and the html generated from the blocks-renderer.

[
  {
    "type": "paragraph",
    "children": [
      {
        "type": "text",
        "text": "It would be nice to have this \n render to a <br>"
      }
    ]
  }
]

MarijnFK avatar Jul 03 '24 06:07 MarijnFK

hi @MarijnFK so i found https://github.com/strapi/blocks-react-renderer/pull/42, we'll do it when it hits release.

reslear avatar Jul 21 '24 15:07 reslear

@reslear awesome, thanks for the reply

MarijnFK avatar Jul 23 '24 06:07 MarijnFK

Here's a tmp workaround:

const replaceNewlines = (line: string) => {
    const text = line.split(/\r?\n|\r/g);

    return (
        <>
            {
                text.map((part, index) => (
                    <React.Fragment key={index}>
                        {index > 0 && <br/>} {part}
                    </React.Fragment>
                ))
            }
        </>
    );
};
<BlocksRenderer content={children}
                blocks={{
                    paragraph: ({children}) => {
                        return (
                            React.Children.map(children, (child) => {
                                if (React.isValidElement(child)) {
                                    const {props = {text}, ...rest} = child as Child;

                                    /** @fix https://github.com/strapi/blocks-react-renderer/pull/42/files */
                                    if (typeof text === 'string') {
                                        return {
                                            ...rest,
                                            props: {
                                                ...child.props,
                                                text: replaceNewlines(text)
                                            }
                                        };
                                    }
                                }

                                return child;
                            })
                        )
                    }
/>

monolithed avatar Nov 25 '24 16:11 monolithed

@monolithed thanks for the workaround. Unfortunatly it is a React-example, this is a vue-package. Maybe you can update it to match the vue-syntax?

MarijnFK avatar Nov 26 '24 14:11 MarijnFK

@MarijnFK my bad, I don't use Strapi with Vue, but the replaceNewlines function will be the same in both cases.

monolithed avatar Nov 27 '24 06:11 monolithed