-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
150 lines (117 loc) · 4.05 KB
/
index.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
// Process #hashtag
'use strict';
//////////////////////////////////////////////////////////////////////////
// Renderer partials
function hashtag_open(tokens, idx) {
var tagName = tokens[idx].content.toLowerCase();
return '<a href="/tags/' + tagName + '" class="tag">';
}
function hashtag_close() { return '</a>'; }
function hashtag_text(tokens, idx) {
return '#' + tokens[idx].content;
}
//////////////////////////////////////////////////////////////////////////
function isLinkOpen(str) { return /^<a[>\s]/i.test(str); }
function isLinkClose(str) { return /^<\/a\s*>/i.test(str); }
module.exports = function hashtag_plugin(md, options) {
var arrayReplaceAt = md.utils.arrayReplaceAt,
escapeHtml = md.utils.escapeHtml,
regex,
hashtagRegExp = '\\w+',
preceding = '^|\\s';
if (options) {
if (typeof options.preceding !== 'undefined') {
preceding = options.preceding;
}
if (typeof options.hashtagRegExp !== 'undefined') {
hashtagRegExp = options.hashtagRegExp;
}
}
regex = new RegExp('(' + preceding + ')#(' + hashtagRegExp + ')', 'g');
function hashtag(state) {
var i, j, l, m,
tagName,
currentToken,
token,
tokens,
Token = state.Token,
blockTokens = state.tokens,
htmlLinkLevel,
matches,
text,
nodes,
pos,
level;
for (j = 0, l = blockTokens.length; j < l; j++) {
if (blockTokens[j].type !== 'inline') { continue; }
tokens = blockTokens[j].children;
htmlLinkLevel = 0;
for (i = tokens.length - 1; i >= 0; i--) {
currentToken = tokens[i];
// skip content of markdown links
if (currentToken.type === 'link_close') {
i--;
while (tokens[i].level !== currentToken.level && tokens[i].type !== 'link_open') {
i--;
}
continue;
}
// skip content of html links
if (currentToken.type === 'html_inline') {
// we are going backwards, so isLinkOpen shows end of link
if (isLinkOpen(currentToken.content) && htmlLinkLevel > 0) {
htmlLinkLevel--;
}
if (isLinkClose(currentToken.content)) {
htmlLinkLevel++;
}
}
if (htmlLinkLevel > 0) { continue; }
if (currentToken.type !== 'text') { continue; }
// find hashtags
text = currentToken.content;
matches = text.match(regex);
if (matches === null) { continue; }
nodes = [];
level = currentToken.level;
for (m = 0; m < matches.length; m++) {
tagName = matches[m].split('#', 2)[1];
// find the beginning of the matched text
pos = text.indexOf(matches[m]);
// find the beginning of the hashtag
pos = text.indexOf('#' + tagName, pos);
if (pos > 0) {
token = new Token('text', '', 0);
token.content = text.slice(0, pos);
token.level = level;
nodes.push(token);
}
token = new Token('hashtag_open', '', 1);
token.content = tagName;
token.level = level++;
nodes.push(token);
token = new Token('hashtag_text', '', 0);
token.content = escapeHtml(tagName);
token.level = level;
nodes.push(token);
token = new Token('hashtag_close', '', -1);
token.level = --level;
nodes.push(token);
text = text.slice(pos + 1 + tagName.length);
}
if (text.length > 0) {
token = new Token('text', '', 0);
token.content = text;
token.level = level;
nodes.push(token);
}
// replace current node
blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, nodes);
}
}
}
md.core.ruler.after('inline', 'hashtag', hashtag);
md.renderer.rules.hashtag_open = hashtag_open;
md.renderer.rules.hashtag_text = hashtag_text;
md.renderer.rules.hashtag_close = hashtag_close;
};