summaryrefslogtreecommitdiff
path: root/src/mfm/parse/elements/url.ts
blob: a16f67f2c2ec6f749eba5d4a4dd6d3ec163aadac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * URL
 */

export type TextElementUrl = {
	type: 'url';
	content: string;
	url: string;
};

export default function(text: string, before: string) {
	const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.,=\+\-]+/);
	if (!match) return null;
	let url = match[0];
	if (url.endsWith('.')) url = url.substr(0, url.lastIndexOf('.'));
	if (url.endsWith(',')) url = url.substr(0, url.lastIndexOf(','));
	if (url.endsWith(')') && before.endsWith('(')) url = url.substr(0, url.lastIndexOf(')'));
	return {
		type: 'url',
		content: url,
		url: url
	} as TextElementUrl;
}