Merge branch 'fix-mermaid-import' into 'master'

Fixed import of render mermaid & render math methods

See merge request gitlab-org/gitlab-ce!15550
This commit is contained in:
Filipa Lacerda 2017-11-23 15:47:17 +00:00
commit d22c885743
5 changed files with 53 additions and 61 deletions

View file

@ -69,8 +69,6 @@ import './project_import';
import './projects_dropdown'; import './projects_dropdown';
import './projects_list'; import './projects_list';
import './syntax_highlight'; import './syntax_highlight';
import './render_math';
import './render_mermaid';
import './render_gfm'; import './render_gfm';
import './right_sidebar'; import './right_sidebar';
import './search'; import './search';

View file

@ -1,16 +1,15 @@
/* eslint-disable func-names, space-before-function-paren, consistent-return, no-var, no-else-return, prefer-arrow-callback, max-len */ import renderMath from './render_math';
import renderMermaid from './render_mermaid';
// Render Gitlab flavoured Markdown // Render Gitlab flavoured Markdown
// //
// Delegates to syntax highlight and render math & mermaid diagrams. // Delegates to syntax highlight and render math & mermaid diagrams.
// //
(function() { $.fn.renderGFM = function renderGFM() {
$.fn.renderGFM = function() { this.find('.js-syntax-highlight').syntaxHighlight();
this.find('.js-syntax-highlight').syntaxHighlight(); renderMath(this.find('.js-render-math'));
this.find('.js-render-math').renderMath(); renderMermaid(this.find('.js-render-mermaid'));
this.find('.js-render-mermaid').renderMermaid(); return this;
return this; };
};
$(() => $('body').renderGFM()); $(() => $('body').renderGFM());
}).call(window);

View file

@ -1,4 +1,3 @@
/* eslint-disable func-names, space-before-function-paren, consistent-return, no-var, no-else-return, prefer-arrow-callback, max-len, no-console */
/* global katex */ /* global katex */
// Renders math using KaTeX in any element with the // Renders math using KaTeX in any element with the
@ -8,49 +7,45 @@
// //
// <code class="js-render-math"></div> // <code class="js-render-math"></div>
// //
(function() {
// Only load once // Only load once
var katexLoaded = false; let katexLoaded = false;
// Loop over all math elements and render math // Loop over all math elements and render math
var renderWithKaTeX = function (elements) { function renderWithKaTeX(elements) {
elements.each(function () { elements.each(function katexElementsLoop() {
var mathNode = $('<span></span>'); const mathNode = $('<span></span>');
var $this = $(this); const $this = $(this);
var display = $this.attr('data-math-style') === 'display'; const display = $this.attr('data-math-style') === 'display';
try { try {
katex.render($this.text(), mathNode.get(0), { displayMode: display }); katex.render($this.text(), mathNode.get(0), { displayMode: display });
mathNode.insertAfter($this); mathNode.insertAfter($this);
$this.remove(); $this.remove();
} catch (err) { } catch (err) {
// What can we do?? throw err;
console.log(err.message);
}
});
};
$.fn.renderMath = function() {
var $this = this;
if ($this.length === 0) return;
if (katexLoaded) renderWithKaTeX($this);
else {
// Request CSS file so it is in the cache
$.get(gon.katex_css_url, function() {
var css = $('<link>',
{ rel: 'stylesheet',
type: 'text/css',
href: gon.katex_css_url,
});
css.appendTo('head');
// Load KaTeX js
$.getScript(gon.katex_js_url, function() {
katexLoaded = true;
renderWithKaTeX($this); // Run KaTeX
});
});
} }
}; });
}).call(window); }
export default function renderMath($els) {
if (!$els.length) return;
if (katexLoaded) {
renderWithKaTeX($els);
} else {
$.get(gon.katex_css_url, () => {
const css = $('<link>', {
rel: 'stylesheet',
type: 'text/css',
href: gon.katex_css_url,
});
css.appendTo('head');
// Load KaTeX js
$.getScript(gon.katex_js_url, () => {
katexLoaded = true;
renderWithKaTeX($els); // Run KaTeX
});
});
}
}

View file

@ -14,8 +14,8 @@
import Flash from './flash'; import Flash from './flash';
$.fn.renderMermaid = function renderMermaid() { export default function renderMermaid($els) {
if (this.length === 0) return; if (!$els.length) return;
import(/* webpackChunkName: 'mermaid' */ 'blackst0ne-mermaid').then((mermaid) => { import(/* webpackChunkName: 'mermaid' */ 'blackst0ne-mermaid').then((mermaid) => {
mermaid.initialize({ mermaid.initialize({
@ -23,8 +23,10 @@ $.fn.renderMermaid = function renderMermaid() {
theme: 'neutral', theme: 'neutral',
}); });
mermaid.init(undefined, this); $els.each((i, el) => {
mermaid.init(undefined, el);
});
}).catch((err) => { }).catch((err) => {
Flash(`Can't load mermaid module: ${err}`); Flash(`Can't load mermaid module: ${err}`);
}); });
}; }

View file

@ -4,8 +4,6 @@
import 'autosize'; import 'autosize';
import '~/gl_form'; import '~/gl_form';
import '~/lib/utils/text_utility'; import '~/lib/utils/text_utility';
import '~/render_math';
import '~/render_mermaid';
import '~/render_gfm'; import '~/render_gfm';
import '~/notes'; import '~/notes';