Skip to content

Commit 568355a

Browse files
committed
fix: properly validate mermaid syntax and handle parse error
Signed-off-by: Max Wu <jackymaxj@gmail.com>
1 parent 064dfb7 commit 568355a

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

public/js/extra.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,12 @@ function replaceExtraTags (html) {
253253
return html
254254
}
255255

256-
if (typeof window.mermaid !== 'undefined' && window.mermaid) window.mermaid.startOnLoad = false
256+
if (typeof window.mermaid !== 'undefined' && window.mermaid) {
257+
window.mermaid.startOnLoad = false
258+
window.mermaid.parseError = function (err, hash) {
259+
console.warn(err)
260+
}
261+
}
257262

258263
// dynamic event or object binding here
259264
export function finishView (view) {
@@ -397,13 +402,14 @@ export function finishView (view) {
397402
var $value = $(value)
398403
const $ele = $(value).closest('pre')
399404

400-
/* eslint-disable prefer-const */
401-
let text = $value.text()
402-
window.mermaid.parse(text)
403-
$ele.addClass('mermaid')
404-
$ele.text(text)
405-
/* eslint-enable prefer-const */
406-
window.mermaid.init(undefined, $ele)
405+
const text = $value.text()
406+
// validate the syntax first
407+
if (window.mermaid.parse(text)) {
408+
$ele.addClass('mermaid')
409+
$ele.text(text)
410+
// render the diagram
411+
window.mermaid.init(undefined, $ele)
412+
}
407413
} catch (err) {
408414
$value.unwrap()
409415
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err.str)}</div>`)

0 commit comments

Comments
 (0)