/** * Inline typographic transform (smartypants). * * Converts ASCII typography to real Unicode: * "quoted" → "quoted" (U+201C/U+201D) * 'quoted' → 'quoted' (U+2018/U+2019) * don't → don't (apostrophe: U+2019) * -- → — (em dash U+2014) * ... → … (ellipsis U+2026) * * Critical: must NOT touch code, URLs, or HTML attributes. The Codex round * 2 review flagged this specifically — smartypants run over a fenced code * block corrupts the code and tokens inside tag attributes can break * parsing. * * This operates on HTML (marked already produced it) and walks text nodes * only via a lightweight regex that recognizes code/pre/URL zones and * skips them entirely. */ const CODE_ZONE_RE = /<(pre|code|script|style)\b[^>]*>[\s\S]*?<\/\1>/gi; const TAG_RE = /<[^>]+>/g; // \u0000 is the placeholder sentinel (see PLACEHOLDER below). It must be // excluded here: URLs are carved AFTER tags, so a URL sitting flush against // a tag (`https://ex.com`) would otherwise let \S+ swallow // the placeholder standing in for `` — that placeholder then never // restores (restore is a single pass) and the tag is lost. const URL_RE = /\bhttps?:\/\/[^\s\u0000]+/g; /** * Apply smartypants to an HTML string. Zones that should not be touched: * -
, ,