Skip to content

Commit

Permalink
Add support for subsequent zones
Browse files Browse the repository at this point in the history
Closes GH-4.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
anatolykopyl authored Dec 4, 2023
1 parent c64f275 commit ec6eacf
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 16 deletions.
39 changes: 23 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,35 @@ export function zone(node, name, handler) {
{start, end: index, parent: scope}
)

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

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

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

const deleteCount = index - start + 1
scope.children.splice(
start,
deleteCount,
// @ts-expect-error: Assume the correct children are passed.
...result
)

marker = undefined
scope = undefined

return index - deleteCount + result.length
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/remove-subsequent/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @typedef {import('mdast').Root} Root
*/

import {zone} from 'mdast-zone'

/**
* @param {Root} tree
*/
export default function assertion(tree) {
zone(tree, 'foo', function () {
return []
})
}
19 changes: 19 additions & 0 deletions test/fixtures/remove-subsequent/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Foo

<!--foo start-->

Foo.

Foo.

<!--foo end-->

Bar.

<!--foo start-->

Foo.

Foo.

<!--foo end-->
3 changes: 3 additions & 0 deletions test/fixtures/remove-subsequent/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Foo

Bar.
19 changes: 19 additions & 0 deletions test/fixtures/replace-subsequent/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @typedef {import('mdast').Root} Root
*/

import {zone} from 'mdast-zone'

/**
* @param {Root} tree
*/
export default function assertion(tree) {
zone(tree, 'foo', function () {
return [
{
type: 'paragraph',
children: [{type: 'text', value: 'Bar.'}]
}
]
})
}
15 changes: 15 additions & 0 deletions test/fixtures/replace-subsequent/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Foo

<!--foo start-->

Foo.

<!--foo end-->

Bar.

<!--foo start-->

Foo.

<!--foo end-->
7 changes: 7 additions & 0 deletions test/fixtures/replace-subsequent/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Foo

Bar.

Bar.

Bar.

0 comments on commit ec6eacf

Please sign in to comment.