From ec6eacf843cd33246e6e361ca4948b88329a6cfe Mon Sep 17 00:00:00 2001 From: Anatoly Kopyl <33553182+anatolykopyl@users.noreply.github.com> Date: Mon, 4 Dec 2023 18:55:43 +0300 Subject: [PATCH] Add support for subsequent zones Closes GH-4. Reviewed-by: Titus Wormer --- lib/index.js | 39 +++++++++++++--------- test/fixtures/remove-subsequent/index.js | 14 ++++++++ test/fixtures/remove-subsequent/input.md | 19 +++++++++++ test/fixtures/remove-subsequent/output.md | 3 ++ test/fixtures/replace-subsequent/index.js | 19 +++++++++++ test/fixtures/replace-subsequent/input.md | 15 +++++++++ test/fixtures/replace-subsequent/output.md | 7 ++++ 7 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 test/fixtures/remove-subsequent/index.js create mode 100644 test/fixtures/remove-subsequent/input.md create mode 100644 test/fixtures/remove-subsequent/output.md create mode 100644 test/fixtures/replace-subsequent/index.js create mode 100644 test/fixtures/replace-subsequent/input.md create mode 100644 test/fixtures/replace-subsequent/output.md diff --git a/lib/index.js b/lib/index.js index 320aaaf..d9d977b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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} */ - 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} */ + 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 } } } diff --git a/test/fixtures/remove-subsequent/index.js b/test/fixtures/remove-subsequent/index.js new file mode 100644 index 0000000..d643726 --- /dev/null +++ b/test/fixtures/remove-subsequent/index.js @@ -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 [] + }) +} diff --git a/test/fixtures/remove-subsequent/input.md b/test/fixtures/remove-subsequent/input.md new file mode 100644 index 0000000..8ec8bd8 --- /dev/null +++ b/test/fixtures/remove-subsequent/input.md @@ -0,0 +1,19 @@ +# Foo + + + +Foo. + +Foo. + + + +Bar. + + + +Foo. + +Foo. + + diff --git a/test/fixtures/remove-subsequent/output.md b/test/fixtures/remove-subsequent/output.md new file mode 100644 index 0000000..0cf7d4f --- /dev/null +++ b/test/fixtures/remove-subsequent/output.md @@ -0,0 +1,3 @@ +# Foo + +Bar. diff --git a/test/fixtures/replace-subsequent/index.js b/test/fixtures/replace-subsequent/index.js new file mode 100644 index 0000000..9912f36 --- /dev/null +++ b/test/fixtures/replace-subsequent/index.js @@ -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.'}] + } + ] + }) +} diff --git a/test/fixtures/replace-subsequent/input.md b/test/fixtures/replace-subsequent/input.md new file mode 100644 index 0000000..22a750d --- /dev/null +++ b/test/fixtures/replace-subsequent/input.md @@ -0,0 +1,15 @@ +# Foo + + + +Foo. + + + +Bar. + + + +Foo. + + diff --git a/test/fixtures/replace-subsequent/output.md b/test/fixtures/replace-subsequent/output.md new file mode 100644 index 0000000..930aaf9 --- /dev/null +++ b/test/fixtures/replace-subsequent/output.md @@ -0,0 +1,7 @@ +# Foo + +Bar. + +Bar. + +Bar.