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

Linebreak is not rendered correct #60

Open
basler-timhermann opened this issue Oct 22, 2024 · 1 comment
Open

Linebreak is not rendered correct #60

basler-timhermann opened this issue Oct 22, 2024 · 1 comment

Comments

@basler-timhermann
Copy link

Hi,

Linebraks are not rendered in the contentful-rich-text-vue-renderer. We tried something like this:

text: ({ marks, value }, key, markRenderer) => {
    **const textWithLineBreaks = value
      .split('\n')
      .map((str, index) => {
        return index > 0 ? ['<br>', str] : str;
      })
      .flat();**
    if (!marks.length) {
      // avoid hydration mismatches
      // XXX: why are "\r" included in response? not normalized in
      //  contentful? "\r" is not rendered on client side.
      return textWithLineBreaks?.toString()?.replaceAll('\r', '');
    }

    const marksReversed = [...marks].reverse();

    return marksReversed.reduce(
      // XXX: proper typing
      (aggregate: any, mark, i) =>
        markRenderer[mark.type]([aggregate], `${key}-${i}`, h),
      textWithLineBreaks
    );
  }

But then the break is rendered as string. In the 'rich-text-html-renderer' there is an option:

const options = {
  preserveWhitespace: true,
};

Can you please provide an option to render Linebreaks correct?

@tolgap
Copy link
Member

tolgap commented Oct 22, 2024

Hi,

We have examples in our test suite that convert line breaks into <br> tags here: https://github.com/paramander/contentful-rich-text-vue-renderer/blob/master/src/index.test.js#L690

Here is the full commit that adds that test: 733893d

Does this help?

By the way, I see you're struggling with \r\n line breaks. The test is splitting on \n only. So you would have to change that part to fit to your own solution.

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

No branches or pull requests

2 participants