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