|
|
@ -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); |
|
|
|