Skip to content

Commit

Permalink
fix: 🐛 handle multiple space rendering when using preserveWhitespace (#…
Browse files Browse the repository at this point in the history
…531)

When using preserveWhitespace,   is inserted as a string instead of JSX character entity. It is hence not rendered as a whitespace. To fix this, we replace whitespace with nbsp's unicode equivalent

Closes: #514
  • Loading branch information
JinHao-L authored Mar 4, 2024
1 parent 4703988 commit 982b21b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ Array [
exports[`preserveWhitespace preserves spaces between words 1`] = `
Array [
<p>
hello&nbsp;&nbsp;&nbsp;&nbsp;world
hello    world
</p>,
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function nodeToReactComponent(node: CommonNode, options: Options): ReactN

if (preserveWhitespace) {
// Preserve multiple spaces.
nodeValue = (nodeValue as string).replace(/ {2,}/g, (match) => '&nbsp;'.repeat(match.length));
nodeValue = (nodeValue as string).replace(/ {2,}/g, (match) => '\u00A0'.repeat(match.length));

// Preserve line breaks.
let lines = (nodeValue as string).split('\n');
Expand Down

0 comments on commit 982b21b

Please sign in to comment.