Skip to content

Commit

Permalink
fix: Fix the property setting of the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed May 30, 2021
1 parent cf120ca commit 102d675
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
38 changes: 36 additions & 2 deletions __tests__/index.test.ts
Expand Up @@ -9,12 +9,41 @@ const remarkParse = require('remark-parse')
const gfm = require('remark-gfm')
const rehypeAttrs = require('../lib')
const utils = require('../lib/utils')
const visit = require('../lib/visit')

const mrkStr = "<!--rehype:title=Rehype Attrs-->\n```js\nconsole.log('')\n```"

describe('rehype-attr function test case', () => {
it('getURLParameters', async () => {
expect(utils.getURLParameters('title=1&b=2')).toEqual({ title: "1", b: "2" });
it('visit', async () => {
const node = {
"type": "root",
"children": [
{
"type": "element",
"tagName": "p",
"properties": {},
"children": [
{ "type": "text", "value": "This is a " },
{ "type": "element", "tagName": "del", "properties": {}, "children": [ { "type": "text", "value": "title" } ] },
{ "type": "comment", "value": "rehype:style=color:pink;" }
]
}
],
"data": { "quirksMode": false }
}
visit(node, 'element', (node, index, parent) => {
expect(/(del|p)/.test(node.tagName)).toBeTruthy()
expect(typeof node).toEqual('object')
expect(typeof index).toEqual('number')
expect(typeof parent).toEqual('object')
})
expect(visit(node)).toBeUndefined()
expect(visit(node, 'element')).toBeUndefined()
expect(visit(node, 'element', () => {})).toBeUndefined()
expect(visit()).toBeUndefined()
expect(visit(undefined)).toBeUndefined()
expect(visit(undefined, undefined)).toBeUndefined()
expect(visit(undefined, undefined, undefined)).toBeUndefined()
});
it('getCommentObject', async () => {
expect(utils.getCommentObject({})).toEqual({ });
Expand Down Expand Up @@ -165,6 +194,11 @@ describe('rehype-attr test case', () => {
markdown: '<!--rehype:title=Rehype Attrs-->\n```js\nconsole.log("")\n```',
expected: '<!--rehype:title=Rehype Attrs-->\n<pre data-type="rehyp"><code class="language-js" title="Rehype Attrs">console.log("")\n</code></pre>',
},
{
title: 'options="attr" - Code',
markdown: '```js\nconsole.log("")\n```\n<!--rehype:title=Rehype Attrs-->',
expected: '<pre title="Rehype Attrs"><code class="language-js">console.log("")\n</code></pre>\n<!--rehype:title=Rehype Attrs-->',
},
{
title: 'options="attr" - Emphasis <em>',
markdown: 'Npm stand for *node*<!--rehype:style=color: red--> packet manager.',
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Expand Up @@ -66,7 +66,8 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?]> = (options): MdastTransformer =
codeNode.properties = propertiesHandle(codeNode.properties, attr, opts.properties)
}
}
} else if (/^(em|strong|b|a|i|p|blockquote|h(1|2|3|4|5|6)|code|img|del|ul|ol)$/.test(node.tagName as string)) {
}
if (/^(em|strong|b|a|i|p|pre|blockquote|h(1|2|3|4|5|6)|code|img|del|ul|ol)$/.test(node.tagName as string)) {
const child = nextChild(parent.children, index)
if (child) {
const attr = getCommentObject(child)
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Expand Up @@ -19,12 +19,13 @@ export const prevChild = (data: Content[] = [], index: number): CommentData | un
let i = index;
while (i > -1) {
i--;
if ((data[i].value && (data[i].value as string).replace(/(\n|\s)/g, '') !== '') || data[i].type !== 'text') {
if (!data[i]) return
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;
}
}
return {} as CommentData;
return;
}

export const nextChild = (data: Content[] = [], index: number, tagName?: string): CommentData | undefined => {
Expand Down

0 comments on commit 102d675

Please sign in to comment.