Skip to content

Commit

Permalink
chore: Optimize the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed May 31, 2021
1 parent 233b43d commit 371cfbd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
51 changes: 24 additions & 27 deletions __tests__/index.test.ts
Expand Up @@ -64,6 +64,7 @@ describe('rehype-attr function test case', () => {
expect(utils.nextChild([ { type: 'elment', value: 'rehype:title=Rehype Attrs' } ], 0)).toBeUndefined()
expect(utils.nextChild([ { type: 'text' }, { type: 'comment', value: 'rehype:title=Rehype Attrs' } ], 0)).toEqual({ type: "comment", value: "rehype:title=Rehype Attrs" })
expect(utils.nextChild([ { type: 'text', value: '\n' }, { type: 'comment', value: 'rehype:title=Rehype Attrs' } ], 0)).toEqual({ type: "comment", value: "rehype:title=Rehype Attrs" })
expect(utils.nextChild([ { type: 'text', value: '\n' }, { type: 'text', value: '' }, { type: 'element', tagName: 'pre' } ], 0, 'pre')).toEqual({ type: 'element', tagName: 'pre' })
});
it('propertiesHandle', async () => {
expect(utils.propertiesHandle({}, {})).toEqual({
Expand Down Expand Up @@ -129,34 +130,30 @@ describe('rehype-attr test case', () => {
.toString()
expect(htmlStr).toEqual(expected);
});
it('options="attr" - Table', async () => {
const markdown = "| Property | Description |\n |---- |---- |\n | 1 | 2 |\n\n<!--rehype:border=1-->"
const expected = `<table border="1"><thead><tr><th>Property</th><th>Description</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>\n<!--rehype:border=1-->`
const htmlStr = unified()
.use(remarkParse)
.use(gfm)
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeAttrs, { properties: 'attr' })
.use(stringify)
.processSync(markdown)
.toString()
expect(htmlStr.replace(/^\n+/, '')).toEqual(expected);
});

it('options="attr" - Table 2 `\\n\\n` ???', async () => {
const markdown = "| Property | Description |\n |---- |---- |\n | 1 | 2 |\n<!--rehype:border=1-->"
const expected = `<table><thead><tr><th>Property</th><th>Description</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr><tr><td><!--rehype:border=1--></td><td></td></tr></tbody></table>`
const htmlStr = unified()
.use(remarkParse)
.use(gfm)
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeAttrs, { properties: 'attr' })
.use(stringify)
.processSync(markdown)
.toString()
expect(htmlStr.replace(/^\n+/, '')).toEqual(expected);
[
{
title: 'options="attr" - Table',
markdown: '| Property | Description |\n |---- |---- |\n | 1 | 2 |\n\n<!--rehype:border=1-->',
expected: '<table border="1"><thead><tr><th>Property</th><th>Description</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>\n<!--rehype:border=1-->',
}, {
title: 'options="attr" - Table 2 `\\n\\n` ???',
markdown: '| Property | Description |\n |---- |---- |\n | 1 | 2 |\n<!--rehype:border=1-->',
expected: '<table><thead><tr><th>Property</th><th>Description</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr><tr><td><!--rehype:border=1--></td><td></td></tr></tbody></table>',
}
].forEach((data, idx) => {
it(data.title, async () => {
const htmlStr = unified()
.use(remarkParse)
.use(gfm)
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeAttrs, { properties: 'attr' })
.use(stringify)
.processSync(data.markdown)
.toString()
expect(htmlStr.replace(/^\n+/, '')).toEqual(data.expected);
});
});

[
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -56,7 +56,7 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?]> = (options): MdastTransformer =
return transformer
function transformer(tree: Root) {
visit(tree, 'element', (node: Root, index: number, parent: Parent) => {
const codeNode = node && node.children ? node.children[0] as any : null
const codeNode = node && node.children && node.children[0] as any
if (node.tagName === 'pre' && codeNode && codeNode.tagName === 'code' && Array.isArray(parent.children) && parent.children.length > 1) {
const child = prevChild(parent.children, index)
if (child) {
Expand Down
3 changes: 1 addition & 2 deletions src/utils.ts
Expand Up @@ -34,8 +34,7 @@ export const nextChild = (data: Content[] = [], index: number, tagName?: string)
i++;
if (tagName) {
if (data[i] && data[i].value && (data[i].value as string).replace(/(\n|\s)/g, '') !== '' || data[i] && (data[i].type as string) === 'element') {
if (data[i].tagName !== tagName) return;
if (data[i].tagName === tagName) return data[i] as unknown as CommentData;
return data[i].tagName === tagName ? data[i] as unknown as CommentData : undefined
}
} else {
if (!data[i] || (data[i].type !== 'text' && (data[i].type as string) !== 'comment') || (data[i].type == 'text' && (data[i].value as string).replace(/(\n|\s)/g, '') !== '')) return
Expand Down

0 comments on commit 371cfbd

Please sign in to comment.