summaryrefslogtreecommitdiff
path: root/src/mfm/parse/elements/link.ts
blob: 796aeb1ab3ee2d5526677fbdffbc34e81e3621c8 (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.startsWith('?');
	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;
}