|
1 | 1 | /** |
| 2 | + * @typedef {import('hast').Root} Root |
| 3 | + * @typedef {import('hast').Content} Content |
| 4 | + * @typedef {import('mdast').Content} MdastContent |
2 | 5 | * @typedef {import('./types.js').H} H |
3 | | - * @typedef {import('./types.js').Node} Node |
4 | | - * @typedef {import('./types.js').MdastNode} MdastNode |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {Root | Content} Node |
| 10 | + * @typedef {Extract<Node, import('unist').Parent>} Parent |
5 | 11 | */ |
6 | 12 |
|
7 | 13 | import {one} from './one.js' |
8 | 14 |
|
9 | 15 | /** |
10 | 16 | * @param {H} h |
11 | | - * @param {Node} parent |
12 | | - * @returns {Array<MdastNode>} |
| 17 | + * Context. |
| 18 | + * @param {Parent} parent |
| 19 | + * Parent to transform. |
| 20 | + * @returns {Array<MdastContent>} |
| 21 | + * mdast nodes. |
13 | 22 | */ |
14 | 23 | export function all(h, parent) { |
15 | | - /** @type {Array<Node>} */ |
16 | | - // @ts-expect-error Assume `parent` is a parent. |
17 | | - const nodes = parent.children || [] |
18 | | - /** @type {Array<MdastNode>} */ |
19 | | - const values = [] |
| 24 | + const children = parent.children || [] |
| 25 | + /** @type {Array<MdastContent>} */ |
| 26 | + const results = [] |
20 | 27 | let index = -1 |
21 | 28 |
|
22 | | - while (++index < nodes.length) { |
23 | | - // @ts-expect-error assume `parent` is a parent. |
24 | | - const result = one(h, nodes[index], parent) |
| 29 | + while (++index < children.length) { |
| 30 | + const child = children[index] |
| 31 | + const result = one(h, child, parent) |
25 | 32 |
|
26 | 33 | if (Array.isArray(result)) { |
27 | | - values.push(...result) |
| 34 | + // @ts-expect-error: assume no `root`. |
| 35 | + results.push(...result) |
28 | 36 | } else if (result) { |
29 | | - values.push(result) |
| 37 | + // @ts-expect-error: assume no `root`. |
| 38 | + results.push(result) |
30 | 39 | } |
31 | 40 | } |
32 | 41 |
|
33 | 42 | let start = 0 |
34 | | - let end = values.length |
| 43 | + let end = results.length |
35 | 44 |
|
36 | | - while (start < end && values[start].type === 'break') { |
| 45 | + while (start < end && results[start].type === 'break') { |
37 | 46 | start++ |
38 | 47 | } |
39 | 48 |
|
40 | | - while (end > start && values[end - 1].type === 'break') { |
| 49 | + while (end > start && results[end - 1].type === 'break') { |
41 | 50 | end-- |
42 | 51 | } |
43 | 52 |
|
44 | | - return start === 0 && end === values.length |
45 | | - ? values |
46 | | - : values.slice(start, end) |
| 53 | + return start === 0 && end === results.length |
| 54 | + ? results |
| 55 | + : results.slice(start, end) |
47 | 56 | } |
0 commit comments