summaryrefslogtreecommitdiff
path: root/src/misc/extract-url-from-mfm.ts
blob: aa7f5f25408b416cb04091498fd07f6c2b64be2c (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
34
import * as mfm from 'mfm-js';
import { unique } from '@/prelude/array';

// unique without hash
// [ http://a/#1, http://a/#2, http://b/#3 ] => [ http://a/#1, http://b/#3 ]
const removeHash = (x: string) => x.replace(/#[^#]*$/, '');

export function extractUrlFromMfm(nodes: mfm.MfmNode[], respectSilentFlag = true): string[] {
	const urlNodes = [] as (mfm.MfmUrl | mfm.MfmLink)[];

	function scan(nodes: mfm.MfmNode[]) {
		for (const node of nodes) {
			if (node.type === 'url') {
				urlNodes.push(node);
			} else if (node.type === 'link') {
				if (!respectSilentFlag || !node.props.silent) {
					urlNodes.push(node);
				}
			} else if (node.children) {
				scan(node.children);
			}
		}
	}

	scan(nodes);

	const urls = unique(urlNodes.map(x => x.props.url));

	return urls.reduce((array, url) => {
		const removed = removeHash(url);
		if (!array.map(x => removeHash(x)).includes(removed)) array.push(url);
		return array;
	}, [] as string[]);
}