From b690db35025bbef0b4ac8ed2c8e6e6a962967bc0 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Mon, 27 Mar 2017 19:49:00 +0800 Subject: [PATCH] bugfix: Mermaid parser error will not throw exception --- src/resources/hoedown.js | 9 +++++++-- src/resources/markdown-it.js | 2 +- src/resources/markdown_template.js | 17 ++++++++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/resources/hoedown.js b/src/resources/hoedown.js index 79b608ff..f68df31a 100644 --- a/src/resources/hoedown.js +++ b/src/resources/hoedown.js @@ -9,12 +9,16 @@ var updateHtml = function(html) { if (code.parentElement.tagName.toLowerCase() == 'pre') { if (VEnableMermaid && code.classList.contains('language-mermaid')) { // Mermaid code block. + mermaidParserErr = false; try { var graph = mermaidAPI.render('mermaid-diagram-' + mermaidIdx++, code.innerText, function(){}); } catch (err) { content.setLog("err: " + err); continue; } + if (mermaidParserErr) { + continue; + } var graphDiv = document.createElement('div'); graphDiv.classList.add(VMermaidDivClass); graphDiv.innerHTML = graph; @@ -29,12 +33,13 @@ var updateHtml = function(html) { } } - if (VEnableMathjax) { + // MathJax may be not loaded for now. + if (VEnableMathjax && (typeof MathJax != "undefined")) { try { MathJax.Hub.Queue(["Typeset", MathJax.Hub, placeholder]); } catch (err) { content.setLog("err: " + err); } } -} +}; diff --git a/src/resources/markdown-it.js b/src/resources/markdown-it.js index 583b9eab..a4d3afbe 100644 --- a/src/resources/markdown-it.js +++ b/src/resources/markdown-it.js @@ -176,5 +176,5 @@ var updateText = function(text) { content.setLog("err: " + err); } } -} +}; diff --git a/src/resources/markdown_template.js b/src/resources/markdown_template.js index d9cbc9c8..4b83e01b 100644 --- a/src/resources/markdown_template.js +++ b/src/resources/markdown_template.js @@ -56,7 +56,7 @@ window.onscroll = function() { if (curHeader != null) { content.setHeader(curHeader); } -} +}; document.onkeydown = function(e) { e = e || window.event; @@ -134,7 +134,14 @@ document.onkeydown = function(e) { return; } e.preventDefault(); -} +}; + +var mermaidParserErr = false; + +mermaidAPI.parseError = function(err, hash) { + content.setLog("err: " + err); + mermaidParserErr = true; +}; // @className, the class name of the mermaid code block, such as 'lang-mermaid'. var renderMermaid = function(className) { @@ -147,12 +154,16 @@ var renderMermaid = function(className) { var code = codes[i]; if (code.classList.contains(className)) { // Mermaid code block. + mermaidParserErr = false; try { var graph = mermaidAPI.render('mermaid-diagram-' + mermaidIdx++, code.innerText, function(){}); } catch (err) { content.setLog("err: " + err); continue; } + if (mermaidParserErr) { + continue; + } var graphDiv = document.createElement('div'); graphDiv.classList.add(VMermaidDivClass); graphDiv.innerHTML = graph; @@ -163,4 +174,4 @@ var renderMermaid = function(className) { --i; } } -} +};