summaryrefslogtreecommitdiff
path: root/src/mfm/parse/elements/link.ts
blob: b353aebc5cdb4965aeb0bcb2b1bdfd8fe544478e (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
/**
 * Link
 */

export type TextElementLink = {
	type: 'link'
	content: string
	title: string
	url: string
	silent: boolean
};

export default function(text: string) {
	const match = text.match(/^\??\[([^\[\]]+?)\]\((https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+?)\)/);
	if (!match) return null;
	const silent = text[0] == '?';
	const link = match[0];
	const title = match[1];
	const url = match[2];
	return {
		type: 'link',
		content: link,
		title: title,
		url: url,
		silent: silent
	} as TextElementLink;
}