index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const markdownIt = require('markdown-it')
  2. exports.name = 'markdown-it'
  3. exports.outputFormat = 'html'
  4. exports.inputFormats = ['markdown-it', 'markdown', 'md']
  5. exports.render = function (str, options) {
  6. options = Object.assign({}, options || {})
  7. // Copy render rules from options, and remove them from options, since
  8. // they're invalid.
  9. const renderRules = Object.assign({}, options.renderRules || {})
  10. delete options.renderRules
  11. const md = markdownIt(options)
  12. // Enable render rules.
  13. Object.assign(md.renderer.rules, renderRules);
  14. // Parse the plugins.
  15. (options.plugins || []).forEach(plugin => {
  16. if (!Array.isArray(plugin)) {
  17. plugin = [plugin]
  18. }
  19. if (typeof plugin[0] === 'string') {
  20. plugin[0] = require(plugin[0])
  21. }
  22. md.use(...plugin)
  23. });
  24. // Parse enable/disable rules.
  25. (options.enable || []).forEach(rule => {
  26. md.enable(...Array.isArray(rule) ? rule : [rule])
  27. });
  28. (options.disable || []).forEach(rule => {
  29. md.disable(...Array.isArray(rule) ? rule : [rule])
  30. })
  31. // Render the markdown.
  32. if (options.inline) {
  33. return md.renderInline(str)
  34. }
  35. return md.render(str)
  36. }