fix: Handle spaces after newline

This commit is contained in:
Sidharth Vinod 2024-07-13 15:11:53 +05:30
parent 644199d0d0
commit d71fe28a35
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
2 changed files with 13 additions and 3 deletions

View File

@ -295,5 +295,15 @@ test('markdownToHTML - no auto wrapping', () => {
you do?`,
{ markdownAutoWrap: false }
)
).toMatchInlineSnapshot(`"<p>Hello,&nbsp;how&nbsp;do<br/>&nbsp;&nbsp;you&nbsp;do?</p>"`);
).toMatchInlineSnapshot(`"<p>Hello,&nbsp;how&nbsp;do<br/>you&nbsp;do?</p>"`);
});
test('markdownToHTML - auto wrapping', () => {
expect(
markdownToHTML(
`Hello, how do
you do?`,
{ markdownAutoWrap: true }
)
).toMatchInlineSnapshot(`"<p>Hello, how do<br/>you do?</p>"`);
});

View File

@ -66,9 +66,9 @@ export function markdownToHTML(markdown: string, { markdownAutoWrap }: MermaidCo
function output(node: Token): string {
if (node.type === 'text') {
if (markdownAutoWrap === false) {
return node.text.replace(/\n/g, '<br/>').replace(/ /g, '&nbsp;');
return node.text.replace(/\n */g, '<br/>').replace(/ /g, '&nbsp;');
}
return node.text.replace(/\n/g, '<br/>');
return node.text.replace(/\n */g, '<br/>');
} else if (node.type === 'strong') {
return `<strong>${node.tokens?.map(output).join('')}</strong>`;
} else if (node.type === 'em') {