mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 20:45:35 +08:00

Also: - remove unused code - rename bbcode_ruler to bbcode.ruler - add md.core.textPostProcess.ruler to apply at end of chain (excluding links)
49 lines
1.1 KiB
JavaScript
49 lines
1.1 KiB
JavaScript
function addMention(buffer, match, state) {
|
|
let username = match.slice(1);
|
|
let mentionLookup = state.md.options.discourse.mentionLookup;
|
|
let getURL = state.md.options.discourse.getURL;
|
|
|
|
let type = mentionLookup && mentionLookup(username);
|
|
|
|
let tag = 'a';
|
|
let className = 'mention';
|
|
let href = null;
|
|
|
|
if (type === 'user') {
|
|
href = getURL('/u/') + username.toLowerCase();
|
|
} else if (type === 'group') {
|
|
href = getURL('/groups/') + username;
|
|
className = 'mention-group';
|
|
} else {
|
|
tag = 'span';
|
|
}
|
|
|
|
let token = new state.Token('mention_open', tag, 1);
|
|
token.attrs = [['class', className]];
|
|
if (href) {
|
|
token.attrs.push(['href', href]);
|
|
}
|
|
|
|
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.registerPlugin(md => {
|
|
|
|
const rule = {
|
|
matcher: /@\w[\w.-]{0,59}/,
|
|
onMatch: addMention
|
|
};
|
|
|
|
md.core.textPostProcess.ruler.push('mentions', rule);
|
|
});
|
|
}
|