2020-02-17 04:08:52 -05:00
|
|
|
import $ from 'jquery';
|
2020-03-16 08:09:12 -04:00
|
|
|
import { once } from 'lodash';
|
2020-08-20 05:09:55 -04:00
|
|
|
import { deprecatedCreateFlash as flash } from '~/flash';
|
2020-08-17 17:09:56 -04:00
|
|
|
import { __, sprintf } from '~/locale';
|
2018-03-20 00:01:17 -04:00
|
|
|
|
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>
|
|
|
|
//
|
|
|
|
|
2019-05-20 10:11:44 -04:00
|
|
|
// This is an arbitrary number; Can be iterated upon when suitable.
|
2020-12-07 16:10:08 -05:00
|
|
|
const MAX_CHAR_LIMIT = 2000;
|
|
|
|
// Max # of mermaid blocks that can be rendered in a page.
|
|
|
|
const MAX_MERMAID_BLOCK_LIMIT = 50;
|
|
|
|
// Keep a map of mermaid blocks we've already rendered.
|
|
|
|
const elsProcessingMap = new WeakMap();
|
|
|
|
let renderedMermaidBlocks = 0;
|
|
|
|
|
2020-03-16 08:09:12 -04:00
|
|
|
let mermaidModule = {};
|
2019-02-22 03:12:25 -05:00
|
|
|
|
2020-03-16 08:09:12 -04:00
|
|
|
function importMermaidModule() {
|
|
|
|
return import(/* webpackChunkName: 'mermaid' */ 'mermaid')
|
2020-12-23 16:10:24 -05:00
|
|
|
.then((mermaid) => {
|
2020-04-30 17:09:47 -04:00
|
|
|
let theme = 'neutral';
|
2020-11-09 07:09:24 -05:00
|
|
|
const ideDarkThemes = ['dark', 'solarized-dark', 'monokai'];
|
2020-04-30 17:09:47 -04:00
|
|
|
|
|
|
|
if (
|
2020-06-09 05:08:20 -04:00
|
|
|
ideDarkThemes.includes(window.gon?.user_color_scheme) &&
|
2020-04-30 17:09:47 -04:00
|
|
|
// if on the Web IDE page
|
|
|
|
document.querySelector('.ide')
|
|
|
|
) {
|
|
|
|
theme = 'dark';
|
|
|
|
}
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
mermaid.initialize({
|
|
|
|
// mermaid core options
|
|
|
|
mermaid: {
|
|
|
|
startOnLoad: false,
|
|
|
|
},
|
|
|
|
// mermaidAPI options
|
2020-04-30 17:09:47 -04:00
|
|
|
theme,
|
2018-11-28 13:39:27 -05:00
|
|
|
flowchart: {
|
2020-03-12 05:09:55 -04:00
|
|
|
useMaxWidth: true,
|
2018-11-28 13:39:27 -05:00
|
|
|
htmlLabels: false,
|
|
|
|
},
|
2019-07-23 09:42:42 -04:00
|
|
|
securityLevel: 'strict',
|
2018-10-10 02:09:55 -04:00
|
|
|
});
|
2017-11-21 22:12:04 -05:00
|
|
|
|
2020-03-16 08:09:12 -04:00
|
|
|
mermaidModule = mermaid;
|
|
|
|
|
|
|
|
return mermaid;
|
|
|
|
})
|
2020-12-23 16:10:24 -05:00
|
|
|
.catch((err) => {
|
2020-03-16 08:09:12 -04:00
|
|
|
flash(sprintf(__("Can't load mermaid module: %{err}"), { err }));
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixElementSource(el) {
|
|
|
|
// Mermaid doesn't like `<br />` tags, so collapse all like tags into `<br>`, which is parsed correctly.
|
|
|
|
const source = el.textContent.replace(/<br\s*\/>/g, '<br>');
|
|
|
|
|
|
|
|
// Remove any extra spans added by the backend syntax highlighting.
|
|
|
|
Object.assign(el, { textContent: source });
|
|
|
|
|
|
|
|
return { source };
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderMermaidEl(el) {
|
2020-12-23 16:10:24 -05:00
|
|
|
mermaidModule.init(undefined, el, (id) => {
|
2020-03-16 08:09:12 -04:00
|
|
|
const source = el.textContent;
|
|
|
|
const svg = document.getElementById(id);
|
|
|
|
|
|
|
|
// As of https://github.com/knsv/mermaid/commit/57b780a0d,
|
|
|
|
// Mermaid will make two init callbacks:one to initialize the
|
|
|
|
// flow charts, and another to initialize the Gannt charts.
|
|
|
|
// Guard against an error caused by double initialization.
|
|
|
|
if (svg.classList.contains('mermaid')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:08:55 -04:00
|
|
|
svg.classList.add('mermaid');
|
2020-03-16 08:09:12 -04:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderMermaids($els) {
|
|
|
|
if (!$els.length) return;
|
|
|
|
|
|
|
|
// A diagram may have been truncated in search results which will cause errors, so abort the render.
|
|
|
|
if (document.querySelector('body').dataset.page === 'search:show') return;
|
|
|
|
|
|
|
|
importMermaidModule()
|
|
|
|
.then(() => {
|
2019-09-23 08:25:41 -04:00
|
|
|
let renderedChars = 0;
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
$els.each((i, el) => {
|
2020-12-07 16:10:08 -05:00
|
|
|
// Skipping all the elements which we've already queued in requestIdleCallback
|
|
|
|
if (elsProcessingMap.has(el)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-16 08:09:12 -04:00
|
|
|
const { source } = fixElementSource(el);
|
2019-02-22 03:12:25 -05:00
|
|
|
/**
|
2020-12-07 16:10:08 -05:00
|
|
|
* Restrict the rendering to a certain amount of character
|
|
|
|
* and mermaid blocks to prevent mermaidjs from hanging
|
|
|
|
* up the entire thread and causing a DoS.
|
2019-02-22 03:12:25 -05:00
|
|
|
*/
|
2020-12-07 16:10:08 -05:00
|
|
|
if (
|
|
|
|
(source && source.length > MAX_CHAR_LIMIT) ||
|
|
|
|
renderedChars > MAX_CHAR_LIMIT ||
|
|
|
|
renderedMermaidBlocks >= MAX_MERMAID_BLOCK_LIMIT
|
|
|
|
) {
|
2020-03-16 08:09:12 -04:00
|
|
|
const html = `
|
|
|
|
<div class="alert gl-alert gl-alert-warning alert-dismissible lazy-render-mermaid-container js-lazy-render-mermaid-container fade show" role="alert">
|
|
|
|
<div>
|
|
|
|
<div class="display-flex">
|
|
|
|
<div>${__(
|
|
|
|
'Warning: Displaying this diagram might cause performance issues on this page.',
|
|
|
|
)}</div>
|
|
|
|
<div class="gl-alert-actions">
|
2021-01-21 01:09:03 -05:00
|
|
|
<button class="js-lazy-render-mermaid btn gl-alert-action btn-warning btn-md gl-button">Display</button>
|
2020-03-16 08:09:12 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
|
|
|
|
<span aria-hidden="true">×</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
|
|
|
const $parent = $(el).parent();
|
|
|
|
|
|
|
|
if (!$parent.hasClass('lazy-alert-shown')) {
|
|
|
|
$parent.after(html);
|
|
|
|
$parent.addClass('lazy-alert-shown');
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:12:25 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-23 08:25:41 -04:00
|
|
|
renderedChars += source.length;
|
2020-12-07 16:10:08 -05:00
|
|
|
renderedMermaidBlocks += 1;
|
|
|
|
|
|
|
|
const requestId = window.requestIdleCallback(() => {
|
|
|
|
renderMermaidEl(el);
|
|
|
|
});
|
2017-12-20 08:48:09 -05:00
|
|
|
|
2020-12-07 16:10:08 -05:00
|
|
|
elsProcessingMap.set(el, requestId);
|
2017-12-20 08:48:09 -05:00
|
|
|
});
|
2018-10-10 02:09:55 -04:00
|
|
|
})
|
2020-12-23 16:10:24 -05:00
|
|
|
.catch((err) => {
|
2020-03-16 08:09:12 -04:00
|
|
|
flash(sprintf(__('Encountered an error while rendering: %{err}'), { err }));
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(err);
|
2017-11-22 13:40:11 -05:00
|
|
|
});
|
|
|
|
}
|
2020-02-17 04:08:52 -05:00
|
|
|
|
2020-03-16 08:09:12 -04:00
|
|
|
const hookLazyRenderMermaidEvent = once(() => {
|
|
|
|
$(document.body).on('click', '.js-lazy-render-mermaid', function eventHandler() {
|
|
|
|
const parent = $(this).closest('.js-lazy-render-mermaid-container');
|
|
|
|
const pre = parent.prev();
|
|
|
|
|
|
|
|
const el = pre.find('.js-render-mermaid');
|
|
|
|
|
|
|
|
parent.remove();
|
|
|
|
|
|
|
|
renderMermaidEl(el);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-17 04:08:52 -05:00
|
|
|
export default function renderMermaid($els) {
|
|
|
|
if (!$els.length) return;
|
|
|
|
|
|
|
|
const visibleMermaids = $els.filter(function filter() {
|
2020-06-22 14:08:47 -04:00
|
|
|
return $(this).closest('details').length === 0 && $(this).is(':visible');
|
2020-02-17 04:08:52 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
renderMermaids(visibleMermaids);
|
|
|
|
|
|
|
|
$els.closest('details').one('toggle', function toggle() {
|
|
|
|
if (this.open) {
|
|
|
|
renderMermaids($(this).find('.js-render-mermaid'));
|
|
|
|
}
|
|
|
|
});
|
2020-03-16 08:09:12 -04:00
|
|
|
|
|
|
|
hookLazyRenderMermaidEvent();
|
2020-02-17 04:08:52 -05:00
|
|
|
}
|