Skip to content

Commit 1b04a5a

Browse files
committed
Add improved docs
1 parent 586ae01 commit 1b04a5a

File tree

5 files changed

+227
-92
lines changed

5 files changed

+227
-92
lines changed

lib/handlers/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,23 @@ import {text} from './text.js'
3939
import {textarea} from './textarea.js'
4040
import {wbr} from './wbr.js'
4141

42+
/**
43+
* Default handlers for nodes.
44+
*
45+
* Each key is a node type, each value is a `NodeHandler`.
46+
*/
4247
export const nodeHandlers = {
4348
root,
4449
text,
4550
comment,
4651
doctype: ignore
4752
}
4853

54+
/**
55+
* Default handlers for elements.
56+
*
57+
* Each key is an element name, each value is a `Handler`.
58+
*/
4959
export const handlers = {
5060
applet: ignore,
5161
area: ignore,

lib/state.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* @typedef {import('mdast').Content} MdastContent
77
* @typedef {import('mdast').BlockContent} MdastBlockContent
88
* @typedef {import('mdast').DefinitionContent} MdastDefinitionContent
9-
* @typedef {import('mdast').PhrasingContent} MdastPhrasingContent
109
* @typedef {import('./types.js').Options} Options
1110
* @typedef {import('./types.js').Handle} Handle
1211
* @typedef {import('./types.js').NodeHandle} NodeHandle
@@ -21,12 +20,14 @@
2120
*
2221
* @typedef State
2322
* Info passed around about the current state.
24-
* @property {PatchPosition} patch
23+
* @property {Patch} patch
2524
* Copy a node’s positional info.
25+
* @property {One} one
26+
* Transform a hast node to mdast.
2627
* @property {All} all
2728
* Transform the children of a hast parent to mdast.
2829
* @property {ToFlow} toFlow
29-
* Transform the children of a hast parent to mdast flow content.
30+
* Transform a list of mdast nodes to flow.
3031
* @property {<ChildType extends MdastNode, ParentType extends MdastParent & {'children': Array<ChildType>}>(nodes: Array<MdastContent>, build: (() => ParentType)) => Array<ParentType>} toSpecificContent
3132
* Turn arbitrary content into a list of a particular node type.
3233
*
@@ -35,8 +36,6 @@
3536
* in this example, when non-items are found, they will be queued, and
3637
* inserted into an adjacent item.
3738
* When no actual items exist, one will be made with `build`.
38-
* @property {One} one
39-
* Transform a hast node to mdast.
4039
* @property {Resolve} resolve
4140
* Resolve a URL relative to a base.
4241
* @property {Options} options
@@ -56,11 +55,11 @@
5655
* @property {number} qNesting
5756
* Non-negative finite integer representing how deep we’re in `<q>`s.
5857
*
59-
* @callback PatchPosition
58+
* @callback Patch
6059
* Copy a node’s positional info.
61-
* @param {Node} origin
60+
* @param {Node} from
6261
* hast node to copy from.
63-
* @param {MdastNode} node
62+
* @param {MdastNode} to
6463
* mdast node to copy into.
6564
* @returns {void}
6665
* Nothing.
@@ -73,7 +72,7 @@
7372
* mdast children.
7473
*
7574
* @callback ToFlow
76-
* Transform the children of a hast parent to mdast, as flow content.
75+
* Transform a list of mdast nodes to flow.
7776
* @param {Array<MdastContent>} nodes
7877
* mdast nodes.
7978
* @returns {Array<MdastFlowContent>}
@@ -233,7 +232,7 @@ function all(parent) {
233232
}
234233

235234
/**
236-
* Transform the children of a hast parent to mdast.
235+
* Transform a list of mdast nodes to flow.
237236
*
238237
* @this {State}
239238
* Info passed around about the current state.
@@ -247,29 +246,7 @@ function toFlow(nodes) {
247246
}
248247

249248
/**
250-
* @this {State}
251-
* Info passed around about the current state.
252-
* @param {string | null | undefined} url
253-
* Possible URL value.
254-
* @returns {string}
255-
* URL, resolved to a `base` element, if any.
256-
*/
257-
function resolve(url) {
258-
const base = this.frozenBaseUrl
259-
260-
if (url === null || url === undefined) {
261-
return ''
262-
}
263-
264-
if (base) {
265-
return String(new URL(url, base))
266-
}
267-
268-
return url
269-
}
270-
271-
/**
272-
* Turn arbitrary content into a list of a particular node type.
249+
* Turn arbitrary content into a particular node type.
273250
*
274251
* This is useful for example for lists, which must have list items as content.
275252
* in this example, when non-items are found, they will be queued, and
@@ -335,3 +312,25 @@ function toSpecificContent(nodes, build) {
335312
return node.type === reference.type
336313
}
337314
}
315+
316+
/**
317+
* @this {State}
318+
* Info passed around about the current state.
319+
* @param {string | null | undefined} url
320+
* Possible URL value.
321+
* @returns {string}
322+
* URL, resolved to a `base` element, if any.
323+
*/
324+
function resolve(url) {
325+
const base = this.frozenBaseUrl
326+
327+
if (url === null || url === undefined) {
328+
return ''
329+
}
330+
331+
if (base) {
332+
return String(new URL(url, base))
333+
}
334+
335+
return url
336+
}

lib/types.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,27 @@
1313
* @typedef {Content | Root} Node
1414
* @typedef {Extract<Node, UnistParent>} Parent
1515
*
16-
* @typedef {(state: State, node: any, parent: Parent | undefined) => MdastNode | Array<MdastNode> | void} NodeHandle
16+
* @callback Handle
17+
* Handle a particular element.
18+
* @param {State} state
19+
* Info passed around about the current state.
20+
* @param {Element} element
21+
* Element to transform.
22+
* @param {Parent | undefined} parent
23+
* Parent of `element`.
24+
* @returns {MdastNode | Array<MdastNode> | void}
25+
* mdast node or nodes.
1726
*
18-
* @typedef {(state: State, node: Element, parent: Parent | undefined) => MdastNode | Array<MdastNode> | void} Handle
27+
* @callback NodeHandle
28+
* Handle a particular node.
29+
* @param {State} state
30+
* Info passed around about the current state.
31+
* @param {any} node
32+
* Node to transform.
33+
* @param {Parent | undefined} parent
34+
* Parent of `node`.
35+
* @returns {MdastNode | Array<MdastNode> | void}
36+
* mdast node or nodes.
1937
*
2038
* @typedef Options
2139
* Configuration.
@@ -52,8 +70,12 @@
5270
* *and* some non-phrasing nodes.
5371
* @property {Record<string, Handle | null | undefined> | null | undefined} [handlers]
5472
* Object mapping tag names to functions handling the corresponding elements.
73+
*
74+
* Merged into the defaults.
5575
* @property {Record<string, NodeHandle | null | undefined> | null | undefined} [nodeHandlers]
5676
* Object mapping node types to functions handling the corresponding nodes.
77+
*
78+
* Merged into the defaults.
5779
*/
5880

5981
export {}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@types/unist": "^2.0.0",
4343
"extend": "^3.0.0",
4444
"hast-util-phrasing": "^2.0.0",
45+
"hast-util-to-html": "^8.0.4",
4546
"hast-util-to-text": "^3.0.0",
4647
"hast-util-whitespace": "^2.0.0",
4748
"mdast-util-phrasing": "^3.0.0",

0 commit comments

Comments
 (0)