From 0cbc2bacc7ab54ad3cd19a2128a157e6b1b67b1c Mon Sep 17 00:00:00 2001 From: Menci Date: Thu, 8 Nov 2018 19:58:18 +0800 Subject: [PATCH] Fix illegal markdown text caused renderer crash --- libs/markdown.js | 15 +++++++-------- package.json | 1 - 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/libs/markdown.js b/libs/markdown.js index 19b3519..e06ad40 100644 --- a/libs/markdown.js +++ b/libs/markdown.js @@ -35,10 +35,12 @@ let config = { highlight: require('./highlight') }; +let uuid = require('uuid'); + function render(s, 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) { let x = cache.get('RES_' + s); if (x !== undefined) return cb(x); @@ -59,7 +61,7 @@ function render(s, cb) { if (cacheOption.highlight) cache.set('H_' + lang + '_' + code, res); if (!--hlPending) finish(); }); - return ''; + return hlID[id] = uuid(); }, mathRenderer: function(str, display) { if (cacheOption.math) { @@ -86,7 +88,7 @@ function render(s, cb) { if (!--mathPending) finish(); }); - return ''; + return mathID[id] = uuid(); } } }); @@ -95,15 +97,12 @@ function render(s, cb) { if (finished || !res || mathPending || hlPending) return; finished = true; 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++) { - x.querySelector('#math-' + i).outerHTML = maths[i]; + res = res.replace(mathID[i], maths[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); cb(res); diff --git a/package.json b/package.json index ff8eed6..62adb05 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "gravatar": "^1.5.2", "javascript-time-ago": "^1.0.30", "js-yaml": "^3.9.0", - "jsdom": "^13.0.0", "jsondiffpatch": "0.3.11", "jsonwebtoken": "^8.3.0", "katex": "^0.10.0",