1
1
/**
2
2
* @typedef {string|number|boolean } MarkerParameterValue
3
+ * @typedef {import('mdast').Root } Root
4
+ * @typedef {import('mdast').Content } Content
5
+ * @typedef {import('mdast').HTML } HTML
6
+ * @typedef {import('mdast-util-mdx-expression').MDXFlowExpression } MDXFlowExpression
7
+ * @typedef {import('mdast-util-mdx-expression').MDXTextExpression } MDXTextExpression
8
+ * @typedef {Root|Content } Node
3
9
* @typedef {Object.<string, MarkerParameterValue> } MarkerParameters
4
10
*
5
- * @typedef HtmlNode
6
- * @property {'html' } type
7
- * @property {string } value
8
- *
9
- * @typedef CommentNode
11
+ * @typedef Mdx1CommentNode
10
12
* @property {'comment' } type
11
13
* @property {string } value
12
14
*
13
15
* @typedef Marker
14
16
* @property {string } name
15
17
* @property {string } attributes
16
18
* @property {MarkerParameters|null } parameters
17
- * @property {HtmlNode|CommentNode } node
19
+ * @property {HTML|Mdx1CommentNode|MDXFlowExpression|MDXTextExpression } node
18
20
*/
19
21
20
22
const commentExpression = / \s * ( [ a - z A - Z \d - ] + ) ( \s + ( [ \s \S ] * ) ) ? \s * /
21
-
23
+ const esCommentExpression = new RegExp (
24
+ '(\\s*\\/\\*' + commentExpression . source + '\\*\\/\\s*)'
25
+ )
22
26
const markerExpression = new RegExp (
23
27
'(\\s*<!--' + commentExpression . source + '-->\\s*)'
24
28
)
@@ -29,19 +33,39 @@ const markerExpression = new RegExp(
29
33
* @returns {Marker|null }
30
34
*/
31
35
export function commentMarker ( value ) {
32
- if ( applicable ( value ) ) {
33
- const match = value . value . match (
34
- value . type === 'comment' ? commentExpression : markerExpression
35
- )
36
+ if (
37
+ isNode ( value ) &&
38
+ ( value . type === 'html' ||
39
+ // @ts -expect-error: MDX@1
40
+ value . type === 'comment' ||
41
+ value . type === 'mdxFlowExpression' ||
42
+ value . type === 'mdxTextExpression' )
43
+ ) {
44
+ let offset = 2
45
+ /** @type {RegExpMatchArray|null|undefined } */
46
+ let match
47
+
48
+ // @ts -expect-error: MDX@1
49
+ if ( value . type === 'comment' ) {
50
+ // @ts -expect-error: MDX@1
51
+ match = value . value . match ( commentExpression )
52
+ offset = 1
53
+ } else if ( value . type === 'html' ) {
54
+ match = value . value . match ( markerExpression )
55
+ } else if (
56
+ value . type === 'mdxFlowExpression' ||
57
+ value . type === 'mdxTextExpression'
58
+ ) {
59
+ match = value . value . match ( esCommentExpression )
60
+ }
36
61
37
62
if ( match && match [ 0 ] . length === value . value . length ) {
38
- const offset = value . type === 'comment' ? 1 : 2
39
63
const parameters = parseParameters ( match [ offset + 1 ] || '' )
40
64
41
65
if ( parameters ) {
42
66
return {
43
67
name : match [ offset ] ,
44
- attributes : match [ offset + 2 ] || '' ,
68
+ attributes : ( match [ offset + 2 ] || '' ) . trim ( ) ,
45
69
parameters,
46
70
node : value
47
71
}
@@ -99,14 +123,8 @@ function parseParameters(value) {
99
123
100
124
/**
101
125
* @param {unknown } value
102
- * @returns {value is HtmlNode | CommentNode }
126
+ * @returns {value is Node }
103
127
*/
104
- function applicable ( value ) {
105
- return Boolean (
106
- value &&
107
- typeof value === 'object' &&
108
- 'type' in value &&
109
- // @ts -expect-error hush
110
- ( value . type === 'html' || value . type === 'comment' )
111
- )
128
+ function isNode ( value ) {
129
+ return Boolean ( value && typeof value === 'object' && 'type' in value )
112
130
}
0 commit comments