index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict'
  2. var html = require('property-information/html')
  3. var svg = require('property-information/svg')
  4. var voids = require('html-void-elements')
  5. var omission = require('./omission')
  6. var one = require('./one')
  7. module.exports = toHTML
  8. /* Characters. */
  9. var DQ = '"'
  10. var SQ = "'"
  11. /* Stringify the given HAST node. */
  12. function toHTML(node, options) {
  13. var settings = options || {}
  14. var quote = settings.quote || DQ
  15. var alternative = quote === DQ ? SQ : DQ
  16. var smart = settings.quoteSmart
  17. if (quote !== DQ && quote !== SQ) {
  18. throw new Error(
  19. 'Invalid quote `' + quote + '`, expected `' + SQ + '` or `' + DQ + '`'
  20. )
  21. }
  22. return one(
  23. {
  24. valid: settings.allowParseErrors ? 0 : 1,
  25. safe: settings.allowDangerousCharacters ? 0 : 1,
  26. schema: settings.space === 'svg' ? svg : html,
  27. omit: settings.omitOptionalTags && omission,
  28. quote: quote,
  29. alternative: smart ? alternative : null,
  30. unquoted: Boolean(settings.preferUnquoted),
  31. tight: settings.tightAttributes,
  32. tightDoctype: Boolean(settings.tightDoctype),
  33. tightLists: settings.tightCommaSeparatedLists,
  34. tightClose: settings.tightSelfClosing,
  35. collapseEmpty: settings.collapseEmptyAttributes,
  36. dangerous: settings.allowDangerousHTML,
  37. voids: settings.voids || voids.concat(),
  38. entities: settings.entities || {},
  39. close: settings.closeSelfClosing,
  40. closeEmpty: settings.closeEmptyElements
  41. },
  42. node
  43. )
  44. }