Browse Source

Fix illegal markdown text caused renderer crash

master
Menci 6 years ago
parent
commit
0cbc2bacc7
  1. 15
      libs/markdown.js
  2. 1
      package.json

15
libs/markdown.js

@ -35,10 +35,12 @@ let config = {
highlight: require('./highlight') highlight: require('./highlight')
}; };
let uuid = require('uuid');
function render(s, cb) { function render(s, cb) {
if (!s.trim()) return cb(''); if (!s.trim()) return cb('');
let mathCnt = 0, mathPending = 0, maths = new Array(), hlCnt = 0, hlPending = 0, hls = new Array(), res, callback, ss, cache = render.cache, cacheOption = render.cacheOption, finished = false; let mathCnt = 0, mathPending = 0, maths = new Array(), mathID = new Array(), hlCnt = 0, hlPending = 0, hls = new Array(), hlID = new Array(), res, callback, ss, cache = render.cache, cacheOption = render.cacheOption, finished = false;
if (cacheOption.result) { if (cacheOption.result) {
let x = cache.get('RES_' + s); let x = cache.get('RES_' + s);
if (x !== undefined) return cb(x); if (x !== undefined) return cb(x);
@ -59,7 +61,7 @@ function render(s, cb) {
if (cacheOption.highlight) cache.set('H_' + lang + '_' + code, res); if (cacheOption.highlight) cache.set('H_' + lang + '_' + code, res);
if (!--hlPending) finish(); if (!--hlPending) finish();
}); });
return '<span id="hl-' + id + '"></span>'; return hlID[id] = uuid();
}, },
mathRenderer: function(str, display) { mathRenderer: function(str, display) {
if (cacheOption.math) { if (cacheOption.math) {
@ -86,7 +88,7 @@ function render(s, cb) {
if (!--mathPending) finish(); if (!--mathPending) finish();
}); });
return '<span id="math-' + id + '"></span>'; return mathID[id] = uuid();
} }
} }
}); });
@ -95,15 +97,12 @@ function render(s, cb) {
if (finished || !res || mathPending || hlPending) return; if (finished || !res || mathPending || hlPending) return;
finished = true; finished = true;
if (maths.length || hls.length) { if (maths.length || hls.length) {
let x = new (require('jsdom').JSDOM)().window.document.createElement('div');
x.innerHTML = res;
for (let i = 0; i < maths.length; i++) { for (let i = 0; i < maths.length; i++) {
x.querySelector('#math-' + i).outerHTML = maths[i]; res = res.replace(mathID[i], maths[i]);
} }
for (let i = 0; i < hls.length; i++) { for (let i = 0; i < hls.length; i++) {
x.querySelector('#hl-' + i).outerHTML = hls[i]; res = res.replace(hlID[i], hls[i]);
} }
res = x.innerHTML;
} }
if (cacheOption.result) cache.set('RES_' + s, res); if (cacheOption.result) cache.set('RES_' + s, res);
cb(res); cb(res);

1
package.json

@ -40,7 +40,6 @@
"gravatar": "^1.5.2", "gravatar": "^1.5.2",
"javascript-time-ago": "^1.0.30", "javascript-time-ago": "^1.0.30",
"js-yaml": "^3.9.0", "js-yaml": "^3.9.0",
"jsdom": "^13.0.0",
"jsondiffpatch": "0.3.11", "jsondiffpatch": "0.3.11",
"jsonwebtoken": "^8.3.0", "jsonwebtoken": "^8.3.0",
"katex": "^0.10.0", "katex": "^0.10.0",

Loading…
Cancel
Save