discourse/app/assets/javascripts/pretty-text/engines/discourse-markdown/mentions.js.es6

54 lines
1.2 KiB
Plaintext
Raw Normal View History

function addMention(buffer, matches, state) {
let username = matches[1] || matches[2];
let { getURL, mentionLookup, formatUsername } = state.md.options.discourse;
let type = mentionLookup && mentionLookup(username);
2018-06-15 11:03:24 -04:00
let tag = "a";
let className = "mention";
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";
} else {
2018-06-15 11:03:24 -04:00
tag = "span";
}
2018-06-15 11:03:24 -04:00
let token = new state.Token("mention_open", tag, 1);
token.attrs = [["class", className]];
if (href) {
2018-06-15 11:03:24 -04:00
token.attrs.push(["href", href]);
}
buffer.push(token);
if (formatUsername) {
username = formatUsername(username);
}
2018-06-15 11:03:24 -04:00
token = new state.Token("text", "", 0);
token.content = "@" + username;
buffer.push(token);
2018-06-15 11:03:24 -04:00
token = new state.Token("mention_close", tag, -1);
buffer.push(token);
}
export function setup(helper) {
helper.registerOptions((opts, siteSettings) => {
opts.features.mentions = !!siteSettings.enable_mentions;
});
helper.registerPlugin(md => {
const rule = {
matcher: /@(\w[\w.-]{0,58}\w)|@(\w)/,
onMatch: addMention
};
2018-06-15 11:03:24 -04:00
md.core.textPostProcess.ruler.push("mentions", rule);
});
}