|
1 |
| -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
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') |
6 | 6 |
|
7 |
| -module.exports = urls; |
| 7 | +module.exports = urls |
8 | 8 |
|
9 |
| -var slashes = /^\/{1,3}$/; |
| 9 | +var slashes = /^\/{1,3}$/ |
10 | 10 |
|
11 | 11 | function urls() {
|
12 |
| - this.Parser.prototype.useFirst('tokenizeSentence', modifyChildren(mergeLinks)); |
| 12 | + this.Parser.prototype.useFirst('tokenizeSentence', modifyChildren(mergeLinks)) |
13 | 13 | }
|
14 | 14 |
|
15 | 15 | /* eslint-disable complexity */
|
16 | 16 | 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 |
26 | 26 |
|
27 | 27 | if (!puncOrSymbol(child.type) || toString(child) !== '.') {
|
28 |
| - return; |
| 28 | + return |
29 | 29 | }
|
30 | 30 |
|
31 |
| - /* Find preceding word/punctuation. Stop before slashes, break after `www`. */ |
| 31 | + // Find preceding word/punctuation. Stop before slashes, break after `www`. |
32 | 32 | while (siblings[start - 1]) {
|
33 |
| - type = siblings[start - 1].type; |
| 33 | + type = siblings[start - 1].type |
34 | 34 |
|
35 | 35 | if (!applicable(type)) {
|
36 |
| - break; |
| 36 | + break |
37 | 37 | }
|
38 | 38 |
|
39 | 39 | if (puncOrSymbol(type) && slashes.test(toString(siblings[start - 1]))) {
|
40 |
| - break; |
| 40 | + break |
41 | 41 | }
|
42 | 42 |
|
43 |
| - start--; |
| 43 | + start-- |
44 | 44 |
|
45 |
| - nodes.unshift(siblings[start]); |
| 45 | + nodes.unshift(siblings[start]) |
46 | 46 |
|
47 | 47 | if (type === 'WordNode' && toString(siblings[start]) === 'www') {
|
48 |
| - break; |
| 48 | + break |
49 | 49 | }
|
50 | 50 | }
|
51 | 51 |
|
52 |
| - /* Find following word/punctuation. */ |
| 52 | + // Find following word/punctuation. |
53 | 53 | while (siblings[end + 1]) {
|
54 |
| - type = siblings[end + 1].type; |
| 54 | + type = siblings[end + 1].type |
55 | 55 |
|
56 | 56 | if (!applicable(type)) {
|
57 |
| - break; |
| 57 | + break |
58 | 58 | }
|
59 | 59 |
|
60 |
| - end++; |
61 |
| - nodes.push(siblings[end]); |
| 60 | + end++ |
| 61 | + nodes.push(siblings[end]) |
62 | 62 | }
|
63 | 63 |
|
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. |
66 | 66 | if (currentIndex === start || currentIndex === end) {
|
67 |
| - return; |
| 67 | + return |
68 | 68 | }
|
69 | 69 |
|
70 |
| - /* 1-3 slashes. */ |
| 70 | + // 1-3 slashes. |
71 | 71 | if (
|
72 | 72 | start > 0 &&
|
73 | 73 | puncOrSymbol(siblings[start - 1].type) &&
|
74 | 74 | slashes.test(toString(siblings[start - 1]))
|
75 | 75 | ) {
|
76 |
| - start--; |
77 |
| - nodes.unshift(siblings[start]); |
| 76 | + start-- |
| 77 | + nodes.unshift(siblings[start]) |
78 | 78 | }
|
79 | 79 |
|
80 |
| - /* URL protocol and colon. */ |
| 80 | + // URL protocol and colon. |
81 | 81 | if (
|
82 | 82 | start > 2 &&
|
83 | 83 | puncOrSymbol(siblings[start - 1].type) &&
|
84 | 84 | toString(siblings[start - 1]) === ':' &&
|
85 | 85 | siblings[start - 2].type === 'WordNode'
|
86 | 86 | ) {
|
87 |
| - nodes.unshift(siblings[start - 2], siblings[start - 1]); |
88 |
| - start -= 2; |
| 87 | + nodes.unshift(siblings[start - 2], siblings[start - 1]) |
| 88 | + start -= 2 |
89 | 89 | }
|
90 | 90 |
|
91 |
| - value = null; |
| 91 | + value = null |
92 | 92 |
|
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 `)`. |
94 | 94 | if (puncOrSymbol(siblings[end].type)) {
|
95 |
| - value = toString(siblings[end]); |
| 95 | + value = toString(siblings[end]) |
96 | 96 |
|
97 | 97 | if (value !== '/' && value !== ')') {
|
98 |
| - end--; |
99 |
| - nodes.pop(); |
| 98 | + end-- |
| 99 | + nodes.pop() |
100 | 100 | }
|
101 | 101 | }
|
102 | 102 |
|
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]) |
106 | 106 |
|
107 | 107 | if (initial.line && final.line) {
|
108 |
| - child.position = {start: initial, end: final}; |
| 108 | + child.position = {start: initial, end: final} |
109 | 109 | }
|
110 | 110 |
|
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) |
113 | 113 |
|
114 |
| - index++; |
| 114 | + index++ |
115 | 115 |
|
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. |
117 | 117 | if (value === '.') {
|
118 |
| - index++; |
| 118 | + index++ |
119 | 119 | }
|
120 | 120 |
|
121 |
| - return index; |
| 121 | + return index |
122 | 122 | }
|
123 | 123 |
|
124 | 124 | function applicable(type) {
|
125 |
| - return type === 'WordNode' || puncOrSymbol(type); |
| 125 | + return type === 'WordNode' || puncOrSymbol(type) |
126 | 126 | }
|
127 | 127 |
|
128 | 128 | function puncOrSymbol(type) {
|
129 |
| - return type === 'PunctuationNode' || type === 'SymbolNode'; |
| 129 | + return type === 'PunctuationNode' || type === 'SymbolNode' |
130 | 130 | }
|
0 commit comments