forked from jonschlinkert/remarkable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkify.js
161 lines (135 loc) · 3.91 KB
/
linkify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Replace link-like texts with link nodes.
//
// Currently restricted by `inline.validateLink()` to http/https/ftp
//
'use strict';
var Autolinker = require('autolinker');
var LINK_SCAN_RE = /www|@|\:\/\//;
function isLinkOpen(str) {
return /^<a[>\s]/i.test(str);
}
function isLinkClose(str) {
return /^<\/a\s*>/i.test(str);
}
// Stupid fabric to avoid singletons, for thread safety.
// Required for engines like Nashorn.
//
function createLinkifier() {
var links = [];
var autolinker = new Autolinker({
stripPrefix: false,
url: true,
email: true,
twitter: false,
replaceFn: function (linker, match) {
// Only collect matched strings but don't change anything.
switch (match.getType()) {
/*eslint default-case:0*/
case 'url':
links.push({
text: match.matchedText,
url: match.getUrl()
});
break;
case 'email':
links.push({
text: match.matchedText,
// normalize email protocol
url: 'mailto:' + match.getEmail().replace(/^mailto:/i, '')
});
break;
}
return false;
}
});
return {
links: links,
autolinker: autolinker
};
}
module.exports = function linkify(state) {
var i, j, l, tokens, token, text, nodes, ln, pos, level, htmlLinkLevel,
blockTokens = state.tokens,
linkifier = null, links, autolinker;
if (!state.options.linkify) { return; }
for (j = 0, l = blockTokens.length; j < l; j++) {
if (blockTokens[j].type !== 'inline') { continue; }
tokens = blockTokens[j].children;
htmlLinkLevel = 0;
// We scan from the end, to keep position when new tags added.
// Use reversed logic in links start/end match
for (i = tokens.length - 1; i >= 0; i--) {
token = tokens[i];
// Skip content of markdown links
if (token.type === 'link_close') {
i--;
while (tokens[i].level !== token.level && tokens[i].type !== 'link_open') {
i--;
}
continue;
}
// Skip content of html tag links
if (token.type === 'htmltag') {
if (isLinkOpen(token.content) && htmlLinkLevel > 0) {
htmlLinkLevel--;
}
if (isLinkClose(token.content)) {
htmlLinkLevel++;
}
}
if (htmlLinkLevel > 0) { continue; }
if (token.type === 'text' && LINK_SCAN_RE.test(token.content)) {
// Init linkifier in lazy manner, only if required.
if (!linkifier) {
linkifier = createLinkifier();
links = linkifier.links;
autolinker = linkifier.autolinker;
}
text = token.content;
links.length = 0;
autolinker.link(text);
if (!links.length) { continue; }
// Now split string to nodes
nodes = [];
level = token.level;
for (ln = 0; ln < links.length; ln++) {
if (!state.inline.validateLink(links[ln].url)) { continue; }
pos = text.indexOf(links[ln].text);
if (pos) {
level = level;
nodes.push({
type: 'text',
content: text.slice(0, pos),
level: level
});
}
nodes.push({
type: 'link_open',
href: links[ln].url,
title: '',
level: level++
});
nodes.push({
type: 'text',
content: links[ln].text,
level: level
});
nodes.push({
type: 'link_close',
level: --level
});
text = text.slice(pos + links[ln].text.length);
}
if (text.length) {
nodes.push({
type: 'text',
content: text,
level: level
});
}
// replace current node
blockTokens[j].children = tokens = [].concat(tokens.slice(0, i), nodes, tokens.slice(i + 1));
}
}
}
};