7194 : fix for markdown processing to KaTeX text to preserve newlines

on-behalf-of: @Mermaid-Chart <hello@mermaidchart.com>
This commit is contained in:
omkarht
2025-11-28 17:14:22 +05:30
parent 614129ca31
commit 31eb2fbf3e
2 changed files with 69 additions and 1 deletions

View File

@@ -33,4 +33,70 @@ describe('Katex', () => {
// { fontFamily: 'courier' } // { fontFamily: 'courier' }
// ); // );
// }); // });
describe('Katex with newlines (issue #7194)', () => {
it('5: should render newlines in plain text nodes with math', () => {
imgSnapshotTest(
`graph TD
plainmath["line 0
line 1
$$x=42$$
line 2"]`,
{ fontFamily: 'courier' }
);
});
it('6: should render newlines in markdown nodes with math', () => {
imgSnapshotTest(
`graph TD
markdownmath["\`line 0
line 1
$$x=42$$
line 2\`"]`,
{ fontFamily: 'courier' }
);
});
it('7: should render multiple math equations with newlines between them', () => {
imgSnapshotTest(
`graph TD
multimath["$$a=1$$
text between
$$b=2$$"]`,
{ fontFamily: 'courier' }
);
});
it('8: should render newlines before and after math in markdown', () => {
imgSnapshotTest(
`graph TD
beforeafter["\`line before
$$x=42$$
line after\`"]`,
{ fontFamily: 'courier' }
);
});
it('9: should render complex example with multiple nodes containing math and newlines', () => {
imgSnapshotTest(
`graph TD
plain["line 0
line 1"]
markdown["\`line 0
line 1\`"]
plainmath["line 0
line 1
$$x=42$$
line 2"]
markdownmath["\`line 0
line 1
$$x=42$$
\`"]
plain --> markdown
markdown --> plainmath
plainmath --> markdownmath`,
{ fontFamily: 'courier' }
);
});
});
}); });

View File

@@ -248,7 +248,9 @@ export const createText = async (
const decodedReplacedText = await replaceIconSubstring(decodeEntities(htmlText), config); const decodedReplacedText = await replaceIconSubstring(decodeEntities(htmlText), config);
//for Katex the text could contain escaped characters, \\relax that should be transformed to \relax //for Katex the text could contain escaped characters, \\relax that should be transformed to \relax
const inputForKatex = text.replace(/\\\\/g, '\\'); const inputForKatex = hasKatex(text)
? markdownToHTML(text.replace(/\\\\/g, '\\'), config)
: text.replace(/\\\\/g, '\\');
const node = { const node = {
isNode, isNode,