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";
|
2018-06-15 23:03:24 +08:00
|
|
|
let className = "mention";
|
2017-07-14 20:27:28 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
let token = new state.Token("mention_open", tag, 1);
|
|
|
|
token.attrs = [["class", className]];
|
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
|
|
|
|
2018-06-15 23:03:24 +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
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
token = new state.Token("mention_close", tag, -1);
|
2017-07-18 04:21:47 +08:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2017-07-18 04:21:47 +08:00
|
|
|
helper.registerPlugin(md => {
|
|
|
|
const rule = {
|
2017-07-21 03:33:36 +08:00
|
|
|
matcher: /@(\w[\w.-]{0,58}\w)|@(\w)/,
|
2017-07-18 04:21:47 +08:00
|
|
|
onMatch: addMention
|
|
|
|
};
|
2017-07-14 20:27:28 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
md.core.textPostProcess.ruler.push("mentions", rule);
|
2016-06-15 02:31:51 +08:00
|
|
|
});
|
|
|
|
}
|