Skip to content

Commit 9dbdb19

Browse files
committed
Refactor code-style
1 parent e3fc4b7 commit 9dbdb19

File tree

6 files changed

+152
-138
lines changed

6 files changed

+152
-138
lines changed

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
retext-syntax-urls.js
3+
retext-syntax-urls.min.js

index.js

+55-55
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,130 @@
1-
'use strict';
1+
'use strict'
22

3-
var position = require('unist-util-position');
4-
var modifyChildren = require('unist-util-modify-children');
5-
var toString = require('nlcst-to-string');
3+
var position = require('unist-util-position')
4+
var modifyChildren = require('unist-util-modify-children')
5+
var toString = require('nlcst-to-string')
66

7-
module.exports = urls;
7+
module.exports = urls
88

9-
var slashes = /^\/{1,3}$/;
9+
var slashes = /^\/{1,3}$/
1010

1111
function urls() {
12-
this.Parser.prototype.useFirst('tokenizeSentence', modifyChildren(mergeLinks));
12+
this.Parser.prototype.useFirst('tokenizeSentence', modifyChildren(mergeLinks))
1313
}
1414

1515
/* eslint-disable complexity */
1616
function mergeLinks(child, index, parent) {
17-
var siblings = parent.children;
18-
var nodes = [child];
19-
var start = index;
20-
var end = index;
21-
var currentIndex = index;
22-
var value;
23-
var type;
24-
var initial;
25-
var final;
17+
var siblings = parent.children
18+
var nodes = [child]
19+
var start = index
20+
var end = index
21+
var currentIndex = index
22+
var value
23+
var type
24+
var initial
25+
var final
2626

2727
if (!puncOrSymbol(child.type) || toString(child) !== '.') {
28-
return;
28+
return
2929
}
3030

31-
/* Find preceding word/punctuation. Stop before slashes, break after `www`. */
31+
// Find preceding word/punctuation. Stop before slashes, break after `www`.
3232
while (siblings[start - 1]) {
33-
type = siblings[start - 1].type;
33+
type = siblings[start - 1].type
3434

3535
if (!applicable(type)) {
36-
break;
36+
break
3737
}
3838

3939
if (puncOrSymbol(type) && slashes.test(toString(siblings[start - 1]))) {
40-
break;
40+
break
4141
}
4242

43-
start--;
43+
start--
4444

45-
nodes.unshift(siblings[start]);
45+
nodes.unshift(siblings[start])
4646

4747
if (type === 'WordNode' && toString(siblings[start]) === 'www') {
48-
break;
48+
break
4949
}
5050
}
5151

52-
/* Find following word/punctuation. */
52+
// Find following word/punctuation.
5353
while (siblings[end + 1]) {
54-
type = siblings[end + 1].type;
54+
type = siblings[end + 1].type
5555

5656
if (!applicable(type)) {
57-
break;
57+
break
5858
}
5959

60-
end++;
61-
nodes.push(siblings[end]);
60+
end++
61+
nodes.push(siblings[end])
6262
}
6363

64-
/* This full stop doesnt look like a link: it’s either not followed,
65-
* or not preceded, by words or punctuation. */
64+
// This full stop doesnt look like a link: it’s either not followed, or not
65+
// preceded, by words or punctuation.
6666
if (currentIndex === start || currentIndex === end) {
67-
return;
67+
return
6868
}
6969

70-
/* 1-3 slashes. */
70+
// 1-3 slashes.
7171
if (
7272
start > 0 &&
7373
puncOrSymbol(siblings[start - 1].type) &&
7474
slashes.test(toString(siblings[start - 1]))
7575
) {
76-
start--;
77-
nodes.unshift(siblings[start]);
76+
start--
77+
nodes.unshift(siblings[start])
7878
}
7979

80-
/* URL protocol and colon. */
80+
// URL protocol and colon.
8181
if (
8282
start > 2 &&
8383
puncOrSymbol(siblings[start - 1].type) &&
8484
toString(siblings[start - 1]) === ':' &&
8585
siblings[start - 2].type === 'WordNode'
8686
) {
87-
nodes.unshift(siblings[start - 2], siblings[start - 1]);
88-
start -= 2;
87+
nodes.unshift(siblings[start - 2], siblings[start - 1])
88+
start -= 2
8989
}
9090

91-
value = null;
91+
value = null
9292

93-
/* Remove the last node if it's punctuation, unless it's `/` or `)`. */
93+
// Remove the last node if it's punctuation, unless it's `/` or `)`.
9494
if (puncOrSymbol(siblings[end].type)) {
95-
value = toString(siblings[end]);
95+
value = toString(siblings[end])
9696

9797
if (value !== '/' && value !== ')') {
98-
end--;
99-
nodes.pop();
98+
end--
99+
nodes.pop()
100100
}
101101
}
102102

103-
child = {type: 'SourceNode', value: toString(nodes)};
104-
initial = position.start(nodes[0]);
105-
final = position.end(nodes[nodes.length - 1]);
103+
child = {type: 'SourceNode', value: toString(nodes)}
104+
initial = position.start(nodes[0])
105+
final = position.end(nodes[nodes.length - 1])
106106

107107
if (initial.line && final.line) {
108-
child.position = {start: initial, end: final};
108+
child.position = {start: initial, end: final}
109109
}
110110

111-
/* Remove the nodes and insert a SourceNode. */
112-
siblings.splice(start, end - start + 1, child);
111+
// Remove the nodes and insert a SourceNode.
112+
siblings.splice(start, end - start + 1, child)
113113

114-
index++;
114+
index++
115115

116-
/* Ignore the following full-stop: it's not part of a link. */
116+
// Ignore the following full-stop: it's not part of a link.
117117
if (value === '.') {
118-
index++;
118+
index++
119119
}
120120

121-
return index;
121+
return index
122122
}
123123

124124
function applicable(type) {
125-
return type === 'WordNode' || puncOrSymbol(type);
125+
return type === 'WordNode' || puncOrSymbol(type)
126126
}
127127

128128
function puncOrSymbol(type) {
129-
return type === 'PunctuationNode' || type === 'SymbolNode';
129+
return type === 'PunctuationNode' || type === 'SymbolNode'
130130
}

package.json

+13-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"is-hidden": "^1.1.1",
3232
"not": "^0.1.0",
3333
"nyc": "^13.0.0",
34+
"prettier": "^1.14.3",
3435
"remark-cli": "^6.0.0",
3536
"remark-preset-wooorm": "^4.0.0",
3637
"retext": "^5.0.0",
@@ -40,23 +41,30 @@
4041
"xo": "^0.23.0"
4142
},
4243
"scripts": {
43-
"build-md": "remark . -qfo",
44+
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
4445
"build-bundle": "browserify index.js --bare -s retextSyntaxUrls > retext-syntax-urls.js",
4546
"build-mangle": "esmangle retext-syntax-urls.js > retext-syntax-urls.min.js",
46-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
47-
"lint": "xo",
47+
"build": "npm run build-bundle && npm run build-mangle",
4848
"test-api": "node test",
4949
"test-coverage": "nyc --reporter lcov tape test/index.js",
50-
"test": "npm run build && npm run lint && npm run test-coverage"
50+
"test": "npm run format && npm run build && npm run test-coverage"
5151
},
5252
"nyc": {
5353
"check-coverage": true,
5454
"lines": 100,
5555
"functions": 100,
5656
"branches": 100
5757
},
58+
"prettier": {
59+
"tabWidth": 2,
60+
"useTabs": false,
61+
"singleQuote": true,
62+
"bracketSpacing": false,
63+
"semi": false,
64+
"trailingComma": "none"
65+
},
5866
"xo": {
59-
"space": true,
67+
"prettier": true,
6068
"esnext": false,
6169
"ignores": [
6270
"retext-syntax-urls.js"

readme.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@ npm install retext-syntax-urls
1616
Without `syntax-urls`:
1717

1818
```javascript
19-
var dictionary = require('dictionary-en-gb');
20-
var unified = require('unified');
21-
var english = require('retext-english');
22-
var stringify = require('retext-stringify');
23-
var spell = require('retext-spell');
24-
var urls = require('retext-syntax-urls');
25-
var report = require('vfile-reporter');
19+
var dictionary = require('dictionary-en-gb')
20+
var unified = require('unified')
21+
var english = require('retext-english')
22+
var stringify = require('retext-stringify')
23+
var spell = require('retext-spell')
24+
var urls = require('retext-syntax-urls')
25+
var report = require('vfile-reporter')
2626

2727
unified()
2828
.use(english)
2929
.use(spell, dictionary)
3030
.use(stringify)
31-
.process('Have you read readme.md? Check it out: www.example.com/readme.md', function (err, file) {
32-
console.log(report(err || file));
33-
});
31+
.process(
32+
'Have you read readme.md? Check it out: www.example.com/readme.md',
33+
function(err, file) {
34+
console.log(report(err || file))
35+
}
36+
)
3437
```
3538

3639
Yields:

0 commit comments

Comments
 (0)