Skip to content

Commit

Permalink
fix: Fix codeBlockParames issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 28, 2022
1 parent fce17a7 commit 17dfaa6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?], Root> = (options = {}) => {
}

if (/^(em|strong|b|a|i|p|pre|kbd|blockquote|h(1|2|3|4|5|6)|code|table|img|del|ul|ol)$/.test(node.tagName) && parent && Array.isArray(parent.children) && typeof index === 'number') {
const child = nextChild(parent.children, index)
const child = nextChild(parent.children, index, '', codeBlockParames)
if (child) {
const attr = getCommentObject(child as Comment)
if (Object.keys(attr).length > 0) {
Expand Down
18 changes: 12 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ export const prevChild = (data: Literal[] = [], index: number): Comment | undefi
return;
}

export const nextChild = (data: RootContent[] | ElementContent[] = [], index: number, tagName?: string): ElementContent | undefined => {
export const nextChild = (data: RootContent[] | ElementContent[] = [], index: number, tagName?: string, codeBlockParames?: boolean): ElementContent | undefined => {
let i = index;

while (i < data.length) {
i++;
if (tagName) {
Expand All @@ -33,12 +34,17 @@ export const nextChild = (data: RootContent[] | ElementContent[] = [], index: nu
}
} else {
const element = data[i] as ElementContent & Literal;
if (!element || (element.type !== 'text' && (element.type as string) !== 'comment') || (element.type === 'text' && (element.value as string).replace(/(\n|\s)/g, '') !== '')) return;
if ((element.type as string) === 'comment') {
if (!element || element.type === 'element') return;
if (element.type === 'text' && element.value.replace(/(\n|\s)/g, '') !== '') return;
if (element?.type === 'comment') {
if (!/^rehype:/.test(element.value as string)) return;
const nextNode = nextChild(data, i, 'pre')
if (nextNode) return;
return element;
if (codeBlockParames) {
const nextNode = nextChild(data, i, 'pre', codeBlockParames)
if (nextNode) return;
return element;
} else {
return element;
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ describe('rehype-attr function test case', () => {
})

describe('rehype-attr test case', () => {
it('default codeBlockParames=false', async () => {
const mrkStr2 = "### title\n<!--rehype:title=title3-->\n```js\nconsole.log('')\n```"
const expected = `<h3 title="title3">title</h3>\n<!--rehype:title=title3-->\n<pre><code class="language-js">console.log('')\n</code></pre>`
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeAttrs, {
properties: 'attr',
codeBlockParames: false
})
.use(stringify)
.processSync(mrkStr2)
.toString()
expect(htmlStr).toEqual(expected);
});

it('default codeBlockParames=false', async () => {
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre><code class="language-js">console.log('')\n</code></pre>`
const htmlStr = unified()
Expand All @@ -88,6 +105,7 @@ describe('rehype-attr test case', () => {
.toString()
expect(htmlStr).toEqual(expected);
});

it('default options="data"', async () => {
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre data-type="rehyp"><code class="language-js" data-config="[object Object]">console.log('')\n</code></pre>`
const htmlStr = unified()
Expand Down

0 comments on commit 17dfaa6

Please sign in to comment.