The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { resolveHighlightLanguage } from "../lib/highlightLanguage"; |
| 2 | |
| 3 | // Fences enriched-markdown colours itself: cpp/highlight/CodeBlockLanguages.cpp entries with a default grammar. |
| 4 | const NATIVE_HIGHLIGHT_FENCES = new Set([ |
| 5 | "bash", |
| 6 | "c", |
| 7 | "css", |
| 8 | "go", |
| 9 | "golang", |
| 10 | "html", |
| 11 | "java", |
| 12 | "javascript", |
| 13 | "js", |
| 14 | "json", |
| 15 | "jsx", |
| 16 | "markdown", |
| 17 | "md", |
| 18 | "py", |
| 19 | "python", |
| 20 | "rs", |
| 21 | "rust", |
| 22 | "sh", |
| 23 | "shell", |
| 24 | "ts", |
| 25 | "tsx", |
| 26 | "typescript", |
| 27 | "yaml", |
| 28 | "yml", |
| 29 | "zsh", |
| 30 | ]); |
| 31 | |
| 32 | /** A fenced code block for `code`, with a backtick fence longer than any backtick run inside it. */ |
| 33 | export function fencedCodeMarkdown(code: string, language: string): string { |
| 34 | let longestRun = 0; |
| 35 | for (const [run] of code.matchAll(/`+/g)) longestRun = Math.max(longestRun, run.length); |
| 36 | const fence = "`".repeat(Math.max(3, longestRun + 1)); |
| 37 | return `${fence}${language}\n${code}\n${fence}`; |
| 38 | } |
| 39 | |
| 40 | type CodeFenceRenderer = |
| 41 | | { readonly kind: "mermaid" } |
| 42 | | { readonly kind: "math" } |
| 43 | | { readonly kind: "highlight"; readonly language: string } |
| 44 | | { readonly kind: "native" }; |
| 45 | |
| 46 | /** Mermaid and math fences first: highlights knows `mermaid` too, and would claim it as code. */ |
| 47 | export function codeFenceRenderer(fence: string, mathRenderingEnabled: boolean): CodeFenceRenderer { |
| 48 | if (fence === "mermaid") return { kind: "mermaid" }; |
| 49 | if (fence === "math" && mathRenderingEnabled) return { kind: "math" }; |
| 50 | const name = fence.toLowerCase(); |
| 51 | if (NATIVE_HIGHLIGHT_FENCES.has(name)) return { kind: "native" }; |
| 52 | const language = resolveHighlightLanguage({ language: name }); |
| 53 | return language === "text" ? { kind: "native" } : { kind: "highlight", language }; |
| 54 | } |