|
1 | 1 | /**
|
2 |
| - * @typedef {import('unist').Node} Node |
3 |
| - * @typedef {import('unist').Parent} Parent |
4 |
| - * @typedef {import('unist-util-visit').Visitor<Node>} Visitor |
| 2 | + * @typedef {import('mdast').Root|import('mdast').Content} Node |
| 3 | + * @typedef {Extract<Node, import('mdast').Parent>} Parent |
5 | 4 | *
|
6 | 5 | * @typedef ZoneInfo
|
7 | 6 | * @property {number} start
|
@@ -32,50 +31,52 @@ export function zone(node, name, callback) {
|
32 | 31 | /** @type {Parent|undefined} */
|
33 | 32 | let scope
|
34 | 33 |
|
35 |
| - visit(node, gather) |
| 34 | + visit( |
| 35 | + node, |
| 36 | + /** |
| 37 | + * Gather one dimensional zones. |
| 38 | + */ |
| 39 | + (node, index, parent) => { |
| 40 | + const info = commentMarker(node) |
| 41 | + const match = |
| 42 | + info && info.name === name && info.attributes.match(/(start|end)\b/) |
| 43 | + const type = match && match[0] |
36 | 44 |
|
37 |
| - /** |
38 |
| - * Gather one dimensional zones. |
39 |
| - * @type {Visitor} |
40 |
| - */ |
41 |
| - function gather(node, index, parent) { |
42 |
| - const info = commentMarker(node) |
43 |
| - const match = |
44 |
| - info && info.name === name && info.attributes.match(/(start|end)\b/) |
45 |
| - const type = match && match[0] |
| 45 | + if (parent && index !== null && type) { |
| 46 | + if (!scope && type === 'start') { |
| 47 | + level = 0 |
| 48 | + marker = node |
| 49 | + scope = /** @type {Parent} */ (parent) |
| 50 | + } |
46 | 51 |
|
47 |
| - if (parent && index !== null && type) { |
48 |
| - if (!scope && type === 'start') { |
49 |
| - level = 0 |
50 |
| - marker = node |
51 |
| - scope = parent |
52 |
| - } |
| 52 | + if (typeof level === 'number' && marker && scope && parent === scope) { |
| 53 | + if (type === 'start') { |
| 54 | + level++ |
| 55 | + } else { |
| 56 | + level-- |
| 57 | + } |
53 | 58 |
|
54 |
| - if (typeof level === 'number' && marker && scope && parent === scope) { |
55 |
| - if (type === 'start') { |
56 |
| - level++ |
57 |
| - } else { |
58 |
| - level-- |
59 |
| - } |
| 59 | + if (type === 'end' && !level) { |
| 60 | + // @ts-expect-error: Assume `scope` is a valid parent of `node`. |
| 61 | + const start = scope.children.indexOf(marker) |
60 | 62 |
|
61 |
| - if (type === 'end' && !level) { |
62 |
| - const start = scope.children.indexOf(marker) |
| 63 | + const result = callback( |
| 64 | + marker, |
| 65 | + scope.children.slice(start + 1, index), |
| 66 | + node, |
| 67 | + {start, end: index, parent: scope} |
| 68 | + ) |
63 | 69 |
|
64 |
| - const result = callback( |
65 |
| - marker, |
66 |
| - scope.children.slice(start + 1, index), |
67 |
| - node, |
68 |
| - {start, end: index, parent} |
69 |
| - ) |
| 70 | + if (result) { |
| 71 | + // @ts-expect-error: Assume the correct children are passed. |
| 72 | + scope.children.splice(start, index - start + 1, ...result) |
| 73 | + } |
70 | 74 |
|
71 |
| - if (result) { |
72 |
| - scope.children.splice(start, index - start + 1, ...result) |
| 75 | + marker = undefined |
| 76 | + scope = undefined |
73 | 77 | }
|
74 |
| - |
75 |
| - marker = undefined |
76 |
| - scope = undefined |
77 | 78 | }
|
78 | 79 | }
|
79 | 80 | }
|
80 |
| - } |
| 81 | + ) |
81 | 82 | }
|
0 commit comments