1
0
Fork 0
mirror of synced 2024-11-13 20:58:57 -05:00
ultimate-vim/sources_non_forked/markdown-preview.nvim/app/pages/mermaid.js

36 lines
961 B
JavaScript
Raw Normal View History

2022-05-19 08:12:11 -04:00
import {escape} from './utils';
/*
* global mermaid
*/
const mermaidChart = (code) => {
try {
// eslint-disable-next-line
mermaid.parse(code)
return `<div class="mermaid">${escape(code)}</div>`
} catch ({ str, hash }) {
return `<pre>${str}</pre>`
}
}
const MermaidPlugin = (md) => {
const origin = md.renderer.rules.fence.bind(md.renderer.rules)
md.renderer.rules.fence = (tokens, idx, options, env, slf) => {
const token = tokens[idx]
const code = token.content.trim()
if (typeof token.info === 'string' && token.info.trim() === 'mermaid') {
return mermaidChart(code)
}
const firstLine = code.split(/\n/)[0].trim()
if (firstLine === 'gantt' ||
firstLine === 'sequenceDiagram' ||
firstLine === 'erDiagram' ||
firstLine.match(/^graph (?:TB|BT|RL|LR|TD);?$/)) {
return mermaidChart(code)
}
return origin(tokens, idx, options, env, slf)
}
}
export default MermaidPlugin