@@ -11,9 +11,7 @@ export type RehypeAttrsOptions = {
11
11
* text
12
12
* <!--rehype:title=Rehype Attrs&abc=2-->
13
13
* ```
14
- *
15
- * ⇣⇣⇣⇣⇣⇣
16
- *
14
+ * 👇👇👇👇👇
17
15
* ```html
18
16
* <p data-config="data-config='[object Object]'">text</p>
19
17
* ```
@@ -25,7 +23,7 @@ export type RehypeAttrsOptions = {
25
23
* <!--rehype:title=Rehype Attrs-->
26
24
* ```
27
25
*
28
- * ⇣⇣⇣⇣⇣⇣
26
+ * 👇👇👇👇👇
29
27
*
30
28
* ```html
31
29
* <p data-config="{"title":"Rehype Attrs","rehyp":true}">text</p>
@@ -37,31 +35,32 @@ export type RehypeAttrsOptions = {
37
35
* text
38
36
* <!--rehype:title=Rehype Attrs-->
39
37
* ```
40
- * ⇣⇣⇣⇣⇣⇣
38
+ * 👇👇👇👇👇
41
39
* ```html
42
40
* <p title="Rehype Attrs">text</p>
43
41
* ```
42
+ * @default `data`
44
43
*/
45
- properties : 'data' | 'string' | 'attr' ;
46
- }
47
-
48
- const defaultOptions : RehypeAttrsOptions = {
49
- properties : 'data' ,
44
+ properties ? : 'data' | 'string' | 'attr' ;
45
+ /**
46
+ * Code block passing parameters
47
+ */
48
+ codeBlockParames ?: boolean ;
50
49
}
51
50
52
- const rehypeAttrs : Plugin < [ RehypeAttrsOptions ?] , Root > = ( options ) => {
53
- const opts = { ... defaultOptions , ... options }
51
+ const rehypeAttrs : Plugin < [ RehypeAttrsOptions ?] , Root > = ( options = { } ) => {
52
+ const { properties = 'data' , codeBlockParames = true } = options ;
54
53
return ( tree ) => {
55
54
visit ( tree , 'element' , ( node , index , parent ) => {
56
- if ( node . tagName === 'pre' && node && Array . isArray ( node . children ) && parent && Array . isArray ( parent . children ) && parent . children . length > 1 ) {
55
+ if ( codeBlockParames && node . tagName === 'pre' && node && Array . isArray ( node . children ) && parent && Array . isArray ( parent . children ) && parent . children . length > 1 ) {
57
56
const firstChild = node . children [ 0 ] as Element ;
58
57
if ( firstChild && firstChild . tagName === 'code' && typeof index === 'number' ) {
59
58
const child = prevChild ( parent . children as Literal [ ] , index ) ;
60
59
if ( child ) {
61
60
const attr = getCommentObject ( child ) ;
62
61
if ( Object . keys ( attr ) . length > 0 ) {
63
62
node . properties = { ...node . properties , ...{ 'data-type' : 'rehyp' } }
64
- firstChild . properties = propertiesHandle ( firstChild . properties , attr , opts . properties ) as Properties
63
+ firstChild . properties = propertiesHandle ( firstChild . properties , attr , properties ) as Properties
65
64
}
66
65
}
67
66
}
@@ -72,7 +71,7 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?], Root> = (options) => {
72
71
if ( child ) {
73
72
const attr = getCommentObject ( child as Comment )
74
73
if ( Object . keys ( attr ) . length > 0 ) {
75
- node . properties = propertiesHandle ( node . properties , attr , opts . properties ) as Properties
74
+ node . properties = propertiesHandle ( node . properties , attr , properties ) as Properties
76
75
}
77
76
}
78
77
}
0 commit comments