discourse/app/assets/javascripts/pretty-text/engines/discourse-markdown/mentions.js.es6
Guo Xiang Tan c5a70eca6e
PERF: Move mention lookups out of the V8 context. (#6640)
We were looking up each mention one by one without any form of caching and that results
in a problem somewhat similar to an N+1. When we have to do alot of DB
lookups, it also increased the time spent in the V8 context which may
eventually lead to a timeout. The change here makes it such that mention lookups only does a single
DB query per post that happens outside of the V8 context.
2018-11-22 14:28:48 +08:00

34 lines
782 B
JavaScript

function addMention(buffer, matches, state) {
let username = matches[1] || matches[2];
let tag = "span";
let className = "mention";
let token = new state.Token("mention_open", tag, 1);
token.attrs = [["class", className]];
buffer.push(token);
token = new state.Token("text", "", 0);
token.content = "@" + username;
buffer.push(token);
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
};
md.core.textPostProcess.ruler.push("mentions", rule);
});
}