34 lines
1.3 KiB
TypeScript
34 lines
1.3 KiB
TypeScript
/**
|
|
* Makes the server notify the same users the chat highlights and the editor
|
|
* suggests, by defining once what counts as an @mention.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
/**
|
|
* Matches an @username, with the name as its first group. The `@` must not
|
|
* follow a word character, so an email address mentions no one. Nor may it
|
|
* follow `[` or `(`, which leaves out a Markdown link's text or URL starting
|
|
* with `@`, and also a plain `(@name)`. The editor's suggestions match its
|
|
* source at the caret; the rest goes through {@link mentionedUsernames}.
|
|
* `Markdown` links mentions with a looser pattern of its own, on text already
|
|
* parsed, where code and URLs are out of the way.
|
|
*/
|
|
export const MENTION_RE: RegExp = /(?<![[(\w])@(\w+)/g;
|
|
|
|
/**
|
|
* Returns the names `body` mentions, in lower case, each once. Code, fenced
|
|
* or inline, and URLs mention no one, as `Markdown` shows them. The server
|
|
* notifies by it, and the chat highlights and chimes by it.
|
|
*/
|
|
export function mentionedUsernames(body: string): string[] {
|
|
const prose = body
|
|
// A fence left open runs to the end, as Markdown renders it.
|
|
.replace(/```[\s\S]*?(?:```|$)/g, " ")
|
|
.replace(/`[^`\n]*`/g, " ")
|
|
.replace(/https?:\/\/\S+/g, " ");
|
|
return [
|
|
...new Set([...prose.matchAll(MENTION_RE)].map((m) => m[1].toLowerCase())),
|
|
];
|
|
}
|