summaryrefslogtreecommitdiff
path: root/src/mfm/parse/elements/mention.ts
blob: 7a609e5d3409b2ba23c1cdf30e3d085f0917cd6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * Mention
 */
import parseAcct from '../../../misc/acct/parse';
import { toUnicode } from 'punycode';

export type TextElementMention = {
	type: 'mention';
	content: string;
	canonical: string;
	username: string;
	host: string;
};

export default function(text: string, before: string) {
	const match = text.match(/^@[a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9])?/i);
	if (!match) return null;
	if (/[a-zA-Z0-9]$/.test(before)) return null;
	const mention = match[0];
	const { username, host } = parseAcct(mention.substr(1));
	const canonical = host != null ? `@${username}@${toUnicode(host)}` : mention;
	return {
		type: 'mention',
		content: mention,
		canonical,
		username,
		host
	} as TextElementMention;
}