Skip to content

Instantly share code, notes, and snippets.

@danishcake
Last active January 28, 2024 14:32
Show Gist options
  • Save danishcake/d045c867594d6be175cb394995c90e2c to your computer and use it in GitHub Desktop.
Save danishcake/d045c867594d6be175cb394995c90e2c to your computer and use it in GitHub Desktop.
md-to-pdf Mermaid configuration

Using Mermaid-js from md-to-pdf

https://github.com/simonhaenisch/md-to-pdf https://mermaid.js.org/

A markdown file with a Mermaid diagram

gantt
    section Section
    Completed :done,    des1, 2014-01-06,2014-01-08
    Active        :active,  des2, 2014-01-07, 3d
    Parallel 1   :         des3, after des1, 1d
    Parallel 2   :         des4, after des1, 1d
    Parallel 3   :         des5, after des3, 1d
    Parallel 4   :         des6, after des4, 1d
Loading

Usage

Run

md-to-pdf --config-file ./config.js ./README.md
// A marked renderer for mermaid diagrams
const renderer = {
code(code, infostring) {
if (infostring === 'mermaid'){
return `<pre class="mermaid">${code}</pre>`
}
return false
},
};
module.exports = {
marked_extensions: [{ renderer }],
script: [
{ url: 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js' },
// Alternative to above: if you have no Internet access, you can also embed a local copy
// { content: require('fs').readFileSync('./node_modules/mermaid/dist/mermaid.js', 'utf-8') }
// For some reason, mermaid initialize doesn't render diagrams as it should. It's like it's missing
// the document.ready callback. Instead we can explicitly render the diagrams
{ content: 'mermaid.initialize({ startOnLoad: false}); (async () => { await mermaid.run(); })();' }
]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment