Menci
6 years ago
6 changed files with 100 additions and 47 deletions
@ -1,8 +0,0 @@
|
||||
const { highlight } = require('syzoj-renderer'); |
||||
const objectHash = require('object-hash'); |
||||
|
||||
module.exports = async (code, lang, cb) => { |
||||
highlight(code, lang, syzoj.redisCache, { |
||||
wrapper: null |
||||
}).then(cb); |
||||
} |
@ -1,33 +0,0 @@
|
||||
const { markdown } = require('syzoj-renderer'); |
||||
const XSS = require('xss'); |
||||
const CSSFilter = require('cssfilter'); |
||||
const xssWhiteList = Object.assign({}, require('xss/lib/default').whiteList); |
||||
|
||||
delete xssWhiteList.audio; |
||||
delete xssWhiteList.video; |
||||
|
||||
for (const tag in xssWhiteList) { |
||||
xssWhiteList[tag] = xssWhiteList[tag].concat(['style', 'class']); |
||||
} |
||||
|
||||
const xss = new XSS.FilterXSS({ |
||||
whiteList: xssWhiteList, |
||||
stripIgnoreTag: true, |
||||
onTagAttr: (tag, name, value, isWhiteAttr) => { |
||||
if (tag.toLowerCase() === 'img' && name.toLowerCase() === 'src' && value.startsWith('data:image/')) { |
||||
return name + '="' + XSS.escapeAttrValue(value) + '"'; |
||||
} |
||||
} |
||||
}); |
||||
|
||||
function filter(html) { |
||||
html = xss.process(html); |
||||
if (html) { |
||||
html = `<div style="position: relative; overflow: hidden; ">${html}</div>`; |
||||
} |
||||
return html; |
||||
}; |
||||
|
||||
module.exports = (markdownCode, callback) => { |
||||
markdown(markdownCode, syzoj.redisCache, filter).then(callback); |
||||
}; |
@ -0,0 +1,32 @@
|
||||
const child_process = require('child_process'); |
||||
|
||||
const rendererd = child_process.fork(__dirname + '/rendererd', [syzoj.config.redis]); |
||||
|
||||
const resolver = {}; |
||||
let currentId = 0; |
||||
|
||||
rendererd.on('message', msg => { |
||||
resolver[msg.id](msg.result); |
||||
delete resolver[msg.id]; |
||||
}); |
||||
|
||||
exports.markdown = (markdownCode, callback) => { |
||||
resolver[++currentId] = callback; |
||||
rendererd.send({ |
||||
id: currentId, |
||||
type: 'markdown', |
||||
source: markdownCode |
||||
}); |
||||
} |
||||
|
||||
exports.highlight = (code, lang, callback) => { |
||||
resolver[++currentId] = callback; |
||||
rendererd.send({ |
||||
id: currentId, |
||||
type: 'highlight', |
||||
source: { |
||||
code, |
||||
lang |
||||
} |
||||
}); |
||||
} |
@ -0,0 +1,61 @@
|
||||
const renderer = require('syzoj-renderer'); |
||||
const XSS = require('xss'); |
||||
const xssWhiteList = Object.assign({}, require('xss/lib/default').whiteList); |
||||
delete xssWhiteList.audio; |
||||
delete xssWhiteList.video; |
||||
|
||||
for (const tag in xssWhiteList) { |
||||
xssWhiteList[tag] = xssWhiteList[tag].concat(['style', 'class']); |
||||
} |
||||
|
||||
const xss = new XSS.FilterXSS({ |
||||
whiteList: xssWhiteList, |
||||
stripIgnoreTag: true, |
||||
onTagAttr: (tag, name, value, isWhiteAttr) => { |
||||
if (tag.toLowerCase() === 'img' && name.toLowerCase() === 'src' && value.startsWith('data:image/')) { |
||||
return name + '="' + XSS.escapeAttrValue(value) + '"'; |
||||
} |
||||
} |
||||
}); |
||||
|
||||
const Redis = require('redis'); |
||||
const util = require('util'); |
||||
const redis = Redis.createClient(process.argv[2]); |
||||
const redisCache = { |
||||
get: util.promisify(redis.get).bind(redis), |
||||
set: util.promisify(redis.set).bind(redis) |
||||
}; |
||||
|
||||
async function highlight(code, lang) { |
||||
return await renderer.highlight(code, lang, redisCache, { |
||||
wrapper: null |
||||
}); |
||||
} |
||||
|
||||
async function markdown(markdownCode) { |
||||
function filter(html) { |
||||
html = xss.process(html); |
||||
if (html) { |
||||
html = `<div style="position: relative; overflow: hidden; ">${html}</div>`; |
||||
} |
||||
return html; |
||||
}; |
||||
|
||||
return await renderer.markdown(markdownCode, redisCache, filter); |
||||
} |
||||
|
||||
process.on('message', async msg => { |
||||
if (msg.type === 'markdown') { |
||||
process.send({ |
||||
id: msg.id, |
||||
result: await markdown(msg.source) |
||||
}); |
||||
} else if (msg.type === 'highlight') { |
||||
process.send({ |
||||
id: msg.id, |
||||
result: await highlight(msg.source.code, msg.source.lang) |
||||
}); |
||||
} |
||||
}); |
||||
|
||||
process.on('disconnect', () => process.exit()); |
Loading…
Reference in new issue