Skip to content

Commit ec6eacf

Browse files
authored
Add support for subsequent zones
Closes GH-4. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent c64f275 commit ec6eacf

File tree

7 files changed

+100
-16
lines changed

7 files changed

+100
-16
lines changed

lib/index.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,35 @@ export function zone(node, name, handler) {
8888
{start, end: index, parent: scope}
8989
)
9090

91-
if (nodes) {
92-
// Ensure no empty nodes are inserted.
93-
// This could be the case if `end` is in `nodes` but no `end` node exists.
94-
/** @type {Array<Nodes>} */
95-
const result = []
96-
let offset = -1
91+
if (!nodes) {
92+
marker = undefined
93+
scope = undefined
94+
return
95+
}
9796

98-
while (++offset < nodes.length) {
99-
const node = nodes[offset]
100-
if (node) result.push(node)
101-
}
97+
// Ensure no empty nodes are inserted.
98+
// This could be the case if `end` is in `nodes` but no `end` node exists.
99+
/** @type {Array<Nodes>} */
100+
const result = []
101+
let offset = -1
102102

103-
scope.children.splice(
104-
start,
105-
index - start + 1,
106-
// @ts-expect-error: Assume the correct children are passed.
107-
...result
108-
)
103+
while (++offset < nodes.length) {
104+
const node = nodes[offset]
105+
if (node) result.push(node)
109106
}
110107

108+
const deleteCount = index - start + 1
109+
scope.children.splice(
110+
start,
111+
deleteCount,
112+
// @ts-expect-error: Assume the correct children are passed.
113+
...result
114+
)
115+
111116
marker = undefined
112117
scope = undefined
118+
119+
return index - deleteCount + result.length
113120
}
114121
}
115122
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @typedef {import('mdast').Root} Root
3+
*/
4+
5+
import {zone} from 'mdast-zone'
6+
7+
/**
8+
* @param {Root} tree
9+
*/
10+
export default function assertion(tree) {
11+
zone(tree, 'foo', function () {
12+
return []
13+
})
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Foo
2+
3+
<!--foo start-->
4+
5+
Foo.
6+
7+
Foo.
8+
9+
<!--foo end-->
10+
11+
Bar.
12+
13+
<!--foo start-->
14+
15+
Foo.
16+
17+
Foo.
18+
19+
<!--foo end-->
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Foo
2+
3+
Bar.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @typedef {import('mdast').Root} Root
3+
*/
4+
5+
import {zone} from 'mdast-zone'
6+
7+
/**
8+
* @param {Root} tree
9+
*/
10+
export default function assertion(tree) {
11+
zone(tree, 'foo', function () {
12+
return [
13+
{
14+
type: 'paragraph',
15+
children: [{type: 'text', value: 'Bar.'}]
16+
}
17+
]
18+
})
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Foo
2+
3+
<!--foo start-->
4+
5+
Foo.
6+
7+
<!--foo end-->
8+
9+
Bar.
10+
11+
<!--foo start-->
12+
13+
Foo.
14+
15+
<!--foo end-->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Foo
2+
3+
Bar.
4+
5+
Bar.
6+
7+
Bar.

0 commit comments

Comments
 (0)