codeBlock

tcmts.site.codeBlock · #pzcrqihb · function · runs anywhere

A fenced code block as HTML: as hl highlights it in its language (the info string's first word: ```ts import.meta.vitest is TypeScript), if it does; as it's written otherwise.

import type { Highlight } from "#codebase/tcmts/content";
import { esc } from "#codebase/tcmts/site";

export function codeBlock(hl: Highlight, text: string, info: string | undefined): string {
  const lang = /^\S*/.exec(info ?? "")![0];
  const html = lang ? hl(text, lang) : undefined;
  if (html !== undefined) return `${html}\n`;
  return `<pre${lang ? ` data-lang="${esc(lang)}"` : ""}>${esc(text)}</pre>\n`;
}

uses

used by