From 240ced593e3e5d4bbdcfe628e7e0fa7c36414dad Mon Sep 17 00:00:00 2001 From: hazre Date: Fri, 27 Mar 2026 12:03:47 +0100 Subject: [PATCH] fix: add arborium language compatibility mapping --- src/app/plugins/arborium/runtime.test.ts | 97 ++++++++++++++++++++++++ src/app/plugins/arborium/runtime.ts | 94 +++++++++++++++++++++-- 2 files changed, 184 insertions(+), 7 deletions(-) diff --git a/src/app/plugins/arborium/runtime.test.ts b/src/app/plugins/arborium/runtime.test.ts index 81d31e2f4..7010460f7 100644 --- a/src/app/plugins/arborium/runtime.test.ts +++ b/src/app/plugins/arborium/runtime.test.ts @@ -45,6 +45,103 @@ describe('highlightCode', () => { expect(highlight).toHaveBeenCalledWith('typescript', 'const value = 1;'); }); + it('maps jsx to tsx when Arborium supports tsx', async () => { + const normalizeLanguage = vi.fn((language: string) => language); + const detectLanguage = vi.fn(() => null); + const highlight = vi.fn( + async (language: string, code: string) => `
${code}
` + ); + const module = { + normalizeLanguage, + detectLanguage, + highlight, + availableLanguages: ['tsx', 'html'], + } as unknown as ArboriumModule; + const loadModule = vi.fn(async () => module); + + const { highlightCode } = await import('.'); + + const result: HighlightResult = await highlightCode( + { + code: '
', + language: 'jsx', + allowDetect: false, + }, + { loadModule } + ); + + expect(result).toEqual({ + mode: 'highlighted', + html: '
', + language: 'tsx', + }); + expect(normalizeLanguage).toHaveBeenCalledWith('tsx'); + expect(detectLanguage).not.toHaveBeenCalled(); + expect(highlight).toHaveBeenCalledWith('tsx', '
'); + }); + + it('maps markup to html when Arborium supports html', async () => { + const normalizeLanguage = vi.fn((language: string) => language); + const detectLanguage = vi.fn(() => null); + const highlight = vi.fn( + async (language: string, code: string) => `
${code}
` + ); + const module = { + normalizeLanguage, + detectLanguage, + highlight, + availableLanguages: ['tsx', 'html'], + } as unknown as ArboriumModule; + const loadModule = vi.fn(async () => module); + + const { highlightCode } = await import('.'); + + const result: HighlightResult = await highlightCode( + { + code: '

hello

', + language: 'markup', + allowDetect: false, + }, + { loadModule } + ); + + expect(result).toEqual({ + mode: 'highlighted', + html: '

hello

', + language: 'html', + }); + expect(normalizeLanguage).toHaveBeenCalledWith('html'); + expect(detectLanguage).not.toHaveBeenCalled(); + expect(highlight).toHaveBeenCalledWith('html', '

hello

'); + }); + + it.each(['txt', 'plaintext', 'plain', 'text', 'log', 'csv', 'makefile', 'make'])( + 'returns plain fallback for %s without loading Arborium', + async (language) => { + const loadModule = vi.fn(async () => { + throw new Error('should not load'); + }); + + const { highlightCode } = await import('.'); + + const result: HighlightResult = await highlightCode( + { + code: 'hello, world', + language, + allowDetect: false, + }, + { loadModule } + ); + + expect(result).toEqual({ + mode: 'plain', + html: 'hello, world', + language, + }); + expect(loadModule).not.toHaveBeenCalled(); + } + ); + it('does not detect a language when allowDetect is false', async () => { const normalizeLanguage = vi.fn((language: string) => language); const detectLanguage = vi.fn(() => 'javascript'); diff --git a/src/app/plugins/arborium/runtime.ts b/src/app/plugins/arborium/runtime.ts index 8f9da59d4..c981233c7 100644 --- a/src/app/plugins/arborium/runtime.ts +++ b/src/app/plugins/arborium/runtime.ts @@ -1,4 +1,24 @@ type ArboriumModule = typeof import('@arborium/arborium'); +type ArboriumModuleWithAvailability = ArboriumModule & { + availableLanguages?: string[]; + 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', +}; export interface HighlightCodeInput { code: string; @@ -37,6 +57,35 @@ 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; +} + +async function isLanguageAvailable( + arborium: ArboriumModuleWithAvailability, + language: string +): Promise { + if (Array.isArray(arborium.availableLanguages)) { + return arborium.availableLanguages.includes(language); + } + + if (typeof arborium.isLanguageAvailable === 'function') { + try { + return await arborium.isLanguageAvailable(language); + } catch { + return false; + } + } + + return true; +} + async function loadArborium( loadModule?: () => Promise ): Promise { @@ -60,6 +109,43 @@ export async function highlightCode( deps?: HighlightCodeDeps ): Promise { 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); + } + + let resolvedLanguage: string; + try { + resolvedLanguage = arborium.normalizeLanguage(compatibleLanguage); + } catch { + return plainResult(code, language); + } + + if (!(await isLanguageAvailable(arborium, resolvedLanguage))) { + return plainResult(code, language); + } + + try { + const html = await arborium.highlight(resolvedLanguage, code); + if (html === escapeHtml(code)) { + return plainResult(code, resolvedLanguage); + } + return { + mode: 'highlighted', + html, + language: resolvedLanguage, + }; + } catch { + return plainResult(code, resolvedLanguage); + } + } + const arborium = await loadArborium(loadModule); if (!arborium) { return plainResult(code, language ?? undefined); @@ -67,13 +153,7 @@ export async function highlightCode( let resolvedLanguage: string | null = null; - if (language) { - try { - resolvedLanguage = arborium.normalizeLanguage(language); - } catch { - return plainResult(code, language ?? undefined); - } - } else if (allowDetect) { + if (allowDetect) { try { const detectedLanguage = arborium.detectLanguage(code); if (detectedLanguage) {