Skip to content

Commit 6d1021e

Browse files
committed
Add improved docs
1 parent 437c1cc commit 6d1021e

File tree

6 files changed

+264
-157
lines changed

6 files changed

+264
-157
lines changed

index.d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type {Literal} from 'hast'
22

33
// Expose types.
4-
export type {Options, Handler, Handlers, H} from './lib/index.js'
4+
export type {H, Handler, Handlers, Options} from './lib/index.js'
55

66
// Expose JS API.
77
export {one, all} from './lib/traverse.js'
8-
export {defaultHandlers, toHast} from './lib/index.js'
8+
export {handlers as defaultHandlers} from './lib/handlers/index.js'
9+
export {toHast} from './lib/index.js'
910

1011
// Expose node type.
1112
/**

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// Note: types exposed from `index.d.ts`.
22
export {one, all} from './lib/traverse.js'
3-
export {defaultHandlers, toHast} from './lib/index.js'
3+
export {handlers as defaultHandlers} from './lib/handlers/index.js'
4+
export {toHast} from './lib/index.js'

lib/handlers/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import {tableCell} from './table-cell.js'
2323
import {text} from './text.js'
2424
import {thematicBreak} from './thematic-break.js'
2525

26+
/**
27+
* Default handlers for nodes.
28+
*/
2629
export const handlers = {
2730
blockquote,
2831
break: hardBreak,

lib/index.js

+84-29
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
* @property {HastProperties} footnoteLabelProperties
9797
* Properties on the HTML tag used for the footnote label.
9898
* @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.
100100
* @property {(identifier: string) => MdastDefinition | null} definition
101101
* Definition cache.
102102
* @property {Record<string, MdastFootnoteDefinition>} footnoteById
@@ -121,38 +121,26 @@
121121
* @typedef Options
122122
* Configuration (optional).
123123
* @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.
125125
* @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).
137131
* @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).
144133
* @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.
152139
* @property {Handlers | null | undefined} [handlers]
153-
* Object mapping mdast nodes to functions handling them
140+
* Extra handlers for nodes.
154141
* @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).
156144
* @property {Handler | null | undefined} [unknownHandler]
157145
* Handler for all unknown nodes.
158146
*
@@ -385,7 +373,74 @@ function applyData(origin, node) {
385373
}
386374

387375
/**
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`.
389444
*
390445
* @param {MdastNodes} tree
391446
* mdast tree.

lib/traverse.js

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export function one(h, node, parent) {
4242
}
4343

4444
if (h.passThrough && h.passThrough.includes(type)) {
45+
// To do: next major: deep clone.
4546
// @ts-expect-error: types of passed through nodes are expected to be added manually.
4647
return 'children' in node ? {...node, children: all(h, node)} : node
4748
}

0 commit comments

Comments
 (0)