|
1 | 1 | 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'; |
3 | 3 | import { visit } from 'unist-util-visit'; |
4 | 4 | import { propertiesHandle, nextChild, prevChild, getCommentObject } from './utils.js'; |
5 | 5 |
|
@@ -76,18 +76,36 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?], Root> = (options = {}) => { |
76 | 76 | } |
77 | 77 | } |
78 | 78 | 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 | + }) |
86 | 88 | } |
87 | 89 | } |
88 | 90 | }); |
89 | 91 | } |
90 | 92 | } |
91 | 93 |
|
| 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 | +} |
92 | 110 |
|
93 | 111 | export default rehypeAttrs |
0 commit comments