From c1a7d04a33ddd22cc26957fd9ca54ad8463e72a0 Mon Sep 17 00:00:00 2001 From: DinoFive Date: Fri, 6 Dec 2024 15:34:54 +0800 Subject: [PATCH] Add Mermaid support for export --- package.json | 1 + src/service/markdown/markdown-pdf.js | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index aa65e28..58417cb 100755 --- a/package.json +++ b/package.json @@ -642,6 +642,7 @@ "markdown-it": "^14.1.0", "markdown-it-anchor": "^9.2.0", "markdown-it-checkbox": "^1.1.0", + "markdown-it-mermaid": "^0.2.5", "markdown-it-plantuml": "^1.4.1", "markdown-it-toc-done-right": "^4.2.0", "mustache": "^4.0.1", diff --git a/src/service/markdown/markdown-pdf.js b/src/service/markdown/markdown-pdf.js index c577bc1..db182a2 100644 --- a/src/service/markdown/markdown-pdf.js +++ b/src/service/markdown/markdown-pdf.js @@ -9,6 +9,7 @@ const markdownItPlantuml = require("markdown-it-plantuml") const markdownItToc = require("markdown-it-toc-done-right") const markdownItAnchor = require("markdown-it-anchor") const { exportByType } = require('./html-export') +const markdownItMermaid = require('markdown-it-mermaid').default; async function convertMarkdown(inputMarkdownFile, config) { @@ -17,8 +18,20 @@ async function convertMarkdown(inputMarkdownFile, config) { const text = fs.readFileSync(inputMarkdownFile).toString() const content = convertMarkdownToHtml(inputMarkdownFile, type, text, config) const html = mergeHtml(content, uri) - await exportByType(inputMarkdownFile, html, type, config) + // insert mermaid script + const $ = require("cheerio").load(html); + const containsMermaid = $('.mermaid').length > 0; + if (containsMermaid) { + const mermaidScript = ` + + + `; + + $('body').append(mermaidScript); + } + + await exportByType(inputMarkdownFile, $.html(), type, config) } @@ -100,6 +113,7 @@ function convertMarkdownToHtml(filename, type, text, config) { .use(markdownItToc) .use(markdownItKatex) .use(markdownItPlantuml) + .use(markdownItMermaid) return md.render(text)