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