The best way to control Claude Code, Codex and more from any computer or phone

main
ts 90 lines 2.88 KB
Raw
1 import { msg } from "gt-react";
2
3 import { m, type MessageResolver } from "./i18n/messages";
4
5 export const CODE_BLOCK_LANGUAGES = [
6 { id: "", label: msg("Plain text") },
7 { id: "asm", label: msg("Assembly") },
8 { id: "bash", label: "Bash" },
9 { id: "c", label: "C" },
10 { id: "cpp", label: "C++" },
11 { id: "csharp", label: "C#" },
12 { id: "css", label: "CSS" },
13 { id: "dart", label: "Dart" },
14 // oxlint-disable-next-line t3code/no-untranslated-copy -- code language name
15 { id: "diff", label: "Diff" },
16 { id: "dockerfile", label: "Dockerfile" },
17 { id: "elixir", label: "Elixir" },
18 { id: "go", label: "Go" },
19 { id: "graphql", label: "GraphQL" },
20 { id: "haskell", label: "Haskell" },
21 { id: "html", label: "HTML" },
22 { id: "java", label: "Java" },
23 { id: "javascript", label: "JavaScript" },
24 { id: "json", label: "JSON" },
25 { id: "jsx", label: "JSX" },
26 { id: "kotlin", label: "Kotlin" },
27 { id: "lua", label: "Lua" },
28 { id: "markdown", label: "Markdown" },
29 { id: "php", label: "PHP" },
30 { id: "powershell", label: "PowerShell" },
31 { id: "python", label: "Python" },
32 { id: "r", label: "R" },
33 { id: "ruby", label: "Ruby" },
34 { id: "rust", label: "Rust" },
35 { id: "scala", label: "Scala" },
36 { id: "scss", label: "SCSS" },
37 { id: "sql", label: "SQL" },
38 { id: "svelte", label: "Svelte" },
39 { id: "swift", label: "Swift" },
40 { id: "toml", label: "TOML" },
41 { id: "tsx", label: "TSX" },
42 { id: "typescript", label: "TypeScript" },
43 { id: "vue", label: "Vue" },
44 { id: "xml", label: "XML" },
45 { id: "yaml", label: "YAML" },
46 { id: "zig", label: "Zig" },
47 ] as const;
48
49 export type CodeBlockLanguage = { readonly id: string; readonly label: string };
50
51 const LANGUAGE_ALIASES: Record<string, string> = {
52 assembly: "asm",
53 cs: "csharp",
54 "c++": "cpp",
55 js: "javascript",
56 md: "markdown",
57 plaintext: "",
58 py: "python",
59 rb: "ruby",
60 rs: "rust",
61 s: "asm",
62 sh: "bash",
63 shell: "bash",
64 text: "",
65 ts: "typescript",
66 txt: "",
67 yml: "yaml",
68 };
69
70 export function languageOfInfoString(info: string): string {
71 return info.trim().split(/\s+/, 1)[0] ?? "";
72 }
73
74 /** Plain text clears the language unless trailing metadata would be parsed as one. */
75 export function withInfoStringLanguage(info: string, language: string): string {
76 const leading = /^\s*/.exec(info)![0];
77 const rest = info.slice(leading.length + languageOfInfoString(info).length);
78 if (language) return `${leading}${language}${rest}`;
79 return rest.trim() ? `${leading}text${rest}` : "";
80 }
81
82 export function codeLanguageEntry(language: string): CodeBlockLanguage | null {
83 const id = LANGUAGE_ALIASES[language.toLowerCase()] ?? language;
84 return CODE_BLOCK_LANGUAGES.find((entry) => entry.id === id.toLowerCase()) ?? null;
85 }
86
87 export function codeLanguageLabel(language: string, resolve: MessageResolver = m): string {
88 const label = codeLanguageEntry(language)?.label;
89 return label === undefined ? language : resolve(label);
90 }