Files
open-webui/src/lib/utils/codeHighlight.ts
T
2026-11-07 22:51:09 +09:00

203 lines
3.9 KiB
TypeScript

/**
* Map file extensions to Shiki language identifiers.
* Only extensions whose Shiki lang id differs from the extension itself need explicit entries.
*/
const EXT_OVERRIDE: Record<string, string> = {
py: 'python',
js: 'javascript',
mjs: 'javascript',
cjs: 'javascript',
ts: 'typescript',
mts: 'typescript',
cts: 'typescript',
jsx: 'jsx',
tsx: 'tsx',
rb: 'ruby',
rs: 'rust',
kt: 'kotlin',
cs: 'csharp',
fs: 'fsharp',
sh: 'bash',
bash: 'bash',
zsh: 'bash',
yml: 'yaml',
md: 'markdown',
mdx: 'mdx',
dockerfile: 'dockerfile',
makefile: 'makefile',
svg: 'xml',
tf: 'terraform',
hcl: 'hcl',
ex: 'elixir',
exs: 'elixir',
erl: 'erlang',
hs: 'haskell',
ml: 'ocaml',
mli: 'ocaml',
pl: 'perl',
pm: 'perl',
r: 'r',
m: 'objective-c',
mm: 'objective-cpp',
h: 'c',
hpp: 'cpp',
cc: 'cpp',
cxx: 'cpp',
proto: 'proto',
nim: 'nim',
zig: 'zig',
v: 'v',
svelte: 'svelte',
vue: 'vue',
astro: 'astro',
prisma: 'prisma',
graphql: 'graphql',
gql: 'graphql',
jsonc: 'jsonc',
jsonl: 'jsonl'
};
// Keep extension IDs local to avoid loading Shiki's full language bundle just to recognize files.
const KNOWN_LANG_IDS = new Set([
'ada',
'awk',
'bat',
'c',
'cmake',
'clojure',
'cpp',
'crystal',
'css',
'd',
'dart',
'diff',
'elixir',
'elm',
'erlang',
'fish',
'gleam',
'glsl',
'go',
'groovy',
'haml',
'haskell',
'hlsl',
'html',
'ini',
'java',
'javascript',
'json',
'json5',
'jsonc',
'jsx',
'julia',
'kotlin',
'latex',
'less',
'lisp',
'log',
'lua',
'make',
'markdown',
'matlab',
'mdx',
'mojo',
'nim',
'nix',
'nushell',
'ocaml',
'pascal',
'perl',
'php',
'postcss',
'powershell',
'prisma',
'prolog',
'proto',
'pug',
'python',
'r',
'ruby',
'rust',
'sass',
'scala',
'scheme',
'scss',
'solidity',
'sql',
'svelte',
'swift',
'tcl',
'terraform',
'tex',
'toml',
'tsx',
'typescript',
'typst',
'v',
'vb',
'verilog',
'vhdl',
'vue',
'wasm',
'wgsl',
'xml',
'yaml',
'zig'
]);
/**
* Resolve a file extension to a Shiki language id, or null if not supported.
*/
export function extToLang(ext: string): string | null {
const lower = ext.toLowerCase();
// explicit override first
if (EXT_OVERRIDE[lower]) return EXT_OVERRIDE[lower];
// if the extension itself is a known language id (e.g. 'go', 'rust', 'sql', 'toml', ...)
if (KNOWN_LANG_IDS.has(lower)) return lower;
return null;
}
/** Resolve filenames as well as extensions (Dockerfile and Makefile have no extension). */
export function languageForFilePath(path: string | null): string | null {
if (!path) return null;
const name = path.split(/[\\/]/).pop()?.toLowerCase() ?? '';
if (/^(dockerfile|containerfile)(\.|$)/.test(name)) return 'dockerfile';
if (/^(makefile|gnumakefile)(\.|$)/.test(name)) return 'makefile';
return extToLang(name.split('.').pop() ?? '');
}
/**
* Returns true if the given file path has a code-file extension that Shiki can highlight.
*/
export function isCodeFile(path: string | null): boolean {
return languageForFilePath(path) !== null;
}
/**
* Highlight code using Shiki with dual light/dark themes via CSS variables.
* Returns an HTML string. Throws on failure.
*
* Shiki is loaded on demand (dynamic import) to avoid pulling ~5-10MB of
* JavaScript into the initial page bundle. Since this function is already
* async, callers are completely unaffected by the change.
*/
export async function highlightCode(code: string, filePath: string): Promise<string> {
return highlightCodeBlock(code, languageForFilePath(filePath) ?? 'text');
}
/** Highlight a Markdown fence, falling back to plain text for unknown language tags. */
export async function highlightCodeBlock(code: string, language: string): Promise<string> {
const { codeToHtml, bundledLanguages } = await import('shiki');
const name = language.trim().toLowerCase();
const lang = Object.hasOwn(bundledLanguages, name) ? name : 'text';
return await codeToHtml(code, {
lang,
themes: {
light: 'github-light',
dark: 'github-dark'
},
defaultColor: 'light'
});
}