Skip to content

Commit 2d4a93d

Browse files
committed
Refactor code-style
1 parent 85d40e7 commit 2d4a93d

File tree

5 files changed

+307
-309
lines changed

5 files changed

+307
-309
lines changed

.editorconfig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ root = true
22

33
[*]
44
indent_style = space
5-
indent_size = 4
5+
indent_size = 2
66
end_of_line = lf
77
charset = utf-8
88
trim_trailing_whitespace = true
99
insert_final_newline = true
10-
11-
[*.{json,remarkrc,eslintrc,sh}]
12-
indent_size = 2
13-
14-
[*.md]
15-
trim_trailing_whitespace = false

index.js

Lines changed: 90 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,116 @@
1-
/**
2-
* @author Titus Wormer
3-
* @copyright 2016 Titus Wormer
4-
* @license MIT
5-
* @module mdast:comment-marker
6-
* @fileoverview Parse a comment marker.
7-
*/
8-
91
'use strict';
102

11-
/* eslint-env commonjs */
3+
/* Expose. */
4+
module.exports = marker;
125

6+
/* HTML type. */
137
var T_HTML = 'html';
148

15-
/*
16-
* Expression for parsing parameters.
17-
*/
18-
9+
/* Expression for parsing parameters. */
1910
var PARAMETERS = new RegExp(
20-
'\\s+' +
21-
'(' +
22-
'[-a-z0-9_]+' +
23-
')' +
11+
'\\s+' +
12+
'(' +
13+
'[-a-z0-9_]+' +
14+
')' +
15+
'(?:' +
16+
'=' +
2417
'(?:' +
25-
'=' +
18+
'"' +
19+
'(' +
20+
'(?:' +
21+
'\\\\[\\s\\S]' +
22+
'|' +
23+
'[^"]' +
24+
')+' +
25+
')' +
26+
'"' +
27+
'|' +
28+
'\'' +
29+
'(' +
2630
'(?:' +
27-
'"' +
28-
'(' +
29-
'(?:' +
30-
'\\\\[\\s\\S]' +
31-
'|' +
32-
'[^"]' +
33-
')+' +
34-
')' +
35-
'"' +
36-
'|' +
37-
'\'' +
38-
'(' +
39-
'(?:' +
40-
'\\\\[\\s\\S]' +
41-
'|' +
42-
'[^\']' +
43-
')+' +
44-
')' +
45-
'\'' +
46-
'|' +
47-
'(' +
48-
'(?:' +
49-
'\\\\[\\s\\S]' +
50-
'|' +
51-
'[^"\'\\s]' +
52-
')+' +
53-
')' +
54-
')' +
55-
')?',
56-
'gi'
31+
'\\\\[\\s\\S]' +
32+
'|' +
33+
'[^\']' +
34+
')+' +
35+
')' +
36+
'\'' +
37+
'|' +
38+
'(' +
39+
'(?:' +
40+
'\\\\[\\s\\S]' +
41+
'|' +
42+
'[^"\'\\s]' +
43+
')+' +
44+
')' +
45+
')' +
46+
')?',
47+
'gi'
5748
);
5849

5950
var MARKER = new RegExp(
60-
'(' +
61-
'\\s*' +
62-
'<!--' +
63-
'\\s*' +
64-
'([a-zA-Z0-9-]+)' +
65-
'(\\s+([\\s\\S]*?))?' +
66-
'\\s*' +
67-
'-->' +
68-
'\\s*' +
69-
')'
51+
'(' +
52+
'\\s*' +
53+
'<!--' +
54+
'\\s*' +
55+
'([a-zA-Z0-9-]+)' +
56+
'(\\s+([\\s\\S]*?))?' +
57+
'\\s*' +
58+
'-->' +
59+
'\\s*' +
60+
')'
7061
);
7162

72-
/**
73-
* Parse `value` into an object.
74-
*
75-
* @param {string} value - HTML comment.
76-
* @return {Object} - Parsed parameters.
77-
*/
78-
function parameters(value) {
79-
var attributes = {};
80-
var rest = value.replace(PARAMETERS, function ($0, $1, $2, $3, $4) {
81-
var result = $2 || $3 || $4 || '';
63+
/* Parse a comment marker */
64+
function marker(node) {
65+
var value;
66+
var match;
67+
var params;
8268

83-
if (result === 'true' || result === '') {
84-
result = true;
85-
} else if (result === 'false') {
86-
result = false;
87-
} else if (!isNaN(result)) {
88-
result = Number(result);
89-
}
69+
if (!node || node.type !== T_HTML) {
70+
return null;
71+
}
9072

91-
attributes[$1] = result;
73+
value = node.value;
74+
match = value.match(MARKER);
9275

93-
return '';
94-
});
76+
if (!match || match[1].length !== value.length) {
77+
return null;
78+
}
9579

96-
return rest ? null : attributes;
97-
}
80+
params = parameters(match[3] || '');
9881

99-
/*
100-
* Constants.
101-
*/
82+
if (!params) {
83+
return null;
84+
}
10285

103-
/**
104-
* Parse a comment marker
105-
*
106-
* @param {Node} node - Node to parse.
107-
* @return {Object?} - Marker information.
108-
*/
109-
function marker(node) {
110-
var value;
111-
var match;
112-
var params;
113-
114-
if (!node || node.type !== T_HTML) {
115-
return null;
116-
}
86+
return {
87+
name: match[2],
88+
attributes: match[4] || '',
89+
parameters: params,
90+
node: node
91+
};
92+
}
11793

118-
value = node.value;
119-
match = value.match(MARKER);
94+
/* eslint-disable max-params */
12095

121-
if (!match || match[1].length !== value.length) {
122-
return null;
96+
/* Parse `value` into an object. */
97+
function parameters(value) {
98+
var attributes = {};
99+
var rest = value.replace(PARAMETERS, function ($0, $1, $2, $3, $4) {
100+
var result = $2 || $3 || $4 || '';
101+
102+
if (result === 'true' || result === '') {
103+
result = true;
104+
} else if (result === 'false') {
105+
result = false;
106+
} else if (!isNaN(result)) {
107+
result = Number(result);
123108
}
124109

125-
params = parameters(match[3] || '');
110+
attributes[$1] = result;
126111

127-
if (!params) {
128-
return null;
129-
}
112+
return '';
113+
});
130114

131-
return {
132-
'name': match[2],
133-
'attributes': match[4] || '',
134-
'parameters': params,
135-
'node': node
136-
};
115+
return rest ? null : attributes;
137116
}
138-
139-
/*
140-
* Expose.
141-
*/
142-
143-
module.exports = marker;

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,23 @@
3737
"remark-usage": "^4.0.0",
3838
"remark-validate-links": "^4.0.0",
3939
"remark-yaml-config": "^3.0.0",
40-
"tape": "^4.0.0"
40+
"tape": "^4.0.0",
41+
"xo": "^0.17.0"
4142
},
4243
"scripts": {
4344
"build-md": "remark . --quiet --frail",
4445
"build-bundle": "browserify index.js --bare -s mdastCommentMarker > mdast-comment-marker.js",
4546
"build-mangle": "esmangle mdast-comment-marker.js > mdast-comment-marker.min.js",
4647
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
47-
"lint-api": "eslint .",
48-
"lint-style": "jscs --reporter inline .",
49-
"lint": "npm run lint-api && npm run lint-style",
48+
"lint": "xo",
5049
"test-api": "node test.js",
5150
"test-coverage": "istanbul cover test.js",
5251
"test": "npm run build && npm run lint && npm run test-coverage"
52+
},
53+
"xo": {
54+
"space": true,
55+
"ignore": [
56+
"mdast-comment-marker.js"
57+
]
5358
}
5459
}

readme.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# mdast-comment-marker [![Build Status][build-badge]][build-status] [![Coverage Status][coverage-badge]][coverage-status] [![Chat][chat-badge]][chat]
22

3-
<!--lint disable list-item-spacing heading-increment list-item-indent-->
4-
53
Parse an [MDAST][] comment marker.
64

75
## Installation
@@ -19,14 +17,10 @@ globals module, [uncompressed and compressed][releases].
1917

2018
```javascript
2119
var marker = require('mdast-comment-marker');
22-
```
2320

24-
A simple marker:
25-
26-
```javascript
2721
var result = marker({
28-
'type': 'html',
29-
'value': '<!--foo-->'
22+
type: 'html',
23+
value: '<!--foo-->'
3024
});
3125
```
3226

@@ -48,8 +42,8 @@ Parameters:
4842

4943
```javascript
5044
result = marker({
51-
'type': 'html',
52-
'value': '<!--foo bar baz=12.4 qux="test test" quux=\'false\'-->'
45+
type: 'html',
46+
value: '<!--foo bar baz=12.4 qux="test test" quux=\'false\'-->'
5347
});
5448
```
5549

@@ -76,8 +70,8 @@ Non-markers:
7670

7771
```javascript
7872
result = marker({
79-
'type': 'html',
80-
'value': '<!doctype html>'
73+
type: 'html',
74+
value: '<!doctype html>'
8175
});
8276
```
8377

0 commit comments

Comments
 (0)