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;
};