Browse Source

Merge branch 'master' of git.runa.moe:syzoj/syzoj_ruc

pull/6/head
Menci 6 years ago
parent
commit
5a29440a47
  1. 60
      libs/markdown.js
  2. 34
      utility.js

60
libs/markdown.js

@ -64,32 +64,35 @@ function render(s, cb) {
return hlID[id] = uuid(); return hlID[id] = uuid();
}, },
mathRenderer: function(str, display) { mathRenderer: function(str, display) {
if (cacheOption.math) { let mathFinish = (error, result) => {
let x = cache.get('M_' + display + '_' + str); if (error) maths[id] = '<p><div style="display: inline-block; border: 1px solid #000; "><strong>' + data.errors.toString() + '</strong></div></p>';
if (x !== undefined) return x; else if (display) maths[id] = '<p style="text-align: center; ">' + result + '</p>';
else maths[id] = result;
if (cacheOption.math) cache.set('M_' + display + '_' + str, maths[id]);
if (!--mathPending) finish();
} }
const id = mathCnt;
mathCnt++, mathPending++;
try { try {
let res = katex.renderToString(str, { displayMode: display }); let x = cache.get('M_' + display + '_' + str);
if (cacheOption.math) cache.set('M_' + display + '_' + str, res); if (x !== undefined) process.nextTick(() => mathFinish(null, x));
return res; else {
let res = katex.renderToString(str, { displayMode: display });
process.nextTick(() => mathFinish(null, '<span style="zoom: 1.01; ">' + res + '</span>'));
}
} catch (e) { } catch (e) {
const id = mathCnt;
mathCnt++, mathPending++;
mj.typeset({ mj.typeset({
math: str, math: str,
format: display ? 'TeX' : 'inline-TeX', format: display ? 'TeX' : 'inline-TeX',
html: true, css: true, svg: true,
width: 0 width: 0
}, function (data) { }, data => {
if (data.errors) maths[id] = '<p><div style="display: inline-block; border: 1px solid #000; "><strong>' + data.errors.toString() + '</strong></div></p>'; mathFinish(data.errors, data.svg);
else if (display) maths[id] = '<p style="text-align: center; ">' + data.html + '</p>';
else maths[id] = data.html;
if (cacheOption.math) cache.set('M_' + display + '_' + str, maths[id]);
if (!--mathPending) finish();
}); });
return mathID[id] = uuid();
} }
return mathID[id] = uuid();
} }
}); });
@ -109,7 +112,28 @@ function render(s, cb) {
} }
try { try {
res = MoeMark(s); let XSS = require('xss');
let CSSFilter = require('cssfilter');
let whiteList = Object.assign({}, require('xss/lib/default').whiteList);
delete whiteList.audio;
delete whiteList.video;
for (let tag in whiteList) whiteList[tag] = whiteList[tag].concat(['style', 'class']);
let xss = new XSS.FilterXSS({
whiteList: whiteList,
stripIgnoreTag: true,
onTagAttr: (tag, name, value, isWhiteAttr) => {
if (tag.toLowerCase() === 'img' && name.toLowerCase() === 'src' && value.startsWith('data:image/')) return name + '="' + XSS.escapeAttrValue(value) + '"';
}
});
let replaceXSS = s => {
s = xss.process(s);
if (s) {
s = `<div style="position: relative; overflow: hidden; ">${s}</div>`;
}
return s;
};
res = replaceXSS(MoeMark(s));
if (mathPending == 0 && hlPending == 0) { if (mathPending == 0 && hlPending == 0) {
finish(); finish();
} }

34
utility.js

@ -34,36 +34,6 @@ module.exports = {
return path.resolve.apply(null, a); return path.resolve.apply(null, a);
}, },
markdown(obj, keys, noReplaceUI) { markdown(obj, keys, noReplaceUI) {
let XSS = require('xss');
let CSSFilter = require('cssfilter');
let whiteList = Object.assign({}, require('xss/lib/default').whiteList);
delete whiteList.audio;
delete whiteList.video;
for (let tag in whiteList) whiteList[tag] = whiteList[tag].concat(['style', 'class']);
let xss = new XSS.FilterXSS({
css: {
whiteList: Object.assign({}, require('cssfilter/lib/default').whiteList, {
'vertical-align': true,
top: true,
bottom: true,
left: true,
right: true,
"white-space": true
})
},
whiteList: whiteList,
stripIgnoreTag: true,
onTagAttr: (tag, name, value, isWhiteAttr) => {
if (tag.toLowerCase() === 'img' && name.toLowerCase() === 'src' && value.startsWith('data:image/')) return name + '="' + XSS.escapeAttrValue(value) + '"';
}
});
let replaceXSS = s => {
s = xss.process(s);
if (s) {
s = `<div style="position: relative; overflow: hidden; ">${s}</div>`;
}
return s;
};
let replaceUI = s => { let replaceUI = s => {
if (noReplaceUI) return s; if (noReplaceUI) return s;
@ -90,13 +60,13 @@ module.exports = {
if (!keys) { if (!keys) {
if (!obj || !obj.trim()) resolve(""); if (!obj || !obj.trim()) resolve("");
else markdownRenderer(obj, s => { else markdownRenderer(obj, s => {
resolve(replaceUI(replaceXSS(s))); resolve(replaceUI(s));
}); });
} else { } else {
let res = obj, cnt = keys.length; let res = obj, cnt = keys.length;
for (let key of keys) { for (let key of keys) {
markdownRenderer(res[key], (s) => { markdownRenderer(res[key], (s) => {
res[key] = replaceUI(replaceXSS(s)); res[key] = replaceUI(s);
if (!--cnt) resolve(res); if (!--cnt) resolve(res);
}); });
} }

Loading…
Cancel
Save