Skip to content

Commit 72e7ff9

Browse files
committed
fix: Fix unsupported traditional markdown list issue. #58
1 parent ca0c7b5 commit 72e7ff9

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

src/index.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Plugin } from 'unified';
2-
import type { Root, Element, Comment, Properties, Literal } from 'hast';
2+
import type { Root, Element, Comment, Properties, Literal, RootContent, ElementContent } from 'hast';
33
import { visit } from 'unist-util-visit';
44
import { propertiesHandle, nextChild, prevChild, getCommentObject } from './utils.js';
55

@@ -76,18 +76,36 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?], Root> = (options = {}) => {
7676
}
7777
}
7878
let rootnode = parent as Root
79-
if ((/^(em|strong|b|a|i|p|pre|kbd|blockquote|h(1|2|3|4|5|6)|code|table|img|del|ul|ol|li)$/.test(node.tagName) || rootnode.type == "root") && parent && Array.isArray(parent.children) && typeof index === 'number') {
80-
const child = nextChild(parent.children, index, '', commentStart, commentEnd)
81-
if (child) {
82-
const attr = getCommentObject(child as Comment, commentStart, commentEnd)
83-
if (Object.keys(attr).length > 0) {
84-
node.properties = propertiesHandle(node.properties, attr, properties) as Properties
85-
}
79+
let testTagName = /^(em|strong|b|a|i|p|pre|kbd|blockquote|h(1|2|3|4|5|6)|code|table|img|del|ul|ol|li)$/.test(node.tagName)
80+
if ((testTagName || rootnode.type == "root") && parent && Array.isArray(parent.children) && typeof index === 'number') {
81+
addPropertyToNode(node, parent.children, index, properties, commentStart, commentEnd)
82+
if (node.tagName == "ul") {
83+
node.children.forEach((li, _) => {
84+
if (li.type == "element" && li.tagName == "li") {
85+
addPropertyToNode(li, li.children, 0, properties, commentStart, commentEnd)
86+
}
87+
})
8688
}
8789
}
8890
});
8991
}
9092
}
9193

94+
function addPropertyToNode(
95+
node: Element,
96+
children: RootContent[] | ElementContent[] = [],
97+
index: number,
98+
properties: RehypeAttrsOptions['properties'],
99+
commentStart = "<!--",
100+
commentEnd = "-->"
101+
) {
102+
const child = nextChild(children, index, "", commentStart, commentEnd)
103+
if (child) {
104+
const attr = getCommentObject(child as Comment, commentStart, commentEnd)
105+
if (Object.keys(attr).length > 0) {
106+
node.properties = propertiesHandle(node.properties, attr, properties) as Properties
107+
}
108+
}
109+
}
92110

93111
export default rehypeAttrs

src/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ export const nextChild = (data: RootContent[] | ElementContent[] = [], index: nu
2626
let i = index;
2727
while (i < data.length) {
2828
i++;
29+
const element = data[i] as Literal & Element;
2930
if (tagName) {
30-
const element = data[i] as Literal & Element;
3131
if (element && element.value && (element.value as string).replace(/(\n|\s)/g, '') !== '' || data[i] && (data[i].type as string) === 'element') {
3232
return element.tagName === tagName ? element : undefined
3333
}
3434
} else {
35-
const element = data[i] as ElementContent & Literal;
3635
if (!element || element.type === 'element') return;
3736
if (element.type === 'text') {
3837
const nextNode = nextChild(data, i, undefined)

test/index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ describe('rehype-attr test case', () => {
368368
markdown: '#### This is a title\n<!--rehype:className=test test2-->',
369369
expected: '<h4 class="test test2">This is a title</h4>\n<!--rehype:className=test test2-->',
370370
},
371+
{
372+
title: 'options="attr" - <li> `- list 1111`',
373+
markdown: '- hello<!--rehype:data-testid=1111-->\n- world<!--rehype:data-testid=2222-->',
374+
expected: '<ul>\n<li data-testid="1111">hello<!--rehype:data-testid=1111--></li>\n<li data-testid="2222">world<!--rehype:data-testid=2222--></li>\n</ul>',
375+
},
371376
].forEach((data, idx) => {
372377
it(data.title, async () => {
373378
const htmlStr = unified()
@@ -450,6 +455,11 @@ describe('rehype-attr test case', () => {
450455
markdown: '<blockquote>content</blockquote><!--rehype:style=color:pink;-->',
451456
expected: '<blockquote style="color:pink;">content</blockquote><!--rehype:style=color:pink;-->',
452457
},
458+
{
459+
title: 'options="attr" - <li> `- list`',
460+
markdown: '<ul>\n<li>hello world!</li><!--rehype:data-testid=111-->\n</ul>',
461+
expected: '<ul>\n<li data-testid="111">hello world!</li><!--rehype:data-testid=111-->\n</ul>',
462+
},
453463
{
454464
title: 'options="attr" - <ol>',
455465
markdown: '<ol><li>Red</li><li>Green</li></ol><!--rehype:style=color:pink;-->',

0 commit comments

Comments
 (0)