|
96 | 96 | * @property {HastProperties} footnoteLabelProperties
|
97 | 97 | * Properties on the HTML tag used for the footnote label.
|
98 | 98 | * @property {string} footnoteBackLabel
|
99 |
| - * Label to use to go back to a footnote call from the footnote section. |
| 99 | + * Label to use from backreferences back to their footnote call. |
100 | 100 | * @property {(identifier: string) => MdastDefinition | null} definition
|
101 | 101 | * Definition cache.
|
102 | 102 | * @property {Record<string, MdastFootnoteDefinition>} footnoteById
|
|
121 | 121 | * @typedef Options
|
122 | 122 | * Configuration (optional).
|
123 | 123 | * @property {boolean | null | undefined} [allowDangerousHtml=false]
|
124 |
| - * Whether to allow `html` nodes and inject them as `raw` HTML. |
| 124 | + * Whether to persist raw HTML in markdown in the hast tree. |
125 | 125 | * @property {string | null | undefined} [clobberPrefix='user-content-']
|
126 |
| - * Prefix to use before the `id` attribute to prevent it from *clobbering*. |
127 |
| - * attributes. |
128 |
| - * DOM clobbering is this: |
129 |
| - * |
130 |
| - * ```html |
131 |
| - * <p id=x></p> |
132 |
| - * <script>alert(x)</script> |
133 |
| - * ``` |
134 |
| - * |
135 |
| - * Elements by their ID are made available in browsers on the `window` object. |
136 |
| - * Using a prefix prevents this from being a problem. |
| 126 | + * Prefix to use before the `id` attribute on footnotes to prevent it from |
| 127 | + * *clobbering*. |
| 128 | + * @property {string | null | undefined} [footnoteBackLabel='Back to content'] |
| 129 | + * Label to use from backreferences back to their footnote call (affects |
| 130 | + * screen readers). |
137 | 131 | * @property {string | null | undefined} [footnoteLabel='Footnotes']
|
138 |
| - * Label to use for the footnotes section. |
139 |
| - * Affects screen reader users. |
140 |
| - * Change it if you’re authoring in a different language. |
141 |
| - * @property {string | null | undefined} [footnoteLabelTagName='h2'] |
142 |
| - * HTML tag to use for the footnote label. |
143 |
| - * Can be changed to match your document structure and play well with your choice of css. |
| 132 | + * Label to use for the footnotes section (affects screen readers). |
144 | 133 | * @property {HastProperties | null | undefined} [footnoteLabelProperties={className: ['sr-only']}]
|
145 |
| - * Properties to use on the footnote label. |
146 |
| - * A 'sr-only' class is added by default to hide this from sighted users. |
147 |
| - * Change it to make the label visible, or add classes for other purposes. |
148 |
| - * @property {string | null | undefined} [footnoteBackLabel='Back to content'] |
149 |
| - * Label to use from backreferences back to their footnote call. |
150 |
| - * Affects screen reader users. |
151 |
| - * Change it if you’re authoring in a different language. |
| 134 | + * Properties to use on the footnote label (note that `id: 'footnote-label'` |
| 135 | + * is always added as footnote calls use it with `aria-describedby` to |
| 136 | + * provide an accessible label). |
| 137 | + * @property {string | null | undefined} [footnoteLabelTagName='h2'] |
| 138 | + * Tag name to use for the footnote label. |
152 | 139 | * @property {Handlers | null | undefined} [handlers]
|
153 |
| - * Object mapping mdast nodes to functions handling them |
| 140 | + * Extra handlers for nodes. |
154 | 141 | * @property {Array<string> | null | undefined} [passThrough]
|
155 |
| - * List of custom mdast node types to pass through (keep) in hast |
| 142 | + * List of custom mdast node types to pass through (keep) in hast (note that |
| 143 | + * the node itself is passed, but eventual children are transformed). |
156 | 144 | * @property {Handler | null | undefined} [unknownHandler]
|
157 | 145 | * Handler for all unknown nodes.
|
158 | 146 | *
|
@@ -385,7 +373,74 @@ function applyData(origin, node) {
|
385 | 373 | }
|
386 | 374 |
|
387 | 375 | /**
|
388 |
| - * Turn an mdast tree into a hast node. |
| 376 | + * Transform mdast to hast. |
| 377 | + * |
| 378 | + * ##### Notes |
| 379 | + * |
| 380 | + * ###### HTML |
| 381 | + * |
| 382 | + * Raw HTML is available in mdast as `html` nodes and can be embedded in hast |
| 383 | + * as semistandard `raw` nodes. |
| 384 | + * Most utilities ignore `raw` nodes but two notable ones don’t: |
| 385 | + * |
| 386 | + * * `hast-util-to-html` also has an option `allowDangerousHtml` which will |
| 387 | + * output the raw HTML. |
| 388 | + * This is typically discouraged as noted by the option name but is useful |
| 389 | + * if you completely trust authors |
| 390 | + * * `hast-util-raw` can handle the raw embedded HTML strings by parsing them |
| 391 | + * into standard hast nodes (`element`, `text`, etc). |
| 392 | + * This is a heavy task as it needs a full HTML parser, but it is the only |
| 393 | + * way to support untrusted content |
| 394 | + * |
| 395 | + * ###### Footnotes |
| 396 | + * |
| 397 | + * Many options supported here relate to footnotes. |
| 398 | + * Footnotes are not specified by CommonMark, which we follow by default. |
| 399 | + * They are supported by GitHub, so footnotes can be enabled in markdown with |
| 400 | + * `mdast-util-gfm`. |
| 401 | + * |
| 402 | + * The options `footnoteBackLabel` and `footnoteLabel` define natural language |
| 403 | + * that explains footnotes, which is hidden for sighted users but shown to |
| 404 | + * assistive technology. |
| 405 | + * When your page is not in English, you must define translated values. |
| 406 | + * |
| 407 | + * Back references use ARIA attributes, but the section label itself uses a |
| 408 | + * heading that is hidden with an `sr-only` class. |
| 409 | + * To show it to sighted users, define different attributes in |
| 410 | + * `footnoteLabelProperties`. |
| 411 | + * |
| 412 | + * ###### Clobbering |
| 413 | + * |
| 414 | + * Footnotes introduces a problem, as it links footnote calls to footnote |
| 415 | + * definitions on the page through `id` attributes generated from user content, |
| 416 | + * which results in DOM clobbering. |
| 417 | + * |
| 418 | + * DOM clobbering is this: |
| 419 | + * |
| 420 | + * ```html |
| 421 | + * <p id=x></p> |
| 422 | + * <script>alert(x) // `x` now refers to the DOM `p#x` element</script> |
| 423 | + * ``` |
| 424 | + * |
| 425 | + * Elements by their ID are made available by browsers on the `window` object, |
| 426 | + * which is a security risk. |
| 427 | + * Using a prefix solves this problem. |
| 428 | + * |
| 429 | + * More information on how to handle clobbering and the prefix is explained in |
| 430 | + * Example: headings (DOM clobbering) in `rehype-sanitize`. |
| 431 | + * |
| 432 | + * ###### Unknown nodes |
| 433 | + * |
| 434 | + * Unknown nodes are nodes with a type that isn’t in `handlers` or `passThrough`. |
| 435 | + * The default behavior for unknown nodes is: |
| 436 | + * |
| 437 | + * * when the node has a `value` (and doesn’t have `data.hName`, |
| 438 | + * `data.hProperties`, or `data.hChildren`, see later), create a hast `text` |
| 439 | + * node |
| 440 | + * * otherwise, create a `<div>` element (which could be changed with |
| 441 | + * `data.hName`), with its children mapped from mdast to hast as well |
| 442 | + * |
| 443 | + * This behavior can be changed by passing an `unknownHandler`. |
389 | 444 | *
|
390 | 445 | * @param {MdastNodes} tree
|
391 | 446 | * mdast tree.
|
|
0 commit comments