/** * Dice completion source — extracts `` text content from markdown files. */ import type { CompletionSource, DiceCompletion } from "../types.js"; function looksLikeDice(raw: string): boolean { if (raw.length > 80) return false; return /^\d*d\d+/i.test(raw) || /^[+-]/.test(raw); } export const diceSource: CompletionSource = { key: "dice", scan(index) { const items: DiceCompletion[] = []; const tagRegex = /:md-dice\[([^[]+)\]/gi; for (const [path, content] of Object.entries(index)) { if (!path.endsWith(".md")) continue; let match: RegExpExecArray | null; tagRegex.lastIndex = 0; while ((match = tagRegex.exec(content)) !== null) { const raw = match[1].trim(); if (!raw || !looksLikeDice(raw)) continue; items.push({ label: raw, notation: raw, source: path, }); } } return items; }, };