Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle line breaks #42

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

r-o-b-o-t-o
Copy link

What does it do?

Transforms \n, \r\n and \r into <br /> tags in <Text />

Why is it needed?

Fixes #37

How to test it?

  • Create rich text blocks with line returns in them (⇧+⏎ in the rich text editor)
  • Render them with <BlocksRenderer />
  • Make sure the newline characters are replaced by <br /> tags

@r-o-b-o-t-o r-o-b-o-t-o changed the title Fix/lf to br fix: handle line breaks Jul 5, 2024
@leelawtondesign
Copy link

This worked perfectly for me, thanks!

@antoine-roux
Copy link

It would be nice to merge :)

@pbozyk
Copy link

pbozyk commented Oct 25, 2024

we need this in production pack! that would be nice.

@r-o-b-o-t-o
Copy link
Author

r-o-b-o-t-o commented Oct 28, 2024

In the meantime, my fork is available on npm: @roboto_/strapi-blocks-react

Copy link
Contributor

@remidej remidej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, could you just add a unit test too?

And sorry I wasn't aware of this PR, it's been a while so if you don't want to get back to it we can make another PR internally

src/Text.tsx Outdated Show resolved Hide resolved
@monolithed
Copy link

@remidej

Looks good, could you just add a unit test too?

And sorry I wasn't aware of this PR, it's been a while so if you don't want to get back to it we can make another PR internally

Please roll out your own PR if it's ready. It’s kinda hard to work without line breaks 😞

@monolithed
Copy link

monolithed commented Nov 27, 2024

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;
                            })
                        )
                    }
/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feat]: Support for Interpreting \n as Line Breaks
6 participants