import hljs from 'highlight.js'; const NAMED_ENTITY_MAP: Record = { '&': '&', '<': '<', '>': '>', '"': '"', ''': "'", }; function decodeEntities(str: string): string { return str.replace(/&#x[0-9a-fA-F]+;|&#[0-9]+;|&|<|>|"|'/g, m => { if (m.startsWith('&#x')) return String.fromCodePoint(parseInt(m.slice(3, -1), 16)); if (m.startsWith('&#')) return String.fromCodePoint(parseInt(m.slice(2, -1), 10)); return NAMED_ENTITY_MAP[m]!; }); } /** * Post-processes an HTML string, replacing every *
…
* with a syntax-highlighted version produced by highlight.js. * * Must be called server-side only (SSG build time). */ export function applyHighlighting(html: string): string { return html.replace( /
([\s\S]*?)<\/code><\/pre>/g,
    (_match, lang: string, encoded: string) => {
      const code = decodeEntities(encoded);
      let highlighted: string;
      try {
        highlighted = hljs.highlight(code, { language: lang, ignoreIllegals: true }).value;
      } catch {
        highlighted = hljs.highlightAuto(code).value;
      }
      return `
${highlighted}
`; }, ); }