2017-11-21 22:12:04 -05:00
|
|
|
// Renders diagrams and flowcharts from text using Mermaid in any element with the
|
|
|
|
// `js-render-mermaid` class.
|
|
|
|
//
|
|
|
|
// Example markup:
|
|
|
|
//
|
|
|
|
// <pre class="js-render-mermaid">
|
|
|
|
// graph TD;
|
|
|
|
// A-- > B;
|
|
|
|
// A-- > C;
|
|
|
|
// B-- > D;
|
|
|
|
// C-- > D;
|
|
|
|
// </pre>
|
|
|
|
//
|
|
|
|
|
|
|
|
import Flash from './flash';
|
|
|
|
|
2017-11-22 13:40:11 -05:00
|
|
|
export default function renderMermaid($els) {
|
|
|
|
if (!$els.length) return;
|
2017-11-21 22:12:04 -05:00
|
|
|
|
|
|
|
import(/* webpackChunkName: 'mermaid' */ 'blackst0ne-mermaid').then((mermaid) => {
|
|
|
|
mermaid.initialize({
|
2018-01-17 17:56:25 -05:00
|
|
|
// mermaid core options
|
|
|
|
mermaid: {
|
|
|
|
startOnLoad: false,
|
|
|
|
},
|
|
|
|
// mermaidAPI options
|
2017-11-21 22:12:04 -05:00
|
|
|
theme: 'neutral',
|
|
|
|
});
|
|
|
|
|
2017-11-22 13:40:11 -05:00
|
|
|
$els.each((i, el) => {
|
2017-12-20 08:48:09 -05:00
|
|
|
const source = el.textContent;
|
|
|
|
|
2018-01-31 16:48:18 -05:00
|
|
|
// Remove any extra spans added by the backend syntax highlighting.
|
|
|
|
Object.assign(el, { textContent: source });
|
|
|
|
|
2017-12-20 08:48:09 -05:00
|
|
|
mermaid.init(undefined, el, (id) => {
|
|
|
|
const svg = document.getElementById(id);
|
|
|
|
|
|
|
|
svg.classList.add('mermaid');
|
|
|
|
|
|
|
|
// pre > code > svg
|
|
|
|
svg.closest('pre').replaceWith(svg);
|
|
|
|
|
|
|
|
// We need to add the original source into the DOM to allow Copy-as-GFM
|
|
|
|
// to access it.
|
|
|
|
const sourceEl = document.createElement('text');
|
|
|
|
sourceEl.classList.add('source');
|
|
|
|
sourceEl.setAttribute('display', 'none');
|
|
|
|
sourceEl.textContent = source;
|
|
|
|
|
|
|
|
svg.appendChild(sourceEl);
|
|
|
|
});
|
2017-11-22 13:40:11 -05:00
|
|
|
});
|
2017-11-21 22:12:04 -05:00
|
|
|
}).catch((err) => {
|
|
|
|
Flash(`Can't load mermaid module: ${err}`);
|
|
|
|
});
|
2017-11-22 13:40:11 -05:00
|
|
|
}
|