renderer.js 1.42 KB
Newer Older
1
const md = require('markdown-it')
2
const mdAttrs = require('markdown-it-attrs')
3
const _ = require('lodash')
4
const underline = require('./underline')
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

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) {
30 31 32 33 34
        if (lang === 'diagram') {
          return `<pre class="diagram">` + Buffer.from(str, 'base64').toString() + `</pre>`
        } else {
          return `<pre><code class="language-${lang}">${_.escape(str)}</code></pre>`
        }
35 36 37
      }
    })

38 39 40 41
    if (this.config.underline) {
      mkdown.use(underline)
    }

42
    mkdown.use(mdAttrs, {
43
      allowedAttributes: ['id', 'class', 'target']
44
    })
45

46 47
    for (let child of this.children) {
      const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
48
      await renderer.init(mkdown, child.config)
49 50
    }

51 52 53
    return mkdown.render(this.input)
  }
}