index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @import {Nodes as HastNodes} from 'hast'
  3. * @import {Nodes as MdastNodes} from 'mdast'
  4. * @import {Options} from './state.js'
  5. */
  6. import {ok as assert} from 'devlop'
  7. import {footer} from './footer.js'
  8. import {createState} from './state.js'
  9. /**
  10. * Transform mdast to hast.
  11. *
  12. * ##### Notes
  13. *
  14. * ###### HTML
  15. *
  16. * Raw HTML is available in mdast as `html` nodes and can be embedded in hast
  17. * as semistandard `raw` nodes.
  18. * Most utilities ignore `raw` nodes but two notable ones don’t:
  19. *
  20. * * `hast-util-to-html` also has an option `allowDangerousHtml` which will
  21. * output the raw HTML.
  22. * This is typically discouraged as noted by the option name but is useful
  23. * if you completely trust authors
  24. * * `hast-util-raw` can handle the raw embedded HTML strings by parsing them
  25. * into standard hast nodes (`element`, `text`, etc).
  26. * This is a heavy task as it needs a full HTML parser, but it is the only
  27. * way to support untrusted content
  28. *
  29. * ###### Footnotes
  30. *
  31. * Many options supported here relate to footnotes.
  32. * Footnotes are not specified by CommonMark, which we follow by default.
  33. * They are supported by GitHub, so footnotes can be enabled in markdown with
  34. * `mdast-util-gfm`.
  35. *
  36. * The options `footnoteBackLabel` and `footnoteLabel` define natural language
  37. * that explains footnotes, which is hidden for sighted users but shown to
  38. * assistive technology.
  39. * When your page is not in English, you must define translated values.
  40. *
  41. * Back references use ARIA attributes, but the section label itself uses a
  42. * heading that is hidden with an `sr-only` class.
  43. * To show it to sighted users, define different attributes in
  44. * `footnoteLabelProperties`.
  45. *
  46. * ###### Clobbering
  47. *
  48. * Footnotes introduces a problem, as it links footnote calls to footnote
  49. * definitions on the page through `id` attributes generated from user content,
  50. * which results in DOM clobbering.
  51. *
  52. * DOM clobbering is this:
  53. *
  54. * ```html
  55. * <p id=x></p>
  56. * <script>alert(x) // `x` now refers to the DOM `p#x` element</script>
  57. * ```
  58. *
  59. * Elements by their ID are made available by browsers on the `window` object,
  60. * which is a security risk.
  61. * Using a prefix solves this problem.
  62. *
  63. * More information on how to handle clobbering and the prefix is explained in
  64. * Example: headings (DOM clobbering) in `rehype-sanitize`.
  65. *
  66. * ###### Unknown nodes
  67. *
  68. * Unknown nodes are nodes with a type that isn’t in `handlers` or `passThrough`.
  69. * The default behavior for unknown nodes is:
  70. *
  71. * * when the node has a `value` (and doesn’t have `data.hName`,
  72. * `data.hProperties`, or `data.hChildren`, see later), create a hast `text`
  73. * node
  74. * * otherwise, create a `<div>` element (which could be changed with
  75. * `data.hName`), with its children mapped from mdast to hast as well
  76. *
  77. * This behavior can be changed by passing an `unknownHandler`.
  78. *
  79. * @param {MdastNodes} tree
  80. * mdast tree.
  81. * @param {Options | null | undefined} [options]
  82. * Configuration (optional).
  83. * @returns {HastNodes}
  84. * hast tree.
  85. */
  86. export function toHast(tree, options) {
  87. const state = createState(tree, options)
  88. const node = state.one(tree, undefined)
  89. const foot = footer(state)
  90. /** @type {HastNodes} */
  91. const result = Array.isArray(node)
  92. ? {type: 'root', children: node}
  93. : node || {type: 'root', children: []}
  94. if (foot) {
  95. // If there’s a footer, there were definitions, meaning block
  96. // content.
  97. // So `result` is a parent node.
  98. assert('children' in result)
  99. result.children.push({type: 'text', value: '\n'}, foot)
  100. }
  101. return result
  102. }