mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-10-12 18:49:37 +02:00
feat: Use marked instead of mdast-util-from-markdown
This will remove 33 dependencies pulled down by mdast-util-from-markdown. marked has 0 dependencies. mermaid's dependency count will go from 102 to 69
This commit is contained in:
@@ -81,7 +81,7 @@
|
||||
"katex": "^0.16.9",
|
||||
"khroma": "^2.1.0",
|
||||
"lodash-es": "^4.17.21",
|
||||
"mdast-util-from-markdown": "^2.0.0",
|
||||
"marked": "^13.0.2",
|
||||
"stylis": "^4.3.1",
|
||||
"ts-dedent": "^2.2.0",
|
||||
"uuid": "^9.0.1"
|
||||
|
@@ -1,4 +1,3 @@
|
||||
/* eslint-disable no-irregular-whitespace */
|
||||
import { markdownToLines, markdownToHTML } from './handle-markdown-text.js';
|
||||
import { test, expect } from 'vitest';
|
||||
|
||||
@@ -215,13 +214,13 @@ test('markdownToLines - No auto wrapping', () => {
|
||||
[
|
||||
[
|
||||
{
|
||||
"content": "Hello, how do",
|
||||
"content": "Hello, how do",
|
||||
"type": "normal",
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
"content": "you do?",
|
||||
"content": "you do?",
|
||||
"type": "normal",
|
||||
},
|
||||
],
|
||||
@@ -296,5 +295,5 @@ test('markdownToHTML - no auto wrapping', () => {
|
||||
you do?`,
|
||||
{ markdownAutoWrap: false }
|
||||
)
|
||||
).toMatchInlineSnapshot('"<p>Hello, how do<br/>you do?</p>"');
|
||||
).toMatchInlineSnapshot(`"<p>Hello, how do<br/> you do?</p>"`);
|
||||
});
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import type { Content } from 'mdast';
|
||||
import { fromMarkdown } from 'mdast-util-from-markdown';
|
||||
import type { MarkedToken, Token } from 'marked';
|
||||
import { marked } from 'marked';
|
||||
import { dedent } from 'ts-dedent';
|
||||
import type { MarkdownLine, MarkdownWordType } from './types.js';
|
||||
import type { MermaidConfig } from '../config.type.js';
|
||||
@@ -24,13 +24,13 @@ function preprocessMarkdown(markdown: string, { markdownAutoWrap }: MermaidConfi
|
||||
*/
|
||||
export function markdownToLines(markdown: string, config: MermaidConfig = {}): MarkdownLine[] {
|
||||
const preprocessedMarkdown = preprocessMarkdown(markdown, config);
|
||||
const { children } = fromMarkdown(preprocessedMarkdown);
|
||||
const nodes = marked.lexer(preprocessedMarkdown);
|
||||
const lines: MarkdownLine[] = [[]];
|
||||
let currentLine = 0;
|
||||
|
||||
function processNode(node: Content, parentType: MarkdownWordType = 'normal') {
|
||||
function processNode(node: MarkedToken, parentType: MarkdownWordType = 'normal') {
|
||||
if (node.type === 'text') {
|
||||
const textLines = node.value.split('\n');
|
||||
const textLines = node.text.split('\n');
|
||||
textLines.forEach((textLine, index) => {
|
||||
if (index !== 0) {
|
||||
currentLine++;
|
||||
@@ -42,17 +42,17 @@ export function markdownToLines(markdown: string, config: MermaidConfig = {}): M
|
||||
}
|
||||
});
|
||||
});
|
||||
} else if (node.type === 'strong' || node.type === 'emphasis') {
|
||||
node.children.forEach((contentNode) => {
|
||||
processNode(contentNode, node.type);
|
||||
} else if (node.type === 'strong' || node.type === 'em') {
|
||||
node.tokens.forEach((contentNode) => {
|
||||
processNode(contentNode as MarkedToken, node.type === 'em' ? 'emphasis' : node.type);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
children.forEach((treeNode) => {
|
||||
nodes.forEach((treeNode) => {
|
||||
if (treeNode.type === 'paragraph') {
|
||||
treeNode.children.forEach((contentNode) => {
|
||||
processNode(contentNode);
|
||||
treeNode.tokens?.forEach((contentNode) => {
|
||||
processNode(contentNode as MarkedToken);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -61,23 +61,23 @@ export function markdownToLines(markdown: string, config: MermaidConfig = {}): M
|
||||
}
|
||||
|
||||
export function markdownToHTML(markdown: string, { markdownAutoWrap }: MermaidConfig = {}) {
|
||||
const { children } = fromMarkdown(markdown);
|
||||
const nodes = marked.lexer(markdown);
|
||||
|
||||
function output(node: Content): string {
|
||||
function output(node: Token): string {
|
||||
if (node.type === 'text') {
|
||||
if (markdownAutoWrap === false) {
|
||||
return node.value.replace(/\n/g, '<br/>').replace(/ /g, ' ');
|
||||
return node.text.replace(/\n/g, '<br/>').replace(/ /g, ' ');
|
||||
}
|
||||
return node.value.replace(/\n/g, '<br/>');
|
||||
return node.text.replace(/\n/g, '<br/>');
|
||||
} else if (node.type === 'strong') {
|
||||
return `<strong>${node.children.map(output).join('')}</strong>`;
|
||||
} else if (node.type === 'emphasis') {
|
||||
return `<em>${node.children.map(output).join('')}</em>`;
|
||||
return `<strong>${node.tokens?.map(output).join('')}</strong>`;
|
||||
} else if (node.type === 'em') {
|
||||
return `<em>${node.tokens?.map(output).join('')}</em>`;
|
||||
} else if (node.type === 'paragraph') {
|
||||
return `<p>${node.children.map(output).join('')}</p>`;
|
||||
return `<p>${node.tokens?.map(output).join('')}</p>`;
|
||||
}
|
||||
return `Unsupported markdown: ${node.type}`;
|
||||
}
|
||||
|
||||
return children.map(output).join('');
|
||||
return nodes.map(output).join('');
|
||||
}
|
||||
|
13
pnpm-lock.yaml
generated
13
pnpm-lock.yaml
generated
@@ -239,9 +239,9 @@ importers:
|
||||
lodash-es:
|
||||
specifier: ^4.17.21
|
||||
version: 4.17.21
|
||||
mdast-util-from-markdown:
|
||||
specifier: ^2.0.0
|
||||
version: 2.0.1
|
||||
marked:
|
||||
specifier: ^13.0.2
|
||||
version: 13.0.2
|
||||
stylis:
|
||||
specifier: ^4.3.1
|
||||
version: 4.3.2
|
||||
@@ -6784,6 +6784,11 @@ packages:
|
||||
markdown-table@3.0.3:
|
||||
resolution: {integrity: sha512-Z1NL3Tb1M9wH4XESsCDEksWoKTdlUafKc4pt0GRwjUyXaCFZ+dc3g2erqB6zm3szA2IUSi7VnPI+o/9jnxh9hw==}
|
||||
|
||||
marked@13.0.2:
|
||||
resolution: {integrity: sha512-J6CPjP8pS5sgrRqxVRvkCIkZ6MFdRIjDkwUwgJ9nL2fbmM6qGQeB2C16hi8Cc9BOzj6xXzy0jyi0iPIfnMHYzA==}
|
||||
engines: {node: '>= 18'}
|
||||
hasBin: true
|
||||
|
||||
marked@4.3.0:
|
||||
resolution: {integrity: sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==}
|
||||
engines: {node: '>= 12'}
|
||||
@@ -16893,6 +16898,8 @@ snapshots:
|
||||
|
||||
markdown-table@3.0.3: {}
|
||||
|
||||
marked@13.0.2: {}
|
||||
|
||||
marked@4.3.0: {}
|
||||
|
||||
mdast-builder@1.1.1:
|
||||
|
Reference in New Issue
Block a user