diff --git a/packages/mermaid/src/rendering-util/handle-markdown-text.js b/packages/mermaid/src/rendering-util/handle-markdown-text.js deleted file mode 100644 index 5102429d3..000000000 --- a/packages/mermaid/src/rendering-util/handle-markdown-text.js +++ /dev/null @@ -1,85 +0,0 @@ -import { fromMarkdown } from 'mdast-util-from-markdown'; -import { dedent } from 'ts-dedent'; - -/** - * @param {string} markdown markdown to process - * @returns {string} processed markdown - */ -function preprocessMarkdown(markdown) { - // Replace multiple newlines with a single newline - const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n'); - // Remove extra spaces at the beginning of each line - const withoutExtraSpaces = dedent(withoutMultipleNewlines); - return withoutExtraSpaces; -} - -/** - * @param {string} markdown markdown to split into lines - */ -export function markdownToLines(markdown) { - const preprocessedMarkdown = preprocessMarkdown(markdown); - const { children } = fromMarkdown(preprocessedMarkdown); - const lines = [[]]; - let currentLine = 0; - - /** - * @param {import('mdast').Content} node - * @param {string} [parentType] - */ - function processNode(node, parentType = 'normal') { - if (node.type === 'text') { - const textLines = node.value.split('\n'); - textLines.forEach((textLine, index) => { - if (index !== 0) { - currentLine++; - lines.push([]); - } - textLine.split(' ').forEach((word) => { - if (word) { - lines[currentLine].push({ content: word, type: parentType }); - } - }); - }); - } else if (node.type === 'strong' || node.type === 'emphasis') { - node.children.forEach((contentNode) => { - processNode(contentNode, node.type); - }); - } - } - - children.forEach((treeNode) => { - if (treeNode.type === 'paragraph') { - treeNode.children.forEach((contentNode) => { - processNode(contentNode); - }); - } - }); - - return lines; -} - -/** - * @param {string} markdown markdown to convert to HTML - * @returns {string} HTML - */ -export function markdownToHTML(markdown) { - const { children } = fromMarkdown(markdown); - - /** - * @param {import('mdast').Content} node - */ - function output(node) { - if (node.type === 'text') { - return node.value.replace(/\n/g, '
'); - } else if (node.type === 'strong') { - return `${node.children.map(output).join('')}`; - } else if (node.type === 'emphasis') { - return `${node.children.map(output).join('')}`; - } else if (node.type === 'paragraph') { - return `

${node.children.map(output).join('')}

`; - } - return `Unsupported markdown: ${node.type}`; - } - - return children.map(output).join(''); -} diff --git a/packages/mermaid/src/rendering-util/handle-markdown-text.spec.ts b/packages/mermaid/src/rendering-util/handle-markdown-text.spec.ts index 8ae519cfa..fba8acf69 100644 --- a/packages/mermaid/src/rendering-util/handle-markdown-text.spec.ts +++ b/packages/mermaid/src/rendering-util/handle-markdown-text.spec.ts @@ -237,10 +237,13 @@ test('markdownToHTML - Only italic formatting', () => { }); test('markdownToHTML - Mixed formatting', () => { - const input = `*Italic* and **bold** formatting`; - const expectedOutput = `

Italic and bold formatting

`; - const output = markdownToHTML(input); - expect(output).toEqual(expectedOutput); + expect(markdownToHTML(`*Italic* and **bold** formatting`)).toMatchInlineSnapshot( + '"

Italic and bold formatting

"' + ); + + expect(markdownToHTML('special characters: `

hi

`')).toMatchInlineSnapshot( + '"

special characters: <p>hi</p>

"' + ); }); test('markdownToHTML - Unsupported formatting', () => { diff --git a/packages/mermaid/src/rendering-util/handle-markdown-text.ts b/packages/mermaid/src/rendering-util/handle-markdown-text.ts index 4fcbaa0b8..91ed94284 100644 --- a/packages/mermaid/src/rendering-util/handle-markdown-text.ts +++ b/packages/mermaid/src/rendering-util/handle-markdown-text.ts @@ -1,12 +1,13 @@ import { micromark } from 'micromark'; import { fromMarkdown } from 'mdast-util-from-markdown'; import type { Content } from 'mdast'; +import { dedent } from 'ts-dedent'; function preprocessMarkdown(markdown: string): string { // Replace multiple newlines with a single newline const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n'); // Remove extra spaces at the beginning of each line - const withoutExtraSpaces = withoutMultipleNewlines.replace(/^\s+/gm, ''); + const withoutExtraSpaces = dedent(withoutMultipleNewlines); return withoutExtraSpaces; }