-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
161 lines (138 loc) · 4.89 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
151
152
153
154
155
156
157
158
159
160
161
require("core-js/fn/array/find");
const mentionOpen = (tokens, idx) => `<a href="${tokens[idx].content}" class="${tokens[idx].linkclass}">`,
mentionClose = () => "</a>",
mentionText = (tokens, idx) => tokens[idx].content,
linkOpenRegExp = /^<a[>\s]/i,
linkCloseRegExp = /^<\/a\s*>/i,
isLinkOpen = (str) => linkOpenRegExp.test(str),
isLinkClose = (str) => linkCloseRegExp.test(str),
mentionRegExpPattern = "@\\{(?:([^}]+); )?([^\\} ]+)\\}",
mentionRegExp = new RegExp(mentionRegExpPattern),
mentionRegExpGlobal = new RegExp(mentionRegExpPattern, "g");
class MentionPlugin {
constructor(md, {mentions, allowHovercards, currentUserId} = {mentions: [], allowHovercards: false}) {
this.mentions = mentions;
this.allowHovercards = allowHovercards;
this.currentUserId = currentUserId;
this.escapeHtml = md.utils.escapeHtml;
md.core.ruler.after("inline", "mention", this.parseMentions.bind(this));
/* eslint-disable camelcase */
md.renderer.rules.mention_open = mentionOpen;
md.renderer.rules.mention_text = mentionText;
md.renderer.rules.mention_close = mentionClose;
/* eslint-enable camelcase */
}
findPersonById(id) {
return this.mentions.find((m) => id === m.diaspora_id || id === m.handle);
}
mentionLinkClass(person) {
return (this.allowHovercards && person.guid !== this.currentUserId) ?
"mention hovercardable" :
"mention";
}
/*
* Skips content inside of markdown links (between link_open and link_close)
* Modifies this.inMarkdownLink and this.markdownLinkStartLevel to keep track of the current state
*/
skipMarkdownLink(currentToken) {
if (this.inMarkdownLink) {
if (currentToken.level === this.markdownLinkStartLevel || currentToken.type === "link_close") {
this.inMarkdownLink = false;
}
return true;
}
if (currentToken.type === "link_open") {
this.markdownLinkStartLevel = currentToken.level;
this.inMarkdownLink = true;
return true;
}
return false;
}
/*
* Skips content inside of inline html links
* Modifies this.htmlLinkLevel to keep track of the current state
*/
skipHtmlLink(currentToken) {
if (currentToken.type === "html_inline") {
if (isLinkClose(currentToken.content) && this.htmlLinkLevel > 0) {
this.htmlLinkLevel--;
}
if (isLinkOpen(currentToken.content)) {
this.htmlLinkLevel++;
}
}
return this.htmlLinkLevel > 0;
}
/*
* Returns a list of the tokens needed for rendering all mentions inside of the current token
*/
mentionTokens(currentToken, state) {
let text = currentToken.content,
level = currentToken.level,
tokens = [],
token;
const matches = text.match(mentionRegExpGlobal);
if (matches === null) {
return [currentToken];
}
matches.forEach((match) => {
let [matchedText, name, diasporaId] = match.match(mentionRegExp),
pos = text.indexOf(match);
if (pos > 0) {
token = new state.Token("text", "", 0);
token.content = text.slice(0, pos);
token.level = level;
tokens.push(token);
}
token = new state.Token("text", "", 0);
token.content = "@";
token.level = level;
tokens.push(token);
const person = this.findPersonById(diasporaId);
if (person) {
token = new state.Token("mention_open", "", 1);
token.content = person.url || "/people/" + person.guid;
token.linkclass = this.mentionLinkClass(person);
token.level = level++;
tokens.push(token);
token = new state.Token("mention_text", "", 0);
token.content = this.escapeHtml(name ? name : person.name).trim();
token.level = level;
tokens.push(token);
token = new state.Token("mention_close", "", -1);
token.level = --level;
tokens.push(token);
} else {
token = new state.Token("text", "", 0);
token.content = name ? name : diasporaId;
token.level = level;
tokens.push(token);
}
text = text.slice(pos + matchedText.length);
});
if (text.length > 0) {
token = new state.Token("text", "", 0);
token.content = text;
token.level = state.level;
tokens.push(token);
}
return tokens;
}
parseMentions(state) {
state.tokens.forEach((blockToken) => {
if (blockToken.type !== "inline") {
return;
}
this.inMarkdownLink = false;
this.htmlLinkLevel = 0;
blockToken.children = blockToken.children.map((currentToken) => {
if (this.skipMarkdownLink(currentToken) || this.skipHtmlLink(currentToken) || currentToken.type !== "text") {
return [currentToken];
}
return this.mentionTokens(currentToken, state);
}).reduce((a, b) => a.concat(b), []);
return;
});
}
}
module.exports = (md, opts) => new MentionPlugin(md, opts);