fix: trust arborium availability for unsupported labels

This commit is contained in:
hazre committed 2026-03-27 12:37:56 +01:00
1 parent 240ced593e
commit 9a15cafc02
2 files changed
+30 -27

No files matched your search

+24 -4
View File
@@ -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 () => '<pre data-language="unexpected"></pre>');
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();
}
);
+6 -23
View File
@@ -4,17 +4,6 @@ type ArboriumModuleWithAvailability = ArboriumModule & {
isLanguageAvailable?: (language: string) => boolean | Promise<boolean>;
};
const PLAIN_LANGUAGES = new Set([
'txt',
'plaintext',
'plain',
'text',
'log',
'csv',
'makefile',
'make',
]);
const LANGUAGE_COMPATIBILITY: Record<string, string> = {
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)) {