~james/core

core/web/src/lib/components/profile/bio.ts -rw-r--r-- 704 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
export type BioSegment = { kind: "text"; text: string } | { kind: "mention"; handle: string };

const MENTION =
	/(?<![\w@.])@((?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?)(?![\w-]|\.[a-z0-9])/gi;

export const bioSegments = (text: string): BioSegment[] => {
	const segments: BioSegment[] = [];
	let last = 0;
	for (const match of text.matchAll(MENTION)) {
		if (match.index > last)
			segments.push({ kind: "text", text: text.slice(last, match.index) });
		segments.push({ kind: "mention", handle: match[1].toLowerCase() });
		last = match.index + match[0].length;
	}
	if (last < text.length) segments.push({ kind: "text", text: text.slice(last) });
	return segments;
};