@@ -26,13 +26,11 @@ const DEFAULT_GITHUB_ICONS = {
26
26
/**
27
27
* @typedef {import("type-mdast").Root } Root
28
28
* @typedef {import("type-mdast").PhrasingContent } PhrasingContent
29
- * @typedef {import("unified").Plugin<RemarkGitHubAlertsOptions[], Root> } Plugin
29
+ * @typedef {import("unified").Plugin<[RemarkGitHubAlertsOptions], Root> } Plugin
30
+ * @typedef {import("unified").Transformer<Root, Root> } Transformer
30
31
*/
31
32
32
- /**
33
- * @param {RemarkGitHubAlertsOptions } [options = {}]
34
- * @returns {Plugin }
35
- */
33
+ /** @type {Plugin }*/
36
34
const remarkGithubAlerts = ( options = { } ) => {
37
35
const {
38
36
markers = [ 'TIP' , 'NOTE' , 'IMPORTANT' , 'WARNING' , 'CAUTION' ] ,
@@ -50,57 +48,49 @@ const remarkGithubAlerts = (options = {}) => {
50
48
matchCaseSensitive ? '' : 'i' ,
51
49
)
52
50
return ( tree ) => {
53
- visit (
54
- tree ,
55
- 'blockquote' ,
56
- (
57
- /**@type {import("type-mdast").Node }*/ node ,
58
- /**@type {Number }*/ _index ,
59
- /**@type {import('type-mdast').Parent } */ _parent ,
60
- ) => {
61
- /**@type {PhrasingContent }*/
62
- const firstContent = node . children ?. [ 0 ] . children ?. [ 0 ]
63
- if ( ! firstContent || firstContent . type !== 'text' ) return
64
- const match = firstContent . value . match ( RE )
65
- if ( ! match ) return
66
- /**@type {keyof typeof icons }*/
67
- const type = match [ 1 ] . toLowerCase ( )
68
- const title = titles [ type ] ||
69
- type . charAt ( 0 ) . toUpperCase ( ) + type . slice ( 1 )
70
- const icon = icons [ type ]
51
+ visit ( tree , 'blockquote' , ( node , _index , _parent ) => {
52
+ /**@type {PhrasingContent }*/
53
+ const firstContent = node . children ?. [ 0 ] . children ?. [ 0 ]
54
+ if ( ! firstContent || firstContent . type !== 'text' ) return
55
+ const match = firstContent . value . match ( RE )
56
+ if ( ! match ) return
57
+ /**@type {keyof typeof icons }*/
58
+ const type = match [ 1 ] . toLowerCase ( )
59
+ const title = titles [ type ] ||
60
+ type . charAt ( 0 ) . toUpperCase ( ) + type . slice ( 1 )
61
+ const icon = icons [ type ]
71
62
72
- firstContent . value = firstContent . value . slice ( match [ 0 ] . length )
73
- . trimStart ( )
74
- node . data = {
75
- hName : 'div' ,
76
- hProperties : {
77
- class : `${ classPrefix } ${ classPrefix } -${ type } ` ,
78
- } ,
79
- }
80
- node . children = [
81
- {
82
- type : 'paragraph' ,
83
- data : {
84
- hName : 'p' ,
85
- hProperties : {
86
- class : `${ classPrefix } -title` ,
87
- } ,
63
+ firstContent . value = firstContent . value . slice ( match [ 0 ] . length )
64
+ . trimStart ( )
65
+ node . data = {
66
+ hName : 'div' ,
67
+ hProperties : {
68
+ class : `${ classPrefix } ${ classPrefix } -${ type } ` ,
69
+ } ,
70
+ }
71
+ node . children = [
72
+ {
73
+ type : 'paragraph' ,
74
+ data : {
75
+ hName : 'p' ,
76
+ hProperties : {
77
+ class : `${ classPrefix } -title` ,
88
78
} ,
89
- children : [
90
- {
91
- type : 'html' ,
92
- value : icon ,
93
- } ,
94
- {
95
- type : 'text' ,
96
- value : title ,
97
- } ,
98
- ] ,
99
79
} ,
100
- ...node . children ,
101
- ]
102
- } ,
103
- )
80
+ children : [
81
+ {
82
+ type : 'html' ,
83
+ value : icon ,
84
+ } ,
85
+ {
86
+ type : 'text' ,
87
+ value : title ,
88
+ } ,
89
+ ] ,
90
+ } ,
91
+ ...node . children ,
92
+ ]
93
+ } )
104
94
return tree
105
95
}
106
96
}
0 commit comments