1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-10 11:21:29 +00:00
Files
seahub/frontend/src/utils/seafile-markdown2html.js

57 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-10-23 13:13:44 +08:00
var unified = require('unified');
var markdown = require('remark-parse');
var slug = require('remark-slug');
var breaks = require('remark-breaks');
var remark2rehype = require('remark-rehype');
var format = require('rehype-format');
var raw = require('rehype-raw');
var xtend = require('xtend');
var toHTML = require('hast-util-to-html');
var sanitize = require('hast-util-sanitize');
var gh = require('hast-util-sanitize/lib/github');
var deepmerge = require('deepmerge').default;
function stringify(config) {
var settings = xtend(config, this.data('settings'));
var schema = deepmerge(gh, {
2018-10-25 10:39:16 +08:00
'attributes': {
'input': [
'type',
2018-10-23 13:13:44 +08:00
],
2018-10-25 10:39:16 +08:00
'li': [
'className'
2018-10-23 13:13:44 +08:00
],
2018-10-25 10:39:16 +08:00
'code': [
'className',
2018-10-23 13:13:44 +08:00
],
},
2018-10-25 10:39:16 +08:00
'tagNames': [
'input',
'code'
2018-10-23 13:13:44 +08:00
]
});
this.Compiler = compiler;
function compiler(tree) {
2018-10-25 10:39:16 +08:00
// use sanity to remove dangerous html, the default is
2018-10-23 13:13:44 +08:00
var hast = sanitize(tree, schema);
return toHTML(hast, settings);
}
}
2018-10-25 10:39:16 +08:00
// markdown -> mdast -> html AST -> html
2018-10-23 13:13:44 +08:00
var processor = unified()
.use(markdown, {commonmark: true})
.use(breaks)
.use(slug)
.use(remark2rehype, {allowDangerousHTML: true})
.use(raw)
.use(format)
.use(stringify);
var processorGetAST = unified()
.use(markdown, {commonmark: true})
2018-10-25 10:39:16 +08:00
.use(slug);
2018-10-23 13:13:44 +08:00
export { processor, processorGetAST };