/*** EXPORT ------------------------------------------- ***/ export function markdown(input: string): string { const inlineCode: string[] = []; const inlineCodePattern = /`([^`]+)`/g; const refPattern = /\s*\[(\w+)\]\s+<([^>]+)>$/gm; const refs = new Map(); let processed = input.replace(inlineCodePattern, (_match, content) => { const index = inlineCode.length; inlineCode.push(content); return ``; }); for (const match of processed.matchAll(refPattern)) { const [, ref, url] = match; refs.set(ref, url); } processed = processed .replace(/\[([^\]]+)\]\(([^)]+)\)/g, `$1`) .replace(/(\*\*|__)(.*?)\1/g, `$2`) .replace(/'/g, "’") .replace(/(\.\.\.)/g, "…") .replace(/---/g, "
"); for (const block in inlineCode) { processed = processed.replace(``, `${escapeHtml(inlineCode[block])}`); } return processed.trimEnd(); } /*** HELPER ------------------------------------------- ***/ function escapeHtml(str: string): string { return str .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); }