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: '', + language: 'html', + }); + expect(normalizeLanguage).toHaveBeenCalledWith('html'); + expect(detectLanguage).not.toHaveBeenCalled(); + expect(highlight).toHaveBeenCalledWith('html', 'hello
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