Skip to content

Commit e0fd84b

Browse files
committed
refactor(ui): use highlight.js native registry
1 parent 319a796 commit e0fd84b

1 file changed

Lines changed: 19 additions & 57 deletions

File tree

‎ui/src/components/markdown.ts‎

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -802,72 +802,34 @@ function splitStableStreamingMarkdown(markdownLocal: string): StreamingMarkdownS
802802
return { boundary, tailHasOpenFence: openFence !== null };
803803
}
804804

805-
for (const [language, definition, aliases] of [
806-
["bash", bash, ["sh", "shell"]],
807-
["cpp", cpp, ["c++", "cxx"]],
808-
["css", css, []],
809-
["diff", diff, ["patch"]],
810-
["go", go, ["golang"]],
811-
["java", java, []],
812-
["javascript", javascript, ["js", "jsx"]],
813-
["json", json, []],
814-
["markdown", markdown, ["md"]],
815-
["python", python, ["py"]],
816-
["rust", rust, ["rs"]],
817-
["typescript", typescript, ["ts", "tsx"]],
818-
["xml", xml, ["html", "svg"]],
819-
["yaml", yaml, ["yml"]],
820-
] as const) {
805+
for (const [language, definition] of Object.entries({
806+
bash,
807+
cpp,
808+
css,
809+
diff,
810+
go,
811+
java,
812+
javascript,
813+
json,
814+
markdown,
815+
python,
816+
rust,
817+
typescript,
818+
xml,
819+
yaml,
820+
})) {
821821
hljs.registerLanguage(language, definition);
822-
if (aliases.length > 0) {
823-
hljs.registerAliases([...aliases], { languageName: language });
824-
}
825-
}
826-
827-
function normalizeHighlightLanguage(lang: string): string {
828-
const normalized = lang.trim().toLowerCase();
829-
if (!normalized) {
830-
return "";
831-
}
832-
const aliases: Record<string, string> = {
833-
"c++": "cpp",
834-
cxx: "cpp",
835-
js: "javascript",
836-
jsx: "javascript",
837-
md: "markdown",
838-
sh: "bash",
839-
shell: "bash",
840-
ts: "typescript",
841-
tsx: "typescript",
842-
};
843-
return aliases[normalized] ?? normalized;
844822
}
845-
846-
const autoHighlightLanguages = [
847-
"bash",
848-
"cpp",
849-
"css",
850-
"diff",
851-
"go",
852-
"java",
853-
"javascript",
854-
"json",
855-
"markdown",
856-
"python",
857-
"rust",
858-
"typescript",
859-
"xml",
860-
"yaml",
861-
];
823+
hljs.registerAliases("shell", { languageName: "bash" });
862824

863825
function highlightCode(text: string, lang: string): string {
864-
const language = normalizeHighlightLanguage(lang);
826+
const language = lang.trim().toLowerCase();
865827
try {
866828
if (language && hljs.getLanguage(language)) {
867829
return hljs.highlight(text, { language, ignoreIllegals: true }).value;
868830
}
869831
if (!language && text.trim()) {
870-
const result = hljs.highlightAuto(text, autoHighlightLanguages);
832+
const result = hljs.highlightAuto(text);
871833
if (result.relevance >= 2) {
872834
return result.value;
873835
}

0 commit comments

Comments
 (0)