diff --git a/lib/index.js b/lib/index.js index 573fded..274026f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -39,7 +39,7 @@ import {visit} from 'unist-util-visit' * * @param {Nodes} node * Tree to search. - * @param {string} name + * @param {string | RegExp} name * Comment name to look for. * @param {Handler} handler * Handle a section. @@ -57,7 +57,8 @@ export function zone(node, name, handler) { visit(node, function (node, index, parent) { const info = commentMarker(node) const match = - info && info.name === name + info && + (typeof name === 'string' ? info.name === name : info.name.match(name)) ? info.attributes.match(/(start|end)\b/) : undefined const type = match ? match[0] : undefined diff --git a/test/fixtures/regexp/index.js b/test/fixtures/regexp/index.js new file mode 100644 index 0000000..76aa216 --- /dev/null +++ b/test/fixtures/regexp/index.js @@ -0,0 +1,16 @@ +import {zone} from 'mdast-zone' + +/** @param {import('mdast').Root} tree */ +export default function assertion(tree) { + zone(tree, /.*foo.*/, function (start, _, end) { + return [ + start, + { + type: 'paragraph', + depth: 2, + children: [{type: 'text', value: 'changed'}] + }, + end + ] + }) +} diff --git a/test/fixtures/regexp/input.md b/test/fixtures/regexp/input.md new file mode 100644 index 0000000..5e0fa88 --- /dev/null +++ b/test/fixtures/regexp/input.md @@ -0,0 +1,5 @@ +# Foo + + + + diff --git a/test/fixtures/regexp/output.md b/test/fixtures/regexp/output.md new file mode 100644 index 0000000..afbed60 --- /dev/null +++ b/test/fixtures/regexp/output.md @@ -0,0 +1,7 @@ +# Foo + + + +changed + +