Skip to content

Commit

Permalink
fix: Fix child filter issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Apr 6, 2022
1 parent 642144c commit aefdb6d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
34 changes: 28 additions & 6 deletions src/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import rehypeRaw from 'rehype-raw';
import stringify from 'rehype-stringify';
import rehypeIgnore from '..';

it('rehypeIgnore test case', async () => {
it('rehypeIgnore test case 1', async () => {
const html = `<!--rehype:ignore:start--><h1>header</h1><!--rehype:ignore:end-->`;
const htmlStr = rehype()
.data('settings', { fragment: true })
Expand All @@ -17,7 +17,7 @@ it('rehypeIgnore test case', async () => {
expect(htmlStr).toEqual('');
});

it('rehypeIgnore test case', async () => {
it('rehypeIgnore test case 2', async () => {
const html = `<!--rehype:ignore:start--><h1>header</h1>`;
const htmlStr = rehype()
.data('settings', { fragment: true })
Expand All @@ -28,7 +28,7 @@ it('rehypeIgnore test case', async () => {
expect(htmlStr).toEqual(html);
});

it('rehypeIgnore test case', async () => {
it('rehypeIgnore test case 3', async () => {
const html = `<h1>header</h1><!--rehype:ignore:end-->`;
const htmlStr = rehype()
.data('settings', { fragment: true })
Expand All @@ -39,7 +39,7 @@ it('rehypeIgnore test case', async () => {
expect(htmlStr).toEqual(html);
});

it('rehypeIgnore test case', async () => {
it('rehypeIgnore test case 4', async () => {
const html = `<!--rehype:ignore:start--><h1>header</h1><!--rehype:ignore:end--><!--rehype:ignore:end-->`;
const htmlStr = rehype()
.data('settings', { fragment: true })
Expand All @@ -60,13 +60,35 @@ it('rehypeIgnore test case', async () => {
`;
const expected = `
<p>
Hello </p>
`
Hello
</p>
`;
const htmlStr = rehype()
.data('settings', { fragment: true })
.use(rehypeIgnore, { })
.use(stringify)
.processSync(html)
.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);
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const rehypeIgnore: Plugin<[RehypeIgnoreOptions?], Root> = (options = {}) => {
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.splice(start, end + 1);
node.children = node.children.filter((_, idx) => idx < start || idx > end);
}
}
});
Expand Down

0 comments on commit aefdb6d

Please sign in to comment.