You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

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