Skip to content

Commit

Permalink
fix: Fix multiple ignore issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Apr 15, 2022
1 parent 084ddca commit df50949
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
57 changes: 33 additions & 24 deletions src/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ it('rehypeIgnore test case 2', async () => {
.use(stringify)
.processSync(html)
.toString()
expect(htmlStr).toEqual(html);
expect(htmlStr).toEqual('');
});

it('rehypeIgnore test case 3', async () => {
Expand All @@ -36,7 +36,7 @@ it('rehypeIgnore test case 3', async () => {
.use(stringify)
.processSync(html)
.toString()
expect(htmlStr).toEqual(html);
expect(htmlStr).toEqual('<h1>header</h1>');
});

it('rehypeIgnore test case 4', async () => {
Expand All @@ -47,21 +47,51 @@ it('rehypeIgnore test case 4', async () => {
.use(stringify)
.processSync(html)
.toString()
expect(htmlStr).toEqual(`<!--rehype:ignore:end-->`);
expect(htmlStr).toEqual('');
});

it('rehypeIgnore Markdown test case', async () => {
const html = `# Hello World
<!--rehype:ignore:start-->Hello World<!--rehype:ignore:end-->
Good!`;
const expected = `<h1>Hello World</h1>
<p>Good!</p>`;
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeIgnore, { })
.use(stringify)
.processSync(html)
.toString()

expect(htmlStr).toEqual(expected);
});


it('rehypeIgnore test case', async () => {
const html = `<!--rehype:ignore:start-->
<h1>header</h1>
<!--rehype:ignore:end-->
<p>
Hello <!--rehype:ignore:start--> <code>World</code> <!--rehype:ignore:end-->
</p>
<!--rehype:ignore:start-->
<h2>header</h2>
<!--rehype:ignore:end-->
<p>Hi</p>
`;
const expected = `
<p>
Hello
</p>
<p>Hi</p>
`;
const htmlStr = rehype()
.data('settings', { fragment: true })
Expand All @@ -71,24 +101,3 @@ it('rehypeIgnore test case', async () => {
.toString()
expect(htmlStr).toEqual(expected);
});

it('rehypeIgnore Markdown test case', async () => {
const html = `# Hello World
<!--rehype:ignore:start-->Hello World<!--rehype:ignore:end-->
Good!`;
const expected = `<h1>Hello World</h1>
<p>Good!</p>`;
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeIgnore, { })
.use(stringify)
.processSync(html)
.toString()

expect(htmlStr).toEqual(expected);
});
23 changes: 18 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,24 @@ const rehypeIgnore: Plugin<[RehypeIgnoreOptions?], Root> = (options = {}) => {
return (tree) => {
visit(tree, (node: Root | RootContent, index, parent) => {
if (node.type === 'element' || node.type === 'root') {
const start = node.children.findIndex((item) => item.type === 'comment' && item.value === openDelimiter);
const end = node.children.findIndex((item) => item.type === 'comment' && item.value === closeDelimiter);
if (start > -1 && end > -1) {
node.children = node.children.filter((_, idx) => idx < start || idx > end);
}
// const start = node.children.findIndex((item) => item.type === 'comment' && item.value === openDelimiter);
// const end = node.children.findIndex((item) => item.type === 'comment' && item.value === closeDelimiter);
// if (start > -1 && end > -1) {
// node.children = node.children.filter((_, idx) => idx < start || idx > end);
// }
let start = false;
node.children = node.children.filter((item) => {
if (item.type === 'comment' && item.value.trim() === openDelimiter) {
start = true;
return false
}
if (item.type === 'comment' && item.value.trim() === closeDelimiter) {
start = false;
return false
}

return !start;
})
}
});
}
Expand Down

0 comments on commit df50949

Please sign in to comment.