bugfix: Mermaid parser error will not throw exception

This commit is contained in:
Le Tan 2017-03-27 19:49:00 +08:00
parent 3406eab29c
commit b690db3502
3 changed files with 22 additions and 6 deletions

View File

@ -9,12 +9,16 @@ var updateHtml = function(html) {
if (code.parentElement.tagName.toLowerCase() == 'pre') { if (code.parentElement.tagName.toLowerCase() == 'pre') {
if (VEnableMermaid && code.classList.contains('language-mermaid')) { if (VEnableMermaid && code.classList.contains('language-mermaid')) {
// Mermaid code block. // Mermaid code block.
mermaidParserErr = false;
try { try {
var graph = mermaidAPI.render('mermaid-diagram-' + mermaidIdx++, code.innerText, function(){}); var graph = mermaidAPI.render('mermaid-diagram-' + mermaidIdx++, code.innerText, function(){});
} catch (err) { } catch (err) {
content.setLog("err: " + err); content.setLog("err: " + err);
continue; continue;
} }
if (mermaidParserErr) {
continue;
}
var graphDiv = document.createElement('div'); var graphDiv = document.createElement('div');
graphDiv.classList.add(VMermaidDivClass); graphDiv.classList.add(VMermaidDivClass);
graphDiv.innerHTML = graph; 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 { try {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, placeholder]); MathJax.Hub.Queue(["Typeset", MathJax.Hub, placeholder]);
} catch (err) { } catch (err) {
content.setLog("err: " + err); content.setLog("err: " + err);
} }
} }
} };

View File

@ -176,5 +176,5 @@ var updateText = function(text) {
content.setLog("err: " + err); content.setLog("err: " + err);
} }
} }
} };

View File

@ -56,7 +56,7 @@ window.onscroll = function() {
if (curHeader != null) { if (curHeader != null) {
content.setHeader(curHeader); content.setHeader(curHeader);
} }
} };
document.onkeydown = function(e) { document.onkeydown = function(e) {
e = e || window.event; e = e || window.event;
@ -134,7 +134,14 @@ document.onkeydown = function(e) {
return; return;
} }
e.preventDefault(); 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'. // @className, the class name of the mermaid code block, such as 'lang-mermaid'.
var renderMermaid = function(className) { var renderMermaid = function(className) {
@ -147,12 +154,16 @@ var renderMermaid = function(className) {
var code = codes[i]; var code = codes[i];
if (code.classList.contains(className)) { if (code.classList.contains(className)) {
// Mermaid code block. // Mermaid code block.
mermaidParserErr = false;
try { try {
var graph = mermaidAPI.render('mermaid-diagram-' + mermaidIdx++, code.innerText, function(){}); var graph = mermaidAPI.render('mermaid-diagram-' + mermaidIdx++, code.innerText, function(){});
} catch (err) { } catch (err) {
content.setLog("err: " + err); content.setLog("err: " + err);
continue; continue;
} }
if (mermaidParserErr) {
continue;
}
var graphDiv = document.createElement('div'); var graphDiv = document.createElement('div');
graphDiv.classList.add(VMermaidDivClass); graphDiv.classList.add(VMermaidDivClass);
graphDiv.innerHTML = graph; graphDiv.innerHTML = graph;
@ -163,4 +174,4 @@ var renderMermaid = function(className) {
--i; --i;
} }
} }
} };