Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composer-code-languages.ts

96 lines3.1 KB

1import { msg } from "gt-react";
2​
3import { m, type MessageResolver } from "./i18n/messages";
4​
5export 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: "systemverilog", label: "SystemVerilog" },
41 { id: "toml", label: "TOML" },
42 { id: "tsx", label: "TSX" },
43 { id: "typescript", label: "TypeScript" },
44 // oxlint-disable-next-line t3code/no-untranslated-copy -- code language name
45 { id: "verilog", label: "Verilog" },
46 { id: "vhdl", label: "VHDL" },
47 { id: "vue", label: "Vue" },
48 { id: "xml", label: "XML" },
49 { id: "yaml", label: "YAML" },
50 { id: "zig", label: "Zig" },
51] as const;
52​
53export type CodeBlockLanguage = { readonly id: string; readonly label: string };
54​
55const LANGUAGE_ALIASES: Record<string, string> = {
56 assembly: "asm",
57 cs: "csharp",
58 "c++": "cpp",
59 js: "javascript",
60 md: "markdown",
61 plaintext: "",
62 py: "python",
63 rb: "ruby",
64 rs: "rust",
65 s: "asm",
66 sh: "bash",
67 shell: "bash",
68 sv: "systemverilog",
69 "system-verilog": "systemverilog",
70 text: "",
71 ts: "typescript",
72 txt: "",
73 yml: "yaml",
74};
75​
76export function languageOfInfoString(info: string): string {
77 return info.trim().split(/\s+/, 1)[0] ?? "";
79​
80/** Plain text clears the language unless trailing metadata would be parsed as one. */
81export function withInfoStringLanguage(info: string, language: string): string {
82 const leading = /^\s*/.exec(info)![0];
83 const rest = info.slice(leading.length + languageOfInfoString(info).length);
84 if (language) return `${leading}${language}${rest}`;
85 return rest.trim() ? `${leading}text${rest}` : "";
87​
88export function codeLanguageEntry(language: string): CodeBlockLanguage | null {
89 const id = LANGUAGE_ALIASES[language.toLowerCase()] ?? language;
90 return CODE_BLOCK_LANGUAGES.find((entry) => entry.id === id.toLowerCase()) ?? null;
92​
93export function codeLanguageLabel(language: string, resolve: MessageResolver = m): string {
94 const label = codeLanguageEntry(language)?.label;
95 return label === undefined ? language : resolve(label);