Skip to content

Commit 35db2db

Browse files
committed
Replace h with state
* this removes `h` as a function with different complex overloads, it is now recommended to build your replacement nodes as literal objects * move `document`, `checked`, `quotes`, and `unchecked` fields on `state` (what used to be `h`) with an `options` object, so that all user options can more easily be accessed
1 parent ddc0cd6 commit 35db2db

40 files changed

+270
-317
lines changed

index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/**
2-
* @typedef {import('./lib/types.js').Options} Options
3-
* @typedef {import('./lib/types.js').Context} Context
4-
* @typedef {import('./lib/types.js').H} H
2+
* @typedef {import('./lib/types.js').State} State
53
* @typedef {import('./lib/types.js').Handle} Handle
4+
* @typedef {import('./lib/types.js').Options} Options
65
*/
76

87
export {one, all, defaultHandlers, toMdast} from './lib/index.js'

lib/all.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @typedef {import('hast').Root} Root
33
* @typedef {import('hast').Content} Content
44
* @typedef {import('mdast').Content} MdastContent
5-
* @typedef {import('./types.js').H} H
5+
* @typedef {import('./types.js').State} State
66
*/
77

88
/**
@@ -13,22 +13,22 @@
1313
import {one} from './one.js'
1414

1515
/**
16-
* @param {H} h
17-
* Context.
16+
* @param {State} state
17+
* State.
1818
* @param {Parent} parent
1919
* Parent to transform.
2020
* @returns {Array<MdastContent>}
2121
* mdast nodes.
2222
*/
23-
export function all(h, parent) {
23+
export function all(state, parent) {
2424
const children = parent.children || []
2525
/** @type {Array<MdastContent>} */
2626
const results = []
2727
let index = -1
2828

2929
while (++index < children.length) {
3030
const child = children[index]
31-
const result = one(h, child, parent)
31+
const result = one(state, child, parent)
3232

3333
if (Array.isArray(result)) {
3434
// @ts-expect-error: assume no `root`.

lib/handlers/a.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
/**
22
* @typedef {import('mdast').Link} Link
33
* @typedef {import('hast').Element} Element
4-
* @typedef {import('../types.js').H} H
4+
* @typedef {import('../types.js').State} State
55
*/
66

77
import {all} from '../all.js'
88
import {resolve} from '../util/resolve.js'
99

1010
/**
11-
* @param {H} h
12-
* Context.
11+
* @param {State} state
12+
* State.
1313
* @param {Element} node
1414
* hast element to transform.
1515
* @returns {Link}
1616
* mdast node.
1717
*/
18-
export function a(h, node) {
18+
export function a(state, node) {
1919
const properties = node.properties || {}
2020

2121
/** @type {Link} */
2222
const result = {
2323
type: 'link',
24-
url: resolve(h, String(properties.href || '') || null),
24+
url: resolve(state, String(properties.href || '') || null),
2525
title: properties.title ? String(properties.title) : null,
2626
// @ts-expect-error: assume valid children.
27-
children: all(h, node)
27+
children: all(state, node)
2828
}
2929

3030
// To do: clean.

lib/handlers/base.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/**
22
* @typedef {import('hast').Element} Element
3-
* @typedef {import('../types.js').H} H
3+
* @typedef {import('../types.js').State} State
44
*/
55

66
/**
7-
* @param {H} h
8-
* Context.
7+
* @param {State} state
8+
* State.
99
* @param {Element} node
1010
* hast element to transform.
1111
* @returns {void}
1212
* Nothing.
1313
*/
14-
export function base(h, node) {
15-
if (!h.baseFound) {
16-
h.frozenBaseUrl =
14+
export function base(state, node) {
15+
if (!state.baseFound) {
16+
state.frozenBaseUrl =
1717
String((node.properties && node.properties.href) || '') || undefined
18-
h.baseFound = true
18+
state.baseFound = true
1919
}
2020
}

lib/handlers/blockquote.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Blockquote} Blockquote
4-
* @typedef {import('../types.js').H} H
4+
* @typedef {import('../types.js').State} State
55
*/
66

77
import {wrapChildren} from '../util/wrap-children.js'
88

99
/**
10-
* @param {H} h
11-
* Context.
10+
* @param {State} state
11+
* State.
1212
* @param {Element} node
1313
* hast element to transform.
1414
* @returns {Blockquote}
1515
* mdast node.
1616
*/
17-
export function blockquote(h, node) {
17+
export function blockquote(state, node) {
1818
/** @type {Blockquote} */
1919
const result = {
2020
type: 'blockquote',
21-
children: wrapChildren(h, node)
21+
children: wrapChildren(state, node)
2222
}
2323

2424
// To do: clean.

lib/handlers/br.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Break} Break
44
* @typedef {import('mdast').Text} Text
5-
* @typedef {import('../types.js').H} H
5+
* @typedef {import('../types.js').State} State
66
*/
77

88
/**
9-
* @param {H} h
10-
* Context.
9+
* @param {State} state
10+
* State.
1111
* @param {Element} node
1212
* hast element to transform.
1313
* @returns {Break | Text}
1414
* mdast node.
1515
*/
16-
export function br(h, node) {
16+
export function br(state, node) {
1717
/** @type {Break | Text} */
18-
const result = h.wrapText ? {type: 'break'} : {type: 'text', value: ' '}
18+
const result = state.wrapText ? {type: 'break'} : {type: 'text', value: ' '}
1919

2020
// To do: clean.
2121
if (node.position) {

lib/handlers/code.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Code} Code
4-
* @typedef {import('../types.js').H} H
4+
* @typedef {import('../types.js').State} State
55
*/
66

77
import {toText} from 'hast-util-to-text'
@@ -11,14 +11,14 @@ import {wrapText} from '../util/wrap-text.js'
1111
const prefix = 'language-'
1212

1313
/**
14-
* @param {H} h
15-
* Context.
14+
* @param {State} state
15+
* State.
1616
* @param {Element} node
1717
* hast element to transform.
1818
* @returns {Code}
1919
* mdast node.
2020
*/
21-
export function code(h, node) {
21+
export function code(state, node) {
2222
const children = node.children
2323
let index = -1
2424
/** @type {Array<string | number> | undefined} */
@@ -59,7 +59,7 @@ export function code(h, node) {
5959
type: 'code',
6060
lang: lang || null,
6161
meta: null,
62-
value: trimTrailingLines(wrapText(h, toText(node)))
62+
value: trimTrailingLines(wrapText(state, toText(node)))
6363
}
6464

6565
// To do: clean.

lib/handlers/comment.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
/**
22
* @typedef {import('hast').Comment} Comment
33
* @typedef {import('mdast').HTML} HTML
4-
* @typedef {import('../types.js').H} H
4+
* @typedef {import('../types.js').State} State
55
*/
66

77
import {wrapText} from '../util/wrap-text.js'
88

99
/**
10-
* @param {H} h
11-
* Context.
10+
* @param {State} state
11+
* State.
1212
* @param {Comment} node
1313
* hast element to transform.
1414
* @returns {HTML}
1515
* mdast node.
1616
*/
17-
export function comment(h, node) {
17+
export function comment(state, node) {
1818
/** @type {HTML} */
19-
const result = {type: 'html', value: '<!--' + wrapText(h, node.value) + '-->'}
19+
const result = {
20+
type: 'html',
21+
value: '<!--' + wrapText(state, node.value) + '-->'
22+
}
2023

2124
// To do: clean.
2225
if (node.position) {

lib/handlers/del.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Delete} Delete
4-
* @typedef {import('../types.js').H} H
4+
* @typedef {import('../types.js').State} State
55
*/
66

77
import {all} from '../all.js'
88

99
/**
10-
* @param {H} h
11-
* Context.
10+
* @param {State} state
11+
* State.
1212
* @param {Element} node
1313
* hast element to transform.
1414
* @returns {Delete}
1515
* mdast node.
1616
*/
17-
export function del(h, node) {
17+
export function del(state, node) {
1818
/** @type {Delete} */
1919
const result = {
2020
type: 'delete',
2121
// @ts-expect-error: assume valid children.
22-
children: all(h, node)
22+
children: all(state, node)
2323
}
2424

2525
// To do: clean.

lib/handlers/dl.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @typedef {import('mdast').BlockContent} BlockContent
66
* @typedef {import('mdast').DefinitionContent} DefinitionContent
77
* @typedef {import('mdast').ListContent} ListContent
8-
* @typedef {import('../types.js').H} H
8+
* @typedef {import('../types.js').State} State
99
*
1010
* @typedef Group
1111
* Title/definition group.
@@ -19,14 +19,14 @@ import {listItemsSpread} from '../util/list-items-spread.js'
1919
import {wrapListItems} from '../util/wrap-list-items.js'
2020

2121
/**
22-
* @param {H} h
23-
* Context.
22+
* @param {State} state
23+
* State.
2424
* @param {Element} node
2525
* hast element to transform.
2626
* @returns {List | undefined}
2727
* mdast node.
2828
*/
29-
export function dl(h, node) {
29+
export function dl(state, node) {
3030
/** @type {Array<ElementContent>} */
3131
const clean = []
3232
/** @type {Array<Group>} */
@@ -79,8 +79,8 @@ export function dl(h, node) {
7979

8080
while (++index < groups.length) {
8181
const result = [
82-
...handle(h, groups[index].titles),
83-
...handle(h, groups[index].definitions)
82+
...handle(state, groups[index].titles),
83+
...handle(state, groups[index].definitions)
8484
]
8585

8686
if (result.length > 0) {
@@ -114,15 +114,15 @@ export function dl(h, node) {
114114
}
115115

116116
/**
117-
* @param {H} h
118-
* Context.
117+
* @param {State} state
118+
* State.
119119
* @param {Array<ElementContent>} children
120120
* hast element children to transform.
121121
* @returns {Array<BlockContent | DefinitionContent>}
122122
* mdast nodes.
123123
*/
124-
function handle(h, children) {
125-
const nodes = wrapListItems(h, {type: 'element', tagName: 'x', children})
124+
function handle(state, children) {
125+
const nodes = wrapListItems(state, {type: 'element', tagName: 'x', children})
126126

127127
if (nodes.length === 0) {
128128
return []

lib/handlers/em.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Emphasis} Emphasis
4-
* @typedef {import('../types.js').H} H
4+
* @typedef {import('../types.js').State} State
55
*/
66

77
import {all} from '../all.js'
88

99
/**
10-
* @param {H} h
11-
* Context.
10+
* @param {State} state
11+
* State.
1212
* @param {Element} node
1313
* hast element to transform.
1414
* @returns {Emphasis}
1515
* mdast node.
1616
*/
17-
export function em(h, node) {
17+
export function em(state, node) {
1818
/** @type {Emphasis} */
1919
const result = {
2020
type: 'emphasis',
2121
// @ts-expect-error: assume valid children.
22-
children: all(h, node)
22+
children: all(state, node)
2323
}
2424

2525
// To do: clean.

lib/handlers/heading.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Heading} Heading
4-
* @typedef {import('../types.js').H} H
4+
* @typedef {import('../types.js').State} State
55
*/
66

77
import {all} from '../all.js'
88

99
/**
10-
* @param {H} h
11-
* Context.
10+
* @param {State} state
11+
* State.
1212
* @param {Element} node
1313
* hast element to transform.
1414
* @returns {Heading}
1515
* mdast node.
1616
*/
17-
export function heading(h, node) {
17+
export function heading(state, node) {
1818
/* c8 ignore next */
1919
const depth = Number(node.tagName.charAt(1)) || 1
20-
const wrap = h.wrapText
20+
const wrap = state.wrapText
2121

2222
// To do: next major.
2323
// To do: `mdast-util-to-markdown` should support breaks currently.
24-
h.wrapText = false
25-
const children = all(h, node)
26-
h.wrapText = wrap
24+
state.wrapText = false
25+
const children = all(state, node)
26+
state.wrapText = wrap
2727

2828
/** @type {Heading} */
2929
const result = {

0 commit comments

Comments
 (0)