|
1 | 1 | /** |
2 | | - * @typedef {import('mdast').Root} Root |
3 | | - * @typedef {import('mdast').Content} Content |
4 | | - * @typedef {import('mdast').HTML} HTML |
5 | | - * @typedef {import('mdast-util-mdx-expression').MDXFlowExpression} MDXFlowExpression |
6 | | - * @typedef {import('mdast-util-mdx-expression').MDXTextExpression} MDXTextExpression |
7 | | - * @typedef {Root|Content} Node |
8 | | - * |
9 | | - * @typedef {string|number|boolean} MarkerParameterValue |
10 | | - * @typedef {Record<string, MarkerParameterValue>} MarkerParameters |
11 | | - * |
12 | | - * @typedef Mdx1CommentNode |
13 | | - * @property {'comment'} type |
14 | | - * @property {string} value |
15 | | - * |
16 | | - * @typedef Marker |
17 | | - * Comment marker. |
18 | | - * @property {string} name |
19 | | - * Name of marker. |
20 | | - * @property {string} attributes |
21 | | - * Value after name. |
22 | | - * @property {MarkerParameters|null} parameters |
23 | | - * Parsed attributes, with decimal numbers, `true`, and `false` are casted to |
24 | | - * numbers and booleans. |
25 | | - * @property {HTML|Mdx1CommentNode|MDXFlowExpression|MDXTextExpression} node |
26 | | - * Reference to given node. |
| 2 | + * @typedef {import('./lib/index.js').Marker} Marker |
| 3 | + * @typedef {import('./lib/index.js').MarkerParameterValue} MarkerParameterValue |
| 4 | + * @typedef {import('./lib/index.js').MarkerParameters} MarkerParameters |
27 | 5 | */ |
28 | 6 |
|
29 | | -const commentExpression = /\s*([a-zA-Z\d-]+)(\s+([\s\S]*))?\s*/ |
30 | | -const esCommentExpression = new RegExp( |
31 | | - '(\\s*\\/\\*' + commentExpression.source + '\\*\\/\\s*)' |
32 | | -) |
33 | | -const markerExpression = new RegExp( |
34 | | - '(\\s*<!--' + commentExpression.source + '-->\\s*)' |
35 | | -) |
36 | | - |
37 | | -/** |
38 | | - * Parse a comment marker. |
39 | | - * |
40 | | - * @param {unknown} value |
41 | | - * `Node` to parse. |
42 | | - * @returns {Marker|null} |
43 | | - * Information, when applicable. |
44 | | - */ |
45 | | -export function commentMarker(value) { |
46 | | - if ( |
47 | | - isNode(value) && |
48 | | - (value.type === 'html' || |
49 | | - // @ts-expect-error: MDX@1 |
50 | | - value.type === 'comment' || |
51 | | - value.type === 'mdxFlowExpression' || |
52 | | - value.type === 'mdxTextExpression') |
53 | | - ) { |
54 | | - let offset = 2 |
55 | | - /** @type {RegExpMatchArray|null|undefined} */ |
56 | | - let match |
57 | | - |
58 | | - // @ts-expect-error: MDX@1 |
59 | | - if (value.type === 'comment') { |
60 | | - // @ts-expect-error: MDX@1 |
61 | | - match = value.value.match(commentExpression) |
62 | | - offset = 1 |
63 | | - } else if (value.type === 'html') { |
64 | | - match = value.value.match(markerExpression) |
65 | | - } else if ( |
66 | | - value.type === 'mdxFlowExpression' || |
67 | | - value.type === 'mdxTextExpression' |
68 | | - ) { |
69 | | - match = value.value.match(esCommentExpression) |
70 | | - } |
71 | | - |
72 | | - if (match && match[0].length === value.value.length) { |
73 | | - const parameters = parseParameters(match[offset + 1] || '') |
74 | | - |
75 | | - if (parameters) { |
76 | | - return { |
77 | | - name: match[offset], |
78 | | - attributes: (match[offset + 2] || '').trim(), |
79 | | - parameters, |
80 | | - node: value |
81 | | - } |
82 | | - } |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - return null |
87 | | -} |
88 | | - |
89 | | -/** |
90 | | - * Parse `value` into an object. |
91 | | - * |
92 | | - * @param {string} value |
93 | | - * @returns {MarkerParameters|null} |
94 | | - */ |
95 | | -function parseParameters(value) { |
96 | | - /** @type {MarkerParameters} */ |
97 | | - const parameters = {} |
98 | | - |
99 | | - return value |
100 | | - .replace( |
101 | | - /\s+([-\w]+)(?:=(?:"((?:\\[\s\S]|[^"])+)"|'((?:\\[\s\S]|[^'])+)'|((?:\\[\s\S]|[^"'\s])+)))?/gi, |
102 | | - replacer |
103 | | - ) |
104 | | - .replace(/\s+/g, '') |
105 | | - ? null |
106 | | - : parameters |
107 | | - |
108 | | - /** |
109 | | - * @param {string} _ |
110 | | - * @param {string} $1 |
111 | | - * @param {string} $2 |
112 | | - * @param {string} $3 |
113 | | - * @param {string} $4 |
114 | | - */ |
115 | | - // eslint-disable-next-line max-params |
116 | | - function replacer(_, $1, $2, $3, $4) { |
117 | | - /** @type {MarkerParameterValue} */ |
118 | | - let value = $2 || $3 || $4 || '' |
119 | | - |
120 | | - if (value === 'true' || value === '') { |
121 | | - value = true |
122 | | - } else if (value === 'false') { |
123 | | - value = false |
124 | | - } else if (!Number.isNaN(Number(value))) { |
125 | | - value = Number(value) |
126 | | - } |
127 | | - |
128 | | - parameters[$1] = value |
129 | | - |
130 | | - return '' |
131 | | - } |
132 | | -} |
133 | | - |
134 | | -/** |
135 | | - * @param {unknown} value |
136 | | - * @returns {value is Node} |
137 | | - */ |
138 | | -function isNode(value) { |
139 | | - return Boolean(value && typeof value === 'object' && 'type' in value) |
140 | | -} |
| 7 | +export {commentMarker} from './lib/index.js' |
0 commit comments