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 30, 2021
1 parent e0998fa commit 8ab766b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 19 additions & 1 deletion __tests__/index.test.ts
Expand Up @@ -51,6 +51,20 @@ describe('rehype-attr function test case', () => {
expect(utils.getCommentObject({})).toEqual({ });
expect(utils.getCommentObject({ value: 'rehype:title=Rehype Attrs' })).toEqual({ title: 'Rehype Attrs' });
});
it('prevChild', async () => {
expect(utils.prevChild(undefined, 0)).toBeUndefined()
expect(utils.prevChild(undefined, -1)).toBeUndefined()
expect(utils.prevChild([ { type: 'elment', value: 'rehype:title=Rehype Attrs' } ], 1)).toEqual({ type: "elment", value: "rehype:title=Rehype Attrs" })
expect(utils.prevChild([ { type: 'comment', value: 'rehype:title=Rehype Attrs' }, { type: 'text' } ], 1)).toEqual({ type: "comment", value: "rehype:title=Rehype Attrs" })
expect(utils.prevChild([ { type: 'text', value: '\n' }, { type: 'comment', value: 'rehype:title=Rehype Attrs' } ], 2)).toEqual({ type: "comment", value: "rehype:title=Rehype Attrs" })
});
it('prevChild', async () => {
expect(utils.nextChild(undefined, 0)).toBeUndefined()
expect(utils.nextChild(undefined, -1)).toBeUndefined()
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" })
});
it('propertiesHandle', async () => {
expect(utils.propertiesHandle({}, {})).toEqual({
'data-config': {
Expand Down Expand Up @@ -145,7 +159,6 @@ describe('rehype-attr test case', () => {
expect(htmlStr).toEqual(expected);
});


it('options="attr" - Multiple value settings 3', async () => {
const markdown = "test\n<!--rehype:title=Rehype Attrs-->\n```js\nconsole.log('')\n```"
const expected = `<p>test</p>\n<!--rehype:title=Rehype Attrs-->\n<pre data-type="rehyp"><code class="language-js" title="Rehype Attrs">console.log('')\n</code></pre>`
Expand Down Expand Up @@ -191,6 +204,11 @@ describe('rehype-attr test case', () => {
markdown: 'test\n<!--rehype:a&-->',
expected: '<p>test</p>\n<!--rehype:a&-->',
},
{
title: 'options="attr" - Code - not config 7',
markdown: '<!--rehype:-->\n```js\nconsole.log("")\n```',
expected: '<!--rehype:-->\n<pre><code class="language-js">console.log("")\n</code></pre>',
},
{
title: 'options="attr" - Code',
markdown: '<!--rehype:title=Rehype Attrs-->\n```js\nconsole.log("")\n```',
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Expand Up @@ -20,7 +20,7 @@ export const prevChild = (data: Content[] = [], index: number): CommentData | un
while (i > -1) {
i--;
if (!data[i]) return
if ((data[i] && data[i].value && ((data[i].value || '') as string).replace(/(\n|\s)/g, '') !== '') || data[i].type !== 'text') {
if ((data[i] && data[i].value && (data[i].value as string).replace(/(\n|\s)/g, '') !== '') || data[i].type !== 'text') {
if (!/rehype:/.test(data[i].value as string) && (data[i].type as string) !== 'comment') return;
return data[i] as unknown as CommentData;
}
Expand Down Expand Up @@ -72,7 +72,7 @@ export const getCommentObject = ({ value = '' }: CommentData): Record<string, st
return param;
}

export const propertiesHandle = (defaultAttrs: Record<string, string> = {}, attrs: Record<string, string | number | boolean | null>, type: RehypeAttrsOptions['properties']) => {
export const propertiesHandle = (defaultAttrs: Record<string, string>, attrs: Record<string, string | number | boolean | null>, type: RehypeAttrsOptions['properties']) => {
if (type === 'string') {
return { ...defaultAttrs, 'data-config': JSON.stringify({ ...attrs, rehyp: true })}
} else if (type === 'attr') {
Expand Down

0 comments on commit 8ab766b

Please sign in to comment.