/** * Factory card references in pull request text, GitHub style. * * A reference is a card link (`https:///c/`, or the older * `/cards/` and `/work?card=` forms) or the short form * `factory:`. A closing keyword right before it (close, closes, closed, * fix, fixes, fixed, resolve, resolves, resolved, with an optional colon) * closes the card when the PR merges; any other reference only links the PR. * Text inside HTML comments, fenced code blocks, and inline code is ignored, * so PR template examples never match. */ export type CardRef = { cardId: string; closes: boolean; keyword: string | null; text: string } export const CLOSING_KEYWORDS = ['close', 'closes', 'closed', 'fix', 'fixes', 'fixed', 'resolve', 'resolves', 'resolved'] as const const ID = '(wi_[A-Za-z0-9_-]{3,60})' const URL_REF = `https?://[A-Za-z0-9.-]+(?::\\d+)?(?:/[A-Za-z0-9._~-]+)*?/(?:c|cards)/${ID}(?![A-Za-z0-9_-])` const WORK_REF = `https?://[A-Za-z0-9.-]+(?::\\d+)?(?:/[A-Za-z0-9._~-]+)*?/work\\?(?:[^\\s#]*&)?card=${ID}(?![A-Za-z0-9_-])` const SHORT_REF = `(?|$)/g, ' ') .replace(/(^|\n)[ \t]*(```|~~~)[\s\S]*?(?:\n[ \t]*\2[^\n]*|$)/g, '$1 ') .replace(/`[^`\n]*`/g, ' ') } /** * Every distinct card referenced in `body`. A card referenced both with and * without a keyword counts as closing. */ export function parseCardRefs(body: unknown): CardRef[] { if (typeof body !== 'string' || !body) return [] const text = stripNonProse(body.slice(0, 65536)) const byId = new Map() for (const m of text.matchAll(REF_RE)) { const keyword = m[1] ? m[1].toLowerCase() : null const cardId = m[2] ?? m[3] ?? m[4] if (!cardId) continue const prev = byId.get(cardId) if (prev && (prev.closes || !keyword)) continue byId.set(cardId, { cardId, closes: Boolean(keyword), keyword, text: m[0].trim().slice(0, 200) }) if (byId.size >= 50) break } return [...byId.values()] }