Skip to content

Add support for MDX comment node #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 19 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,49 @@ module.exports = marker

var whiteSpaceExpression = /\s+/g

// Expression for parsing parameters.
var parametersExpression = new RegExp(
'\\s+' +
'(' +
'[-a-z0-9_]+' +
')' +
'(?:' +
'=' +
'(?:' +
'"' +
'(' +
'(?:' +
'\\\\[\\s\\S]' +
'|' +
'[^"]' +
')+' +
')' +
'"' +
'|' +
"'" +
'(' +
'(?:' +
'\\\\[\\s\\S]' +
'|' +
"[^']" +
')+' +
')' +
"'" +
'|' +
'(' +
'(?:' +
'\\\\[\\s\\S]' +
'|' +
'[^"\'\\s]' +
')+' +
')' +
')' +
')?',
'gi'
)
var parametersExpression = /\s+([-a-z0-9_]+)(?:=(?:"((?:\\[\s\S]|[^"])+)"|'((?:\\[\s\S]|[^'])+)'|((?:\\[\s\S]|[^"'\s])+)))?/gi

var commentExpression = /\s*([a-zA-Z0-9-]+)(\s+([\s\S]*))?\s*/

var markerExpression = new RegExp(
'(' +
'\\s*' +
'<!--' +
'\\s*' +
'([a-zA-Z0-9-]+)' +
'(\\s+([\\s\\S]*?))?' +
'\\s*' +
'-->' +
'\\s*' +
')'
'(\\s*<!--' + commentExpression.source + '-->\\s*)'
)

// Parse a comment marker.
function marker(node) {
var type
var value
var match
var params

if (!node || node.type !== 'html') {
if (!node) {
return null
}

type = node.type

if (type !== 'html' && type !== 'comment') {
return null
}

value = node.value
match = value.match(markerExpression)
match = value.match(type === 'comment' ? commentExpression : markerExpression)

if (!match || match[1].length !== value.length) {
if (!match || match[0].length !== value.length) {
return null
}

params = parameters(match[3] || '')
match = match.slice(node.type === 'comment' ? 1 : 2)

params = parameters(match[1] || '')

if (!params) {
return null
}

return {
name: match[2],
attributes: match[4] || '',
name: match[0],
attributes: match[2] || '',
parameters: params,
node: node
}
Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ console.log(marker({
type: 'html',
value: '<!doctype html>'
}));

// Also supports MDX comment nodes.
console.log(marker({
type: 'comment',
value: 'bar'
}));
```

Yields:
Expand All @@ -45,6 +51,10 @@ Yields:
{ type: 'html',
value: '<!--foo bar baz=12.4 qux="test test" quux=\'false\'-->' } }
null
{ name: 'bar',
attributes: '',
parameters: {},
node: { type: 'comment', value: 'bar' } }
```

## API
Expand Down
139 changes: 134 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var test = require('tape')
var marker = require('.')

test('normalize(value, allowApostrophes)', function(t) {
test('commentMaker(node)', function(t) {
var node

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

node = {
type: 'html',
value: "<!--foo bar='baz qux'-->"
}
node = {type: 'html', value: "<!--foo bar='baz qux'-->"}

t.deepEqual(
marker(node),
Expand Down Expand Up @@ -157,3 +154,135 @@ test('normalize(value, allowApostrophes)', function(t) {

t.end()
})

test('comment node', function(t) {
var node

t.equal(
marker({type: 'comment', value: ' '}),
null,
'should work for empty comments'
)

node = {type: 'comment', value: 'foo'}

t.deepEqual(
marker(node),
{name: 'foo', attributes: '', parameters: {}, node: node},
'comment without attributes'
)

node = {type: 'comment', value: ' foo '}

t.deepEqual(
marker(node),
{name: 'foo', attributes: '', parameters: {}, node: node},
'comment without attributes ignoring spaces'
)

node = {type: 'comment', value: 'foo bar'}

t.deepEqual(
marker(node),
{name: 'foo', attributes: 'bar', parameters: {bar: true}, node: node},
'comment with boolean attributes'
)

node = {type: 'comment', value: 'foo bar=baz qux'}

t.deepEqual(
marker(node),
{
name: 'foo',
attributes: 'bar=baz qux',
parameters: {bar: 'baz', qux: true},
node: node
},
'comment with unquoted attributes'
)

node = {type: 'comment', value: 'foo bar="baz qux"'}

t.deepEqual(
marker(node),
{
name: 'foo',
attributes: 'bar="baz qux"',
parameters: {bar: 'baz qux'},
node: node
},
'comment with double quoted attributes'
)

node = {type: 'comment', value: "foo bar='baz qux'"}

t.deepEqual(
marker(node),
{
name: 'foo',
attributes: "bar='baz qux'",
parameters: {bar: 'baz qux'},
node: node
},
'comment with single quoted attributes'
)

node = {type: 'comment', value: 'foo bar=3'}

t.deepEqual(
marker(node),
{
name: 'foo',
attributes: 'bar=3',
parameters: {bar: 3},
node: node
},
'comment with numbers'
)

node = {type: 'comment', value: 'foo bar=true'}

t.deepEqual(
marker(node),
{
name: 'foo',
attributes: 'bar=true',
parameters: {bar: true},
node: node
},
'comment with boolean true'
)

node = {type: 'comment', value: 'foo bar=false'}

t.deepEqual(
marker(node),
{
name: 'foo',
attributes: 'bar=false',
parameters: {bar: false},
node: node
},
'comment with boolean false'
)

t.equal(
marker({type: 'comment', value: 'foo bar='}),
null,
'marker stop for invalid parameters (#1)'
)

t.equal(
marker({type: 'comment', value: 'foo bar= qux'}),
null,
'marker stop for invalid parameters (#2)'
)

t.equal(
marker({type: 'comment', value: 'foo |'}),
null,
'marker stop for invalid parameters (#3)'
)

t.end()
})