wikijs-fork/server/modules/rendering/markdown-core/renderer.js

56 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2018-09-10 00:33:10 +00:00
const md = require('markdown-it')
2018-11-24 02:10:24 +00:00
const mdAttrs = require('markdown-it-attrs')
const mdDecorate = require('markdown-it-decorate')
2018-09-10 00:33:10 +00:00
const _ = require('lodash')
const underline = require('./underline')
2018-09-10 00:33:10 +00:00
const quoteStyles = {
Chinese: '””‘’',
English: '“”‘’',
French: ['«\xA0', '\xA0»', '\xA0', '\xA0'],
German: '„“‚‘',
Greek: '«»‘’',
Japanese: '「」「」',
Hungarian: '„”’’',
Polish: '„”‚‘',
Portuguese: '«»‘’',
Russian: '«»„“',
Spanish: '«»‘’',
Swedish: '””’’'
}
module.exports = {
async render() {
const mkdown = md({
html: this.config.allowHTML,
breaks: this.config.linebreaks,
linkify: this.config.linkify,
typographer: this.config.typographer,
quotes: _.get(quoteStyles, this.config.quotes, quoteStyles.English),
highlight(str, lang) {
if (lang === 'diagram') {
return `<pre class="diagram">` + Buffer.from(str, 'base64').toString() + `</pre>`
} else {
return `<pre><code class="language-${lang}">${_.escape(str)}</code></pre>`
}
2018-09-10 00:33:10 +00:00
}
})
if (this.config.underline) {
mkdown.use(underline)
}
mkdown.use(mdAttrs, {
allowedAttributes: ['id', 'class', 'target']
})
mkdown.use(mdDecorate)
2018-11-24 02:10:24 +00:00
for (let child of this.children) {
const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
2020-04-30 02:30:56 +00:00
await renderer.init(mkdown, child.config)
}
2018-09-10 00:33:10 +00:00
return mkdown.render(this.input)
}
}