Skip to content

Commit 1900f4a

Browse files
authored
1 parent f1b1f62 commit 1900f4a

File tree

3 files changed

+163
-61
lines changed

3 files changed

+163
-61
lines changed

index.js

Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,86 +4,49 @@ module.exports = marker
44

55
var whiteSpaceExpression = /\s+/g
66

7-
// Expression for parsing parameters.
8-
var parametersExpression = new RegExp(
9-
'\\s+' +
10-
'(' +
11-
'[-a-z0-9_]+' +
12-
')' +
13-
'(?:' +
14-
'=' +
15-
'(?:' +
16-
'"' +
17-
'(' +
18-
'(?:' +
19-
'\\\\[\\s\\S]' +
20-
'|' +
21-
'[^"]' +
22-
')+' +
23-
')' +
24-
'"' +
25-
'|' +
26-
"'" +
27-
'(' +
28-
'(?:' +
29-
'\\\\[\\s\\S]' +
30-
'|' +
31-
"[^']" +
32-
')+' +
33-
')' +
34-
"'" +
35-
'|' +
36-
'(' +
37-
'(?:' +
38-
'\\\\[\\s\\S]' +
39-
'|' +
40-
'[^"\'\\s]' +
41-
')+' +
42-
')' +
43-
')' +
44-
')?',
45-
'gi'
46-
)
7+
var parametersExpression = /\s+([-a-z0-9_]+)(?:=(?:"((?:\\[\s\S]|[^"])+)"|'((?:\\[\s\S]|[^'])+)'|((?:\\[\s\S]|[^"'\s])+)))?/gi
8+
9+
var commentExpression = /\s*([a-zA-Z0-9-]+)(\s+([\s\S]*))?\s*/
4710

4811
var markerExpression = new RegExp(
49-
'(' +
50-
'\\s*' +
51-
'<!--' +
52-
'\\s*' +
53-
'([a-zA-Z0-9-]+)' +
54-
'(\\s+([\\s\\S]*?))?' +
55-
'\\s*' +
56-
'-->' +
57-
'\\s*' +
58-
')'
12+
'(\\s*<!--' + commentExpression.source + '-->\\s*)'
5913
)
6014

6115
// Parse a comment marker.
6216
function marker(node) {
17+
var type
6318
var value
6419
var match
6520
var params
6621

67-
if (!node || node.type !== 'html') {
22+
if (!node) {
23+
return null
24+
}
25+
26+
type = node.type
27+
28+
if (type !== 'html' && type !== 'comment') {
6829
return null
6930
}
7031

7132
value = node.value
72-
match = value.match(markerExpression)
33+
match = value.match(type === 'comment' ? commentExpression : markerExpression)
7334

74-
if (!match || match[1].length !== value.length) {
35+
if (!match || match[0].length !== value.length) {
7536
return null
7637
}
7738

78-
params = parameters(match[3] || '')
39+
match = match.slice(node.type === 'comment' ? 1 : 2)
40+
41+
params = parameters(match[1] || '')
7942

8043
if (!params) {
8144
return null
8245
}
8346

8447
return {
85-
name: match[2],
86-
attributes: match[4] || '',
48+
name: match[0],
49+
attributes: match[2] || '',
8750
parameters: params,
8851
node: node
8952
}

readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ console.log(marker({
2929
type: 'html',
3030
value: '<!doctype html>'
3131
}));
32+
33+
// Also supports MDX comment nodes.
34+
console.log(marker({
35+
type: 'comment',
36+
value: 'bar'
37+
}));
3238
```
3339

3440
Yields:
@@ -45,6 +51,10 @@ Yields:
4551
{ type: 'html',
4652
value: '<!--foo bar baz=12.4 qux="test test" quux=\'false\'-->' } }
4753
null
54+
{ name: 'bar',
55+
attributes: '',
56+
parameters: {},
57+
node: { type: 'comment', value: 'bar' } }
4858
```
4959

5060
## API

test.js

Lines changed: 134 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var test = require('tape')
44
var marker = require('.')
55

6-
test('normalize(value, allowApostrophes)', function(t) {
6+
test('commentMaker(node)', function(t) {
77
var node
88

99
t.equal(marker(), null, 'should work without node')
@@ -82,10 +82,7 @@ test('normalize(value, allowApostrophes)', function(t) {
8282
'marker with double quoted attributes'
8383
)
8484

85-
node = {
86-
type: 'html',
87-
value: "<!--foo bar='baz qux'-->"
88-
}
85+
node = {type: 'html', value: "<!--foo bar='baz qux'-->"}
8986

9087
t.deepEqual(
9188
marker(node),
@@ -157,3 +154,135 @@ test('normalize(value, allowApostrophes)', function(t) {
157154

158155
t.end()
159156
})
157+
158+
test('comment node', function(t) {
159+
var node
160+
161+
t.equal(
162+
marker({type: 'comment', value: ' '}),
163+
null,
164+
'should work for empty comments'
165+
)
166+
167+
node = {type: 'comment', value: 'foo'}
168+
169+
t.deepEqual(
170+
marker(node),
171+
{name: 'foo', attributes: '', parameters: {}, node: node},
172+
'comment without attributes'
173+
)
174+
175+
node = {type: 'comment', value: ' foo '}
176+
177+
t.deepEqual(
178+
marker(node),
179+
{name: 'foo', attributes: '', parameters: {}, node: node},
180+
'comment without attributes ignoring spaces'
181+
)
182+
183+
node = {type: 'comment', value: 'foo bar'}
184+
185+
t.deepEqual(
186+
marker(node),
187+
{name: 'foo', attributes: 'bar', parameters: {bar: true}, node: node},
188+
'comment with boolean attributes'
189+
)
190+
191+
node = {type: 'comment', value: 'foo bar=baz qux'}
192+
193+
t.deepEqual(
194+
marker(node),
195+
{
196+
name: 'foo',
197+
attributes: 'bar=baz qux',
198+
parameters: {bar: 'baz', qux: true},
199+
node: node
200+
},
201+
'comment with unquoted attributes'
202+
)
203+
204+
node = {type: 'comment', value: 'foo bar="baz qux"'}
205+
206+
t.deepEqual(
207+
marker(node),
208+
{
209+
name: 'foo',
210+
attributes: 'bar="baz qux"',
211+
parameters: {bar: 'baz qux'},
212+
node: node
213+
},
214+
'comment with double quoted attributes'
215+
)
216+
217+
node = {type: 'comment', value: "foo bar='baz qux'"}
218+
219+
t.deepEqual(
220+
marker(node),
221+
{
222+
name: 'foo',
223+
attributes: "bar='baz qux'",
224+
parameters: {bar: 'baz qux'},
225+
node: node
226+
},
227+
'comment with single quoted attributes'
228+
)
229+
230+
node = {type: 'comment', value: 'foo bar=3'}
231+
232+
t.deepEqual(
233+
marker(node),
234+
{
235+
name: 'foo',
236+
attributes: 'bar=3',
237+
parameters: {bar: 3},
238+
node: node
239+
},
240+
'comment with numbers'
241+
)
242+
243+
node = {type: 'comment', value: 'foo bar=true'}
244+
245+
t.deepEqual(
246+
marker(node),
247+
{
248+
name: 'foo',
249+
attributes: 'bar=true',
250+
parameters: {bar: true},
251+
node: node
252+
},
253+
'comment with boolean true'
254+
)
255+
256+
node = {type: 'comment', value: 'foo bar=false'}
257+
258+
t.deepEqual(
259+
marker(node),
260+
{
261+
name: 'foo',
262+
attributes: 'bar=false',
263+
parameters: {bar: false},
264+
node: node
265+
},
266+
'comment with boolean false'
267+
)
268+
269+
t.equal(
270+
marker({type: 'comment', value: 'foo bar='}),
271+
null,
272+
'marker stop for invalid parameters (#1)'
273+
)
274+
275+
t.equal(
276+
marker({type: 'comment', value: 'foo bar= qux'}),
277+
null,
278+
'marker stop for invalid parameters (#2)'
279+
)
280+
281+
t.equal(
282+
marker({type: 'comment', value: 'foo |'}),
283+
null,
284+
'marker stop for invalid parameters (#3)'
285+
)
286+
287+
t.end()
288+
})

0 commit comments

Comments
 (0)