|
4 | 4 | * @typedef {import('mdast').Heading} Heading
|
5 | 5 | *
|
6 | 6 | * @typedef {(value: string, node: Heading) => boolean} TestFunction
|
| 7 | + * Function called for each heading with its content and `node` itself check |
| 8 | + * if it’s the one to look for. |
| 9 | + * |
7 | 10 | * @typedef {string|RegExp|TestFunction} Test
|
8 | 11 | *
|
9 | 12 | * @typedef Options
|
| 13 | + * Configuration (optional). |
10 | 14 | * @property {Test} test
|
| 15 | + * Heading to look for. |
| 16 | + * When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`; |
| 17 | + * when `RegExp`, wrapped in `function (value) {expression.test(value)}` |
11 | 18 | * @property {boolean} [ignoreFinalDefinitions=false]
|
| 19 | + * Ignore final definitions otherwise in the section. |
12 | 20 | *
|
13 | 21 | * @typedef ZoneInfo
|
| 22 | + * Extra info. |
| 23 | + * @property {Parent|null} parent |
| 24 | + * Parent of the range. |
14 | 25 | * @property {number} start
|
| 26 | + * index of `start` in `parent` |
15 | 27 | * @property {number} end
|
16 |
| - * @property {Parent|null} parent |
| 28 | + * index of `end` in `parent` |
17 | 29 | *
|
18 | 30 | * @callback Handler
|
| 31 | + * Callback called when a range is found. |
19 | 32 | * @param {Heading|undefined} start
|
20 |
| - * @param {Array.<Node>} between |
| 33 | + * Start of range. |
| 34 | + * @param {Array<Node>} between |
| 35 | + * Nodes between `start` and `end`. |
21 | 36 | * @param {Node|undefined} end
|
22 |
| - * @param {ZoneInfo} info |
| 37 | + * End of range, if any. |
| 38 | + * @param {ZoneInfo} scope |
| 39 | + * Extra info. |
23 | 40 | */
|
24 | 41 |
|
25 | 42 | import {toString} from 'mdast-util-to-string'
|
26 | 43 |
|
27 | 44 | /**
|
28 |
| - * Search `node` with `options` and invoke `callback`. |
| 45 | + * Search `tree` and transform a section without affecting other parts with |
| 46 | + * `handler`. |
| 47 | + * |
| 48 | + * A “section” is a heading that passes `test`, until the next heading of the |
| 49 | + * same or lower depth, or the end of the document. |
| 50 | + * If `ignoreFinalDefinitions: true`, final definitions “in” the section are |
| 51 | + * excluded. |
29 | 52 | *
|
30 |
| - * @param {Node} node |
| 53 | + * @param {Node} tree |
31 | 54 | * @param {Test|Options} options
|
32 | 55 | * @param {Handler} handler
|
33 | 56 | */
|
34 | 57 | // eslint-disable-next-line complexity
|
35 |
| -export function headingRange(node, options, handler) { |
| 58 | +export function headingRange(tree, options, handler) { |
36 | 59 | let test = options
|
37 |
| - /** @type {Array.<Node>} */ |
38 |
| - const children = 'children' in node ? node.children : [] |
| 60 | + /** @type {Array<Node>} */ |
| 61 | + const children = 'children' in tree ? tree.children : [] |
39 | 62 | /** @type {boolean|undefined} */
|
40 | 63 | let ignoreFinalDefinitions
|
41 | 64 |
|
@@ -101,19 +124,19 @@ export function headingRange(node, options, handler) {
|
101 | 124 | }
|
102 | 125 | }
|
103 | 126 |
|
104 |
| - /** @type {Array.<Node>} */ |
| 127 | + /** @type {Array<Node>} */ |
105 | 128 | const nodes = handler(
|
106 | 129 | // @ts-expect-error `start` points to a heading.
|
107 | 130 | children[start],
|
108 | 131 | children.slice(start + 1, end),
|
109 | 132 | children[end],
|
110 |
| - {parent: node, start, end: children[end] ? end : null} |
| 133 | + {parent: tree, start, end: children[end] ? end : null} |
111 | 134 | )
|
112 | 135 |
|
113 | 136 | if (nodes) {
|
114 | 137 | // Ensure no empty nodes are inserted.
|
115 | 138 | // This could be the case if `end` is in `nodes` but no `end` node exists.
|
116 |
| - /** @type {Array.<Node>} */ |
| 139 | + /** @type {Array<Node>} */ |
117 | 140 | const result = []
|
118 | 141 | let index = -1
|
119 | 142 |
|
|
0 commit comments