index.js 947 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict'
  2. var xtend = require('xtend')
  3. var toHAST = require('mdast-util-to-hast')
  4. var toHTML = require('hast-util-to-html')
  5. var sanitize = require('hast-util-sanitize')
  6. module.exports = plugin
  7. function plugin(options) {
  8. var settings = options || {}
  9. var clean = settings.sanitize
  10. var schema = clean && typeof clean === 'object' ? clean : null
  11. var handlers = settings.handlers || {}
  12. this.Compiler = compiler
  13. function compiler(node, file) {
  14. var root = node && node.type && node.type === 'root'
  15. var hast = toHAST(node, {allowDangerousHTML: !clean, handlers: handlers})
  16. var result
  17. if (file.extname) {
  18. file.extname = '.html'
  19. }
  20. if (clean) {
  21. hast = sanitize(hast, schema)
  22. }
  23. result = toHTML(hast, xtend(settings, {allowDangerousHTML: !clean}))
  24. /* Add a final newline. */
  25. if (root && result.charAt(result.length - 1) !== '\n') {
  26. result += '\n'
  27. }
  28. return result
  29. }
  30. }