Skip to content

feat(mdxish): render newlines for consecutive blocks#1328

Closed
eaglethrost wants to merge 4 commits intonextfrom
dimas/CX-2911-mdxsih-mdx-modify-line-break-behaviour
Closed

feat(mdxish): render newlines for consecutive blocks#1328
eaglethrost wants to merge 4 commits intonextfrom
dimas/CX-2911-mdxsih-mdx-modify-line-break-behaviour

Conversation

@eaglethrost
Copy link
Copy Markdown
Contributor

PR App Fix RM-XYZ

🧰 Changes

  • In mdxish, currently consecutive lines don't get rendered as separate lines and will get combined to 1 line
Line 1
Line 2

- Item 1
- Item 2
This should not be merged to Item 2
Screenshot 2026-02-10 at 4 27 15 pm
  • This is due to remark's default behavior. This happens in MDX as well, but not in legacy, as legacy uses a different library
  • This PR adds a transformer that looks for inline \n characters that get bunched by remark in a text node, and parses and splits the node with a <br> tag so that newlines get rendered

🧬 QA & Testing

  • In an mdxish project, create content with consecutive lines. They should get rendered in separate lines
Line 1 
Line 2

- Item 1
- Item 2
This should not be merged to Item 2
  • Make sure consecutive lines in code blocks, components, don't get modified

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Noticed some uncaught lint errors from next, fixed them here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice!

.use(rehypeRaw, { passThrough: ['html-block'] })
.use(restoreBooleanProperties)
.use(mdxishMermaidTransformer) // Add mermaid-render className to pre wrappers
.use(splitNewlines)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Decided to do it in the HTML ecosystem, and not in the mdxishAstProcessor stage because:

  • It would slightly change the resulting AST and since our editor relies on it, might cause unintended effects. I also think since this is a rendering issue, it doesn't really make sense to put it in the ast processor stage
  • Easier to process in the ecosystem


// Typically this transformer should only be applied to text inside p and li tags
// where the problem occurs. For other tags, we bias to leave the behavior as is.
const INCLUDED_PARENTS = new Set(['p', 'li']);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To avoid transforming under areas that shouldn't be touched (e.g. code blocks, inline code), and to avoid regressions, I decided to limit under which tags does this apply to. From my understanding this new line rendering issue almost all of the times happen under paragraphs, and sometimes list. But I might have missed other elements (let me know if you can think of something else)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Lint

// transform it
if (typeof node.value !== 'string' || !node.value.trim() || !node.value.includes('\n')) return;

// Splint text by \n into different nodes, with the \n being converted to <br> element
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Splint text by \n into different nodes, with the \n being converted to <br> element
// Split text by \n into different nodes, with the \n being converted to <br> element

typo?

Copy link
Copy Markdown
Contributor

@kevinports kevinports left a comment

Choose a reason for hiding this comment

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

Tried to think of any test cases we might want to add. But the logic looks good to me!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few edge case tests we might want to cover:

  • Double new lines Line 1\n\nLine 2 should render
     <p>Line 1</p>
     <p>Line 2</p>
    
  • Trailing new line Line 1\n should render
     <p>Line 1</p>
    
  • Leading new line \nLine 1 should render
     <p>Line 1</p>
    

* This transformer would allow consecutive lines in markdown to be rendered as separate lines
* without the need for the empty line.
*/
const splitNewlines = () => (tree: Root) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@eaglethrost do you mind writing up a quick comparison/reasoning for this approach versus the way we used to do this in Legacy per my comment here?

@maximilianfalco
Copy link
Copy Markdown
Contributor

going to close this PR in favour of doing them together over in #1329

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.

4 participants