diff --git a/src/app/plugins/arborium/runtime.test.ts b/src/app/plugins/arborium/runtime.test.ts index 7010460f..3c6d89ef 100644 --- a/src/app/plugins/arborium/runtime.test.ts +++ b/src/app/plugins/arborium/runtime.test.ts @@ -116,11 +116,29 @@ describe('highlightCode', () => { }); it.each(['txt', 'plaintext', 'plain', 'text', 'log', 'csv', 'makefile', 'make'])( - 'returns plain fallback for %s without loading Arborium', + 'returns plain fallback for %s when Arborium reports it unavailable', async (language) => { - const loadModule = vi.fn(async () => { - throw new Error('should not load'); + const normalizeLanguage = vi.fn((nextLanguage: string) => { + if (nextLanguage === 'txt' || nextLanguage === 'plaintext' || nextLanguage === 'plain') { + return 'text'; + } + if (nextLanguage === 'makefile') { + return 'make'; + } + return nextLanguage; }); + const detectLanguage = vi.fn(() => null); + const highlight = vi.fn(async () => '
');
+      const isLanguageAvailable = vi.fn(
+        async (nextLanguage: string) => !['text', 'log', 'csv', 'make'].includes(nextLanguage)
+      );
+      const module = {
+        normalizeLanguage,
+        detectLanguage,
+        highlight,
+        isLanguageAvailable,
+      } as unknown as ArboriumModule;
+      const loadModule = vi.fn(async () => module);
 
       const { highlightCode } = await import('.');
 
@@ -138,7 +156,9 @@ describe('highlightCode', () => {
         html: 'hello, world',
         language,
       });
-      expect(loadModule).not.toHaveBeenCalled();
+      expect(loadModule).toHaveBeenCalledOnce();
+      expect(normalizeLanguage).toHaveBeenCalledWith(language);
+      expect(highlight).not.toHaveBeenCalled();
     }
   );
 
diff --git a/src/app/plugins/arborium/runtime.ts b/src/app/plugins/arborium/runtime.ts
index c981233c..30d22031 100644
--- a/src/app/plugins/arborium/runtime.ts
+++ b/src/app/plugins/arborium/runtime.ts
@@ -4,17 +4,6 @@ type ArboriumModuleWithAvailability = ArboriumModule & {
   isLanguageAvailable?: (language: string) => boolean | Promise;
 };
 
-const PLAIN_LANGUAGES = new Set([
-  'txt',
-  'plaintext',
-  'plain',
-  'text',
-  'log',
-  'csv',
-  'makefile',
-  'make',
-]);
-
 const LANGUAGE_COMPATIBILITY: Record = {
   jsx: 'tsx',
   markup: 'html',
@@ -57,14 +46,8 @@ function plainResult(code: string, language?: string): HighlightResult {
   return result;
 }
 
-function resolveCompatibleLanguage(language: string): string | null {
-  const lowerLanguage = language.toLowerCase();
-
-  if (PLAIN_LANGUAGES.has(lowerLanguage)) {
-    return null;
-  }
-
-  return LANGUAGE_COMPATIBILITY[lowerLanguage] ?? language;
+function resolveCompatibleLanguage(language: string): string {
+  return LANGUAGE_COMPATIBILITY[language.toLowerCase()] ?? language;
 }
 
 async function isLanguageAvailable(
@@ -111,10 +94,6 @@ export async function highlightCode(
   const { loadModule } = deps ?? {};
   if (language) {
     const compatibleLanguage = resolveCompatibleLanguage(language);
-    if (!compatibleLanguage) {
-      return plainResult(code, language);
-    }
-
     const arborium = await loadArborium(loadModule);
     if (!arborium) {
       return plainResult(code, language);
@@ -168,6 +147,10 @@ export async function highlightCode(
     return plainResult(code, language ?? undefined);
   }
 
+  if (!(await isLanguageAvailable(arborium, resolvedLanguage))) {
+    return plainResult(code, language ?? undefined);
+  }
+
   try {
     const html = await arborium.highlight(resolvedLanguage, code);
     if (html === escapeHtml(code)) {