Skip to content

Commit 17dfaa6

Browse files
committedSep 28, 2022
fix: Fix codeBlockParames issue.
1 parent fce17a7 commit 17dfaa6

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed
 

‎src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?], Root> = (options = {}) => {
6767
}
6868

6969
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') {
70-
const child = nextChild(parent.children, index)
70+
const child = nextChild(parent.children, index, '', codeBlockParames)
7171
if (child) {
7272
const attr = getCommentObject(child as Comment)
7373
if (Object.keys(attr).length > 0) {

‎src/utils.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ export const prevChild = (data: Literal[] = [], index: number): Comment | undefi
2222
return;
2323
}
2424

25-
export const nextChild = (data: RootContent[] | ElementContent[] = [], index: number, tagName?: string): ElementContent | undefined => {
25+
export const nextChild = (data: RootContent[] | ElementContent[] = [], index: number, tagName?: string, codeBlockParames?: boolean): ElementContent | undefined => {
2626
let i = index;
27+
2728
while (i < data.length) {
2829
i++;
2930
if (tagName) {
@@ -33,12 +34,17 @@ export const nextChild = (data: RootContent[] | ElementContent[] = [], index: nu
3334
}
3435
} else {
3536
const element = data[i] as ElementContent & Literal;
36-
if (!element || (element.type !== 'text' && (element.type as string) !== 'comment') || (element.type === 'text' && (element.value as string).replace(/(\n|\s)/g, '') !== '')) return;
37-
if ((element.type as string) === 'comment') {
37+
if (!element || element.type === 'element') return;
38+
if (element.type === 'text' && element.value.replace(/(\n|\s)/g, '') !== '') return;
39+
if (element?.type === 'comment') {
3840
if (!/^rehype:/.test(element.value as string)) return;
39-
const nextNode = nextChild(data, i, 'pre')
40-
if (nextNode) return;
41-
return element;
41+
if (codeBlockParames) {
42+
const nextNode = nextChild(data, i, 'pre', codeBlockParames)
43+
if (nextNode) return;
44+
return element;
45+
} else {
46+
return element;
47+
}
4248
}
4349
}
4450
}

‎test/index.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ describe('rehype-attr function test case', () => {
7676
})
7777

7878
describe('rehype-attr test case', () => {
79+
it('default codeBlockParames=false', async () => {
80+
const mrkStr2 = "### title\n<!--rehype:title=title3-->\n```js\nconsole.log('')\n```"
81+
const expected = `<h3 title="title3">title</h3>\n<!--rehype:title=title3-->\n<pre><code class="language-js">console.log('')\n</code></pre>`
82+
const htmlStr = unified()
83+
.use(remarkParse)
84+
.use(remark2rehype, { allowDangerousHtml: true })
85+
.use(rehypeRaw)
86+
.use(rehypeAttrs, {
87+
properties: 'attr',
88+
codeBlockParames: false
89+
})
90+
.use(stringify)
91+
.processSync(mrkStr2)
92+
.toString()
93+
expect(htmlStr).toEqual(expected);
94+
});
95+
7996
it('default codeBlockParames=false', async () => {
8097
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre><code class="language-js">console.log('')\n</code></pre>`
8198
const htmlStr = unified()
@@ -88,6 +105,7 @@ describe('rehype-attr test case', () => {
88105
.toString()
89106
expect(htmlStr).toEqual(expected);
90107
});
108+
91109
it('default options="data"', async () => {
92110
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre data-type="rehyp"><code class="language-js" data-config="[object Object]">console.log('')\n</code></pre>`
93111
const htmlStr = unified()

0 commit comments

Comments
 (0)
Please sign in to comment.