summaryrefslogtreecommitdiff
path: root/src/mfm/parse/elements/emoji.ts
blob: 6c09ddf5c0ddfeff30a1153714733f58676336c3 (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
30
31
32
33
/**
 * Emoji
 */

import { emojiRegex } from "./emoji.regex";

export type TextElementEmoji = {
	type: 'emoji';
	content: string;
	emoji?: string;
	name?: string;
};

export default function(text: string) {
	const name = text.match(/^:([a-zA-Z0-9+_-]+):/);
	if (name) {
		return {
			type: 'emoji',
			content: name[0],
			name: name[1]
		} as TextElementEmoji;
	}
	const unicode = text.match(emojiRegex);
	if (unicode) {
		const [content] = unicode;
		return {
			type: 'emoji',
			content,
			emoji: content
		} as TextElementEmoji;
	}
	return null;
}