Skip to content

Commit 470e228

Browse files
authored
Merge pull request #1919 from hackmdio/bugfix/dev-2044-mermaid-bundling-issue
chore: integrate mermaid.js into the build process and update initialization in extra.js
2 parents 0051081 + 3043f56 commit 470e228

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

public/js/extra.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ function replaceExtraTags (html) {
254254
}
255255

256256
if (typeof window.mermaid !== 'undefined' && window.mermaid) {
257-
window.mermaid.startOnLoad = false
257+
window.mermaid.initialize({
258+
startOnLoad: false
259+
})
260+
258261
window.mermaid.parseError = function (err, hash) {
259262
console.warn(err)
260263
}
@@ -433,22 +436,26 @@ export function finishView (view) {
433436
})
434437
// mermaid
435438
const mermaids = view.find('div.mermaid.raw').removeClass('raw')
436-
mermaids.each((key, value) => {
439+
mermaids.each(async (key, value) => {
440+
const $value = $(value)
441+
const $ele = $value.closest('pre')
437442
try {
438-
var $value = $(value)
439-
const $ele = $(value).closest('pre')
440-
441443
const text = $value.text()
442444
// validate the syntax first
443445
if (window.mermaid.parse(text)) {
444446
$ele.addClass('mermaid')
445447
$ele.text(text)
446448
// render the diagram
447-
window.mermaid.init(undefined, $ele)
449+
const id = `mermaid-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
450+
const { svg, bindFunctions } = await window.mermaid.render(id, text, $ele[0])
451+
$ele.html(svg)
452+
if (bindFunctions) {
453+
bindFunctions($ele[0])
454+
}
448455
}
449456
} catch (err) {
450457
$value.unwrap()
451-
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err.str)}</div>`)
458+
$ele.append(`<div class="alert alert-warning">${escapeHTML(err)}</div>`)
452459
console.warn(err)
453460
}
454461
})

public/views/codimd/foot.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
<script src="<%- serverURL %>/build/MathJax/MathJax.js" defer></script>
3131
<script src="<%- serverURL %>/build/MathJax/config/TeX-AMS-MML_HTMLorMML.js" defer></script>
3232
<script src="<%- serverURL %>/build/MathJax/config/Safe.js" defer></script>
33+
<script src="<%- serverURL %>/build/mermaid/mermaid.min.js" defer></script>
3334
<%- include('../build/index-pack-scripts') %>
3435
<% } %>

public/views/pretty.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<script src="<%- serverURL %>/build/MathJax/MathJax.js" defer></script>
106106
<script src="<%- serverURL %>/build/MathJax/config/TeX-AMS-MML_HTMLorMML.js" defer></script>
107107
<script src="<%- serverURL %>/build/MathJax/config/Safe.js" defer></script>
108+
<script src="<%- serverURL %>/build/mermaid/mermaid.min.js" defer></script>
108109
<%- include('build/pretty-pack-scripts') %>
109110
<% } %>
110111
<%- include('shared/ga') %>

public/views/slide.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<script src="<%- serverURL %>/build/MathJax/MathJax.js" defer></script>
118118
<script src="<%- serverURL %>/build/MathJax/config/TeX-AMS-MML_HTMLorMML.js" defer></script>
119119
<script src="<%- serverURL %>/build/MathJax/config/Safe.js" defer></script>
120+
<script src="<%- serverURL %>/build/mermaid/mermaid.min.js" defer></script>
120121
<%- include('build/slide-pack-scripts') %>
121122
<% } %>
122123
</body>

webpack.common.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ module.exports = {
205205
context: path.join(__dirname, 'node_modules/fork-awesome'),
206206
from: 'css',
207207
to: 'fork-awesome/css'
208+
},
209+
{
210+
context: path.join(__dirname, 'node_modules/mermaid'),
211+
from: 'dist/mermaid.min.js',
212+
to: 'mermaid/mermaid.min.js'
208213
}
209214
]),
210215
new MiniCssExtractPlugin(
@@ -295,7 +300,6 @@ module.exports = {
295300
'script-loader!jquery-ui-resizable',
296301
'bootstrap-validator',
297302
'expose-loader?jsyaml!js-yaml',
298-
'script-loader!mermaid',
299303
'expose-loader?moment!moment',
300304
'script-loader!handlebars',
301305
'expose-loader?hljs!highlight.js',
@@ -340,7 +344,6 @@ module.exports = {
340344
'pretty-pack': [
341345
'babel-polyfill',
342346
'expose-loader?jsyaml!js-yaml',
343-
'script-loader!mermaid',
344347
'expose-loader?moment!moment',
345348
'script-loader!handlebars',
346349
'expose-loader?hljs!highlight.js',
@@ -383,7 +386,6 @@ module.exports = {
383386
'imports-loader?$=jquery!jquery-mousewheel',
384387
'bootstrap-tooltip',
385388
'expose-loader?jsyaml!js-yaml',
386-
'script-loader!mermaid',
387389
'expose-loader?moment!moment',
388390
'script-loader!handlebars',
389391
'expose-loader?hljs!highlight.js',
@@ -419,7 +421,6 @@ module.exports = {
419421
jqueryTextcomplete: path.join(__dirname, 'public/vendor/jquery-textcomplete/jquery.textcomplete.js'),
420422
codemirrorInlineAttachment: path.join(__dirname, 'public/vendor/inlineAttachment/codemirror.inline-attachment.js'),
421423
ot: path.join(__dirname, 'public/vendor/ot/ot.min.js'),
422-
mermaid: path.join(__dirname, 'node_modules/mermaid/dist/mermaid.min.js'),
423424
handlebars: path.join(__dirname, 'node_modules/handlebars/dist/handlebars.min.js'),
424425
'jquery-ui-resizable': path.join(__dirname, 'public/vendor/jquery-ui/jquery-ui.min.js'),
425426
'gist-embed': path.join(__dirname, 'node_modules/gist-embed/gist-embed.min.js'),

0 commit comments

Comments
 (0)