Skip to content

Commit

Permalink
Add improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 12, 2023
1 parent 586ae01 commit 1b04a5a
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 92 deletions.
10 changes: 10 additions & 0 deletions lib/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ import {text} from './text.js'
import {textarea} from './textarea.js'
import {wbr} from './wbr.js'

/**
* Default handlers for nodes.
*
* Each key is a node type, each value is a `NodeHandler`.
*/
export const nodeHandlers = {
root,
text,
comment,
doctype: ignore
}

/**
* Default handlers for elements.
*
* Each key is an element name, each value is a `Handler`.
*/
export const handlers = {
applet: ignore,
area: ignore,
Expand Down
65 changes: 32 additions & 33 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* @typedef {import('mdast').Content} MdastContent
* @typedef {import('mdast').BlockContent} MdastBlockContent
* @typedef {import('mdast').DefinitionContent} MdastDefinitionContent
* @typedef {import('mdast').PhrasingContent} MdastPhrasingContent
* @typedef {import('./types.js').Options} Options
* @typedef {import('./types.js').Handle} Handle
* @typedef {import('./types.js').NodeHandle} NodeHandle
Expand All @@ -21,12 +20,14 @@
*
* @typedef State
* Info passed around about the current state.
* @property {PatchPosition} patch
* @property {Patch} patch
* Copy a node’s positional info.
* @property {One} one
* Transform a hast node to mdast.
* @property {All} all
* Transform the children of a hast parent to mdast.
* @property {ToFlow} toFlow
* Transform the children of a hast parent to mdast flow content.
* Transform a list of mdast nodes to flow.
* @property {<ChildType extends MdastNode, ParentType extends MdastParent & {'children': Array<ChildType>}>(nodes: Array<MdastContent>, build: (() => ParentType)) => Array<ParentType>} toSpecificContent
* Turn arbitrary content into a list of a particular node type.
*
Expand All @@ -35,8 +36,6 @@
* in this example, when non-items are found, they will be queued, and
* inserted into an adjacent item.
* When no actual items exist, one will be made with `build`.
* @property {One} one
* Transform a hast node to mdast.
* @property {Resolve} resolve
* Resolve a URL relative to a base.
* @property {Options} options
Expand All @@ -56,11 +55,11 @@
* @property {number} qNesting
* Non-negative finite integer representing how deep we’re in `<q>`s.
*
* @callback PatchPosition
* @callback Patch
* Copy a node’s positional info.
* @param {Node} origin
* @param {Node} from
* hast node to copy from.
* @param {MdastNode} node
* @param {MdastNode} to
* mdast node to copy into.
* @returns {void}
* Nothing.
Expand All @@ -73,7 +72,7 @@
* mdast children.
*
* @callback ToFlow
* Transform the children of a hast parent to mdast, as flow content.
* Transform a list of mdast nodes to flow.
* @param {Array<MdastContent>} nodes
* mdast nodes.
* @returns {Array<MdastFlowContent>}
Expand Down Expand Up @@ -233,7 +232,7 @@ function all(parent) {
}

/**
* Transform the children of a hast parent to mdast.
* Transform a list of mdast nodes to flow.
*
* @this {State}
* Info passed around about the current state.
Expand All @@ -247,29 +246,7 @@ function toFlow(nodes) {
}

/**
* @this {State}
* Info passed around about the current state.
* @param {string | null | undefined} url
* Possible URL value.
* @returns {string}
* URL, resolved to a `base` element, if any.
*/
function resolve(url) {
const base = this.frozenBaseUrl

if (url === null || url === undefined) {
return ''
}

if (base) {
return String(new URL(url, base))
}

return url
}

/**
* Turn arbitrary content into a list of a particular node type.
* Turn arbitrary content into a particular node type.
*
* This is useful for example for lists, which must have list items as content.
* in this example, when non-items are found, they will be queued, and
Expand Down Expand Up @@ -335,3 +312,25 @@ function toSpecificContent(nodes, build) {
return node.type === reference.type
}
}

/**
* @this {State}
* Info passed around about the current state.
* @param {string | null | undefined} url
* Possible URL value.
* @returns {string}
* URL, resolved to a `base` element, if any.
*/
function resolve(url) {
const base = this.frozenBaseUrl

if (url === null || url === undefined) {
return ''
}

if (base) {
return String(new URL(url, base))
}

return url
}
26 changes: 24 additions & 2 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,27 @@
* @typedef {Content | Root} Node
* @typedef {Extract<Node, UnistParent>} Parent
*
* @typedef {(state: State, node: any, parent: Parent | undefined) => MdastNode | Array<MdastNode> | void} NodeHandle
* @callback Handle
* Handle a particular element.
* @param {State} state
* Info passed around about the current state.
* @param {Element} element
* Element to transform.
* @param {Parent | undefined} parent
* Parent of `element`.
* @returns {MdastNode | Array<MdastNode> | void}
* mdast node or nodes.
*
* @typedef {(state: State, node: Element, parent: Parent | undefined) => MdastNode | Array<MdastNode> | void} Handle
* @callback NodeHandle
* Handle a particular node.
* @param {State} state
* Info passed around about the current state.
* @param {any} node
* Node to transform.
* @param {Parent | undefined} parent
* Parent of `node`.
* @returns {MdastNode | Array<MdastNode> | void}
* mdast node or nodes.
*
* @typedef Options
* Configuration.
Expand Down Expand Up @@ -52,8 +70,12 @@
* *and* some non-phrasing nodes.
* @property {Record<string, Handle | null | undefined> | null | undefined} [handlers]
* Object mapping tag names to functions handling the corresponding elements.
*
* Merged into the defaults.
* @property {Record<string, NodeHandle | null | undefined> | null | undefined} [nodeHandlers]
* Object mapping node types to functions handling the corresponding nodes.
*
* Merged into the defaults.
*/

export {}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@types/unist": "^2.0.0",
"extend": "^3.0.0",
"hast-util-phrasing": "^2.0.0",
"hast-util-to-html": "^8.0.4",
"hast-util-to-text": "^3.0.0",
"hast-util-whitespace": "^2.0.0",
"mdast-util-phrasing": "^3.0.0",
Expand Down

0 comments on commit 1b04a5a

Please sign in to comment.