Skip to content

Commit

Permalink
fix: properly validate mermaid syntax and handle parse error
Browse files Browse the repository at this point in the history
Signed-off-by: Max Wu <jackymaxj@gmail.com>
  • Loading branch information
jackycute committed Dec 21, 2020
1 parent 064dfb7 commit 568355a
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions public/js/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,12 @@ function replaceExtraTags (html) {
return html
}

if (typeof window.mermaid !== 'undefined' && window.mermaid) window.mermaid.startOnLoad = false
if (typeof window.mermaid !== 'undefined' && window.mermaid) {
window.mermaid.startOnLoad = false
window.mermaid.parseError = function (err, hash) {
console.warn(err)
}
}

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

/* eslint-disable prefer-const */
let text = $value.text()
window.mermaid.parse(text)
$ele.addClass('mermaid')
$ele.text(text)
/* eslint-enable prefer-const */
window.mermaid.init(undefined, $ele)
const text = $value.text()
// validate the syntax first
if (window.mermaid.parse(text)) {
$ele.addClass('mermaid')
$ele.text(text)
// render the diagram
window.mermaid.init(undefined, $ele)
}
} catch (err) {
$value.unwrap()
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err.str)}</div>`)
Expand Down

0 comments on commit 568355a

Please sign in to comment.