/
mentions.js.es6
50 lines (39 loc) · 1.13 KB
/
mentions.js.es6
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
function addMention(buffer, matches, state) {
let username = matches[1] || matches[2];
let { getURL, mentionLookup, formatUsername } = state.md.options.discourse;
let type = mentionLookup && mentionLookup(username);
let tag = 'a';
let className = 'mention';
let href = null;
if (type === 'user') {
href = getURL('/u/') + username.toLowerCase();
} else if (type === 'group') {
href = getURL('/groups/') + username;
className = 'mention-group';
} else {
tag = 'span';
}
let token = new state.Token('mention_open', tag, 1);
token.attrs = [['class', className]];
if (href) {
token.attrs.push(['href', href]);
}
buffer.push(token);
if (formatUsername) {
username = formatUsername(username);
}
token = new state.Token('text', '', 0);
token.content = '@'+username;
buffer.push(token);
token = new state.Token('mention_close', tag, -1);
buffer.push(token);
}
export function setup(helper) {
helper.registerPlugin(md => {
const rule = {
matcher: /@(\w[\w.-]{0,58}\w)|@(\w)/,
onMatch: addMention
};
md.core.textPostProcess.ruler.push('mentions', rule);
});
}