Skip to content

Commit

Permalink
Add Mermaid support for export
Browse files Browse the repository at this point in the history
  • Loading branch information
DinoFive committed Dec 6, 2024
1 parent 1fd4023 commit c1a7d04
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion src/service/markdown/markdown-pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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 = `
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>mermaid.initialize({startOnLoad:true});</script>
`;

$('body').append(mermaidScript);
}

await exportByType(inputMarkdownFile, $.html(), type, config)
}


Expand Down Expand Up @@ -100,6 +113,7 @@ function convertMarkdownToHtml(filename, type, text, config) {
.use(markdownItToc)
.use(markdownItKatex)
.use(markdownItPlantuml)
.use(markdownItMermaid)

return md.render(text)

Expand Down

0 comments on commit c1a7d04

Please sign in to comment.