/** * Strip CommonMark backslash-escapes from a string. A backslash followed by * any ASCII punctuation character collapses to that character; backslash * followed by anything else (or end-of-string) is preserved. * * Why we need this: the article-content pipeline runs HTML through Turndown * to produce markdown source, and Turndown defensively escapes characters * that would otherwise have markdown meaning (`\_`, `\-`, `\[`, `\.` …). * Our TUI markdown renderer should *display* markdown, not show its source, * so the escapes have to be removed before rendering. * * Spec: https://spec.commonmark.org/0.30/#backslash-escapes */ // CommonMark ASCII-punctuation set. Kept as a literal-character class so the // regex engine doesn't have to interpret it. const ESCAPABLE = /\\([!"#$%&'()*+,\-./:;<=>?@[\]^_`{|}~\\])/g; export function unescapeMarkdownPunctuation(input: string): string { if (!input.includes('\\')) return input; return input.replace(ESCAPABLE, '$1'); }