2020-02-12 12:17:15 +02:00
|
|
|
function addContextAssets(context:any) {
|
2020-02-12 00:27:34 +02:00
|
|
|
if ('mermaid' in context.pluginAssets) return;
|
|
|
|
|
|
|
|
context.pluginAssets['mermaid'] = [
|
|
|
|
{ name: 'mermaid.min.js' },
|
|
|
|
{ name: 'mermaid_render.js' },
|
|
|
|
{
|
|
|
|
inline: true,
|
2020-03-04 03:55:48 +02:00
|
|
|
// Note: Mermaid is buggy when rendering below a certain width (500px?)
|
|
|
|
// so set an arbitrarily high width here for the container. Once the
|
|
|
|
// diagram is rendered it will be reset to 100% in mermaid_render.js
|
|
|
|
text: '.mermaid { background-color: white; width: 640px; }',
|
2020-02-12 00:27:34 +02:00
|
|
|
mime: 'text/css',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-02-12 12:17:15 +02:00
|
|
|
// @ts-ignore: Keep the function signature as-is despite unusued arguments
|
2020-02-12 00:27:34 +02:00
|
|
|
function installRule(markdownIt:any, mdOptions:any, ruleOptions:any, context:any) {
|
|
|
|
const defaultRender:Function = markdownIt.renderer.rules.fence || function(tokens:any[], idx:number, options:any, env:any, self:any) {
|
|
|
|
return self.renderToken(tokens, idx, options, env, self);
|
|
|
|
};
|
|
|
|
|
|
|
|
markdownIt.renderer.rules.fence = function(tokens:any[], idx:number, options:{}, env:any, self:any) {
|
|
|
|
const token = tokens[idx];
|
|
|
|
if (token.info !== 'mermaid') return defaultRender(tokens, idx, options, env, self);
|
|
|
|
addContextAssets(context);
|
2020-03-10 01:24:57 +02:00
|
|
|
const contentHtml = markdownIt.utils.escapeHtml(token.content);
|
|
|
|
return `
|
|
|
|
<div class="joplin-editable">
|
|
|
|
<pre class="joplin-source" data-joplin-source-open="\`\`\`mermaid " data-joplin-source-close=" \`\`\` ">${contentHtml}</pre>
|
|
|
|
<div class="mermaid">${contentHtml}</div>
|
|
|
|
</div>
|
|
|
|
`;
|
2020-02-12 00:27:34 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function(context:any, ruleOptions:any) {
|
|
|
|
return function(md:any, mdOptions:any) {
|
|
|
|
installRule(md, mdOptions, ruleOptions, context);
|
|
|
|
};
|
|
|
|
}
|