/** Extract the distinct titles referenced as [[Wiki Links]] in a body of text. */ export function extractWikiLinks(text: string): string[] { const set = new Set(); for (const m of text.matchAll(/\[\[([^\]]+)\]\]/g)) { const title = m[1]?.trim(); if (title) set.add(title); } return [...set]; } /** Split text into plain segments and [[link]] segments for rendering. */ export function splitWikiLinks(text: string): { text: string; link?: string }[] { const parts: { text: string; link?: string }[] = []; let last = 0; for (const m of text.matchAll(/\[\[([^\]]+)\]\]/g)) { const idx = m.index ?? 0; if (idx > last) parts.push({ text: text.slice(last, idx) }); parts.push({ text: m[1]!.trim(), link: m[1]!.trim() }); last = idx + m[0].length; } if (last < text.length) parts.push({ text: text.slice(last) }); return parts; }