2022-12-06 12:54:04 -05:00
|
|
|
import { mentionRegex } from "pretty-text/mentions";
|
|
|
|
|
2017-07-20 15:33:36 -04:00
|
|
|
function addMention(buffer, matches, state) {
|
|
|
|
let username = matches[1] || matches[2];
|
2018-11-22 01:28:48 -05:00
|
|
|
let tag = "span";
|
2017-07-14 08:27:28 -04:00
|
|
|
let className = "mention";
|
|
|
|
|
2017-07-17 16:21:47 -04:00
|
|
|
let token = new state.Token("mention_open", tag, 1);
|
2017-07-14 08:27:28 -04:00
|
|
|
token.attrs = [["class", className]];
|
|
|
|
|
2017-07-17 16:21:47 -04:00
|
|
|
buffer.push(token);
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2017-07-17 16:21:47 -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
|
|
|
|
2017-07-17 16:21:47 -04:00
|
|
|
token = new state.Token("mention_close", tag, -1);
|
|
|
|
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;
|
2019-04-23 06:22:47 -04:00
|
|
|
opts.features.unicodeUsernames = !!siteSettings.unicode_usernames;
|
2017-12-07 11:30:47 -05:00
|
|
|
});
|
|
|
|
|
2017-07-17 16:21:47 -04:00
|
|
|
helper.registerPlugin((md) => {
|
|
|
|
const rule = {
|
2019-04-23 06:22:47 -04:00
|
|
|
matcher: mentionRegex(md.options.discourse.features.unicodeUsernames),
|
2017-07-17 16:21:47 -04:00
|
|
|
onMatch: addMention,
|
|
|
|
};
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2017-07-17 16:21:47 -04:00
|
|
|
md.core.textPostProcess.ruler.push("mentions", rule);
|
2016-06-14 14:31:51 -04:00
|
|
|
});
|
|
|
|
}
|