renderer.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const md = require('markdown-it')
  2. const mdAttrs = require('markdown-it-attrs')
  3. const mdDecorate = require('markdown-it-decorate')
  4. const _ = require('lodash')
  5. const underline = require('./underline')
  6. const quoteStyles = {
  7. Chinese: '””‘’',
  8. English: '“”‘’',
  9. French: ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'],
  10. German: '„“‚‘',
  11. Greek: '«»‘’',
  12. Japanese: '「」「」',
  13. Hungarian: '„”’’',
  14. Polish: '„”‚‘',
  15. Portuguese: '«»‘’',
  16. Russian: '«»„“',
  17. Spanish: '«»‘’',
  18. Swedish: '””’’'
  19. }
  20. module.exports = {
  21. async render() {
  22. const mkdown = md({
  23. html: this.config.allowHTML,
  24. breaks: this.config.linebreaks,
  25. linkify: this.config.linkify,
  26. typographer: this.config.typographer,
  27. quotes: _.get(quoteStyles, this.config.quotes, quoteStyles.English),
  28. highlight(str, lang) {
  29. if (lang === 'diagram') {
  30. return `<pre class="diagram">` + Buffer.from(str, 'base64').toString() + `</pre>`
  31. } else {
  32. return `<pre><code class="language-${lang}">${_.escape(str)}</code></pre>`
  33. }
  34. }
  35. })
  36. if (this.config.underline) {
  37. mkdown.use(underline)
  38. }
  39. mkdown.use(mdAttrs, {
  40. allowedAttributes: ['id', 'class', 'target']
  41. })
  42. mkdown.use(mdDecorate)
  43. for (let child of this.children) {
  44. const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
  45. await renderer.init(mkdown, child.config)
  46. }
  47. return mkdown.render(this.input)
  48. }
  49. }