1
1
/**
2
- * @typedef {import('unist').Parent } UnistParent
3
2
* @typedef {import('mdast').Content } Content
4
3
* @typedef {import('mdast').Heading } Heading
5
4
* @typedef {import('mdast').Root } Root
5
+ * @typedef {import('unist').Parent } UnistParent
6
6
*/
7
7
8
+ // To do: use `Nodes`, `Parents` from `mdast` when released.
8
9
/**
9
10
* @typedef {Content | Root } Node
10
11
* @typedef {Extract<Node, UnistParent> } Parent
11
12
*/
12
13
13
14
/**
14
- * @callback TestFunction
15
- * Check if a node matches.
16
- * @param {string } value
17
- * Plain-text heading.
18
- * @param {Heading } node
19
- * Heading node.
20
- * @returns {boolean | null | undefined }
21
- * Whether this is the heading that is searched for.
22
- *
23
- * @typedef {string | RegExp | TestFunction } Test
24
- * Test for a heading.
25
- *
26
- * When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
27
- * when `RegExp`, wrapped in `(value) => expression.test(value)`
28
- *
29
- * @typedef Options
30
- * Configuration.
31
- * @property {Test } test
32
- * Test for a heading.
33
- * @property {boolean | null | undefined } [ignoreFinalDefinitions=false]
34
- * Ignore final definitions otherwise in the section.
35
- *
36
- * @typedef Info
37
- * Extra info.
38
- * @property {Parent } parent
39
- * Parent of the section.
40
- * @property {number } start
41
- * Index of `start` in `parent`.
42
- * @property {number | null } end
43
- * Index of `end` in `parent`.
44
- *
45
15
* @callback Handler
46
16
* Callback called when a section is found.
47
17
* @param {Heading } start
58
28
* If nothing is returned, nothing will be changed.
59
29
* If an array of nodes (can include `null` and `undefined`) is returned, the
60
30
* original section will be replaced by those nodes.
31
+ *
32
+ * @typedef Info
33
+ * Extra info.
34
+ * @property {Parent } parent
35
+ * Parent of the section.
36
+ * @property {number } start
37
+ * Index of `start` in `parent`.
38
+ * @property {number | null } end
39
+ * Index of `end` in `parent`.
40
+ *
41
+ * @typedef Options
42
+ * Configuration.
43
+ * @property {Test } test
44
+ * Test for a heading.
45
+ * @property {boolean | null | undefined } [ignoreFinalDefinitions=false]
46
+ * Ignore final definitions otherwise in the section (default: `false`).
47
+ *
48
+ * @typedef {RegExp | TestFunction | string } Test
49
+ * Test for a heading.
50
+ *
51
+ * When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
52
+ * when `RegExp`, wrapped in `(value) => expression.test(value)`
53
+ *
54
+ * @callback TestFunction
55
+ * Check if a node matches.
56
+ * @param {string } value
57
+ * Plain-text heading.
58
+ * @param {Heading } node
59
+ * Heading node.
60
+ * @returns {boolean | null | undefined | void }
61
+ * Whether this is the heading that is searched for.
61
62
*/
62
63
64
+ import { ok as assert } from 'devlop'
63
65
import { toString } from 'mdast-util-to-string'
64
66
65
67
// To do: next major: remove `null` in API output.
@@ -78,16 +80,17 @@ import {toString} from 'mdast-util-to-string'
78
80
*
79
81
* @param {Node } tree
80
82
* Tree to change.
81
- * @param {Test | Options } options
83
+ * @param {Options | Test } options
82
84
* Configuration.
83
85
* @param {Handler } handler
84
86
* Handle a section.
85
- * @returns {void }
87
+ * @returns {undefined }
86
88
* Nothing.
87
89
*/
88
90
// eslint-disable-next-line complexity
89
91
export function headingRange ( tree , options , handler ) {
90
92
let test = options
93
+ // To do: smarter types to allow siblings of headings.
91
94
/** @type {Array<Node> } */
92
95
const children = 'children' in tree ? tree . children : [ ]
93
96
let ignoreFinalDefinitions = false
@@ -154,12 +157,11 @@ export function headingRange(tree, options, handler) {
154
157
}
155
158
}
156
159
157
- /** @type {Heading } */
158
- // @ts -expect-error `start` points to a heading.
159
160
const head = children [ start ]
160
- /** @ type { Parent } */
161
- // @ts -expect-error always a parent, or we don’t come here.
161
+ assert ( head . type === 'heading' )
162
+
162
163
const parent = tree
164
+ assert ( 'children' in parent , 'parent is a parent' )
163
165
164
166
const nodes = handler ( head , children . slice ( start + 1 , end ) , children [ end ] , {
165
167
parent,
0 commit comments