2017-07-20 15:33:36 -04:00
|
|
|
function addMention(buffer, matches, state) {
|
|
|
|
let username = matches[1] || matches[2];
|
2017-11-20 16:28:03 -05:00
|
|
|
let { getURL, mentionLookup, formatUsername } = state.md.options.discourse;
|
2017-07-14 08:27:28 -04:00
|
|
|
|
|
|
|
let type = mentionLookup && mentionLookup(username);
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
let tag = "a";
|
|
|
|
let className = "mention";
|
2017-07-14 08:27:28 -04:00
|
|
|
let href = null;
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
if (type === "user") {
|
|
|
|
href = getURL("/u/") + username.toLowerCase();
|
|
|
|
} else if (type === "group") {
|
|
|
|
href = getURL("/groups/") + username;
|
|
|
|
className = "mention-group";
|
2017-07-14 08:27:28 -04:00
|
|
|
} else {
|
2018-06-15 11:03:24 -04:00
|
|
|
tag = "span";
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
let token = new state.Token("mention_open", tag, 1);
|
|
|
|
token.attrs = [["class", className]];
|
2017-07-14 08:27:28 -04:00
|
|
|
if (href) {
|
2018-06-15 11:03:24 -04:00
|
|
|
token.attrs.push(["href", href]);
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
|
|
|
|
2017-07-17 16:21:47 -04:00
|
|
|
buffer.push(token);
|
2017-11-20 16:28:03 -05:00
|
|
|
if (formatUsername) {
|
|
|
|
username = formatUsername(username);
|
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
token = new state.Token("text", "", 0);
|
|
|
|
token.content = "@" + username;
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2017-07-17 16:21:47 -04:00
|
|
|
buffer.push(token);
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
token = new state.Token("mention_close", tag, -1);
|
2017-07-17 16:21:47 -04:00
|
|
|
buffer.push(token);
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
2017-12-07 11:30:47 -05:00
|
|
|
helper.registerOptions((opts, siteSettings) => {
|
|
|
|
opts.features.mentions = !!siteSettings.enable_mentions;
|
|
|
|
});
|
|
|
|
|
2017-07-17 16:21:47 -04:00
|
|
|
helper.registerPlugin(md => {
|
|
|
|
const rule = {
|
2017-07-20 15:33:36 -04:00
|
|
|
matcher: /@(\w[\w.-]{0,58}\w)|@(\w)/,
|
2017-07-17 16:21:47 -04:00
|
|
|
onMatch: addMention
|
|
|
|
};
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
md.core.textPostProcess.ruler.push("mentions", rule);
|
2016-06-14 14:31:51 -04:00
|
|
|
});
|
|
|
|
}
|