summaryrefslogtreecommitdiff
path: root/src/text/parse/elements/link.ts
blob: 7e0d6f5cf82f66fc2d4fa9ec56b0d65698233f47 (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;
}