|
1 | 1 | /**
|
2 |
| - * @typedef {import('mdast').Root|import('mdast').Content} Node |
3 |
| - * @typedef {Extract<Node, import('mdast').Parent>} Parent |
4 |
| - * |
5 |
| - * @typedef ZoneInfo |
6 |
| - * Extra info. |
7 |
| - * @property {Parent} parent |
8 |
| - * Parent of the range. |
9 |
| - * @property {number} start |
10 |
| - * Index of `start` in `parent` |
11 |
| - * @property {number} end |
12 |
| - * Index of `end` in `parent` |
13 |
| - * |
14 |
| - * @callback Handler |
15 |
| - * Callback called when a range is found. |
16 |
| - * @param {Node} start |
17 |
| - * Start of range. |
18 |
| - * @param {Array<Node>} between |
19 |
| - * Nodes between `start` and `end`. |
20 |
| - * @param {Node} end |
21 |
| - * End of range. |
22 |
| - * @param {ZoneInfo} info |
23 |
| - * Extra info. |
24 |
| - * @returns {Array<Node>|null|undefined|void} |
25 |
| - * Nodes to replace. |
| 2 | + * @typedef {import('./lib/index.js').Handler} Handler |
| 3 | + * @typedef {import('./lib/index.js').ZoneInfo} ZoneInfo |
26 | 4 | */
|
27 | 5 |
|
28 |
| -import {commentMarker} from 'mdast-comment-marker' |
29 |
| -import {visit} from 'unist-util-visit' |
30 |
| - |
31 |
| -/** |
32 |
| - * @param {Node} node |
33 |
| - * Tree to search. |
34 |
| - * @param {string} name |
35 |
| - * Comment name to look for. |
36 |
| - * @param {Handler} callback |
37 |
| - * Callback called when a range is found. |
38 |
| - */ |
39 |
| -export function zone(node, name, callback) { |
40 |
| - /** @type {number|undefined} */ |
41 |
| - let level |
42 |
| - /** @type {Node|undefined} */ |
43 |
| - let marker |
44 |
| - /** @type {Parent|undefined} */ |
45 |
| - let scope |
46 |
| - |
47 |
| - visit( |
48 |
| - node, |
49 |
| - /** |
50 |
| - * Gather one dimensional zones. |
51 |
| - */ |
52 |
| - (node, index, parent) => { |
53 |
| - const info = commentMarker(node) |
54 |
| - const match = |
55 |
| - info && info.name === name && info.attributes.match(/(start|end)\b/) |
56 |
| - const type = match && match[0] |
57 |
| - |
58 |
| - if (parent && index !== null && type) { |
59 |
| - if (!scope && type === 'start') { |
60 |
| - level = 0 |
61 |
| - marker = node |
62 |
| - scope = /** @type {Parent} */ (parent) |
63 |
| - } |
64 |
| - |
65 |
| - if (typeof level === 'number' && marker && scope && parent === scope) { |
66 |
| - if (type === 'start') { |
67 |
| - level++ |
68 |
| - } else { |
69 |
| - level-- |
70 |
| - } |
71 |
| - |
72 |
| - if (type === 'end' && !level) { |
73 |
| - // @ts-expect-error: Assume `scope` is a valid parent of `node`. |
74 |
| - const start = scope.children.indexOf(marker) |
75 |
| - |
76 |
| - const result = callback( |
77 |
| - marker, |
78 |
| - scope.children.slice(start + 1, index), |
79 |
| - node, |
80 |
| - {start, end: index, parent: scope} |
81 |
| - ) |
82 |
| - |
83 |
| - if (result) { |
84 |
| - // @ts-expect-error: Assume the correct children are passed. |
85 |
| - scope.children.splice(start, index - start + 1, ...result) |
86 |
| - } |
87 |
| - |
88 |
| - marker = undefined |
89 |
| - scope = undefined |
90 |
| - } |
91 |
| - } |
92 |
| - } |
93 |
| - } |
94 |
| - ) |
95 |
| -} |
| 6 | +export {zone} from './lib/index.js' |
0 commit comments