mirror of https://github.com/status-im/codimd.git
fix: properly validate mermaid syntax and handle parse error
Signed-off-by: Max Wu <jackymaxj@gmail.com>
This commit is contained in:
parent
064dfb7865
commit
568355acf5
|
@ -253,7 +253,12 @@ function replaceExtraTags (html) {
|
||||||
return 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
|
// dynamic event or object binding here
|
||||||
export function finishView (view) {
|
export function finishView (view) {
|
||||||
|
@ -397,13 +402,14 @@ export function finishView (view) {
|
||||||
var $value = $(value)
|
var $value = $(value)
|
||||||
const $ele = $(value).closest('pre')
|
const $ele = $(value).closest('pre')
|
||||||
|
|
||||||
/* eslint-disable prefer-const */
|
const text = $value.text()
|
||||||
let text = $value.text()
|
// validate the syntax first
|
||||||
window.mermaid.parse(text)
|
if (window.mermaid.parse(text)) {
|
||||||
$ele.addClass('mermaid')
|
$ele.addClass('mermaid')
|
||||||
$ele.text(text)
|
$ele.text(text)
|
||||||
/* eslint-enable prefer-const */
|
// render the diagram
|
||||||
window.mermaid.init(undefined, $ele)
|
window.mermaid.init(undefined, $ele)
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
$value.unwrap()
|
$value.unwrap()
|
||||||
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err.str)}</div>`)
|
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err.str)}</div>`)
|
||||||
|
|
Loading…
Reference in New Issue