fix: add arborium language compatibility mapping
This commit is contained in:
2 files changed
+184
-7
No files matched your search
@@ -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) => `<pre data-language="${language}">${code}</pre>`
|
||||
);
|
||||
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: '<div />',
|
||||
language: 'jsx',
|
||||
allowDetect: false,
|
||||
},
|
||||
{ loadModule }
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
mode: 'highlighted',
|
||||
html: '<pre data-language="tsx"><div /></pre>',
|
||||
language: 'tsx',
|
||||
});
|
||||
expect(normalizeLanguage).toHaveBeenCalledWith('tsx');
|
||||
expect(detectLanguage).not.toHaveBeenCalled();
|
||||
expect(highlight).toHaveBeenCalledWith('tsx', '<div />');
|
||||
});
|
||||
|
||||
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) => `<pre data-language="${language}">${code}</pre>`
|
||||
);
|
||||
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: '<p>hello</p>',
|
||||
language: 'markup',
|
||||
allowDetect: false,
|
||||
},
|
||||
{ loadModule }
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
mode: 'highlighted',
|
||||
html: '<pre data-language="html"><p>hello</p></pre>',
|
||||
language: 'html',
|
||||
});
|
||||
expect(normalizeLanguage).toHaveBeenCalledWith('html');
|
||||
expect(detectLanguage).not.toHaveBeenCalled();
|
||||
expect(highlight).toHaveBeenCalledWith('html', '<p>hello</p>');
|
||||
});
|
||||
|
||||
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');
|
||||
|
||||
@@ -1,4 +1,24 @@
|
||||
type ArboriumModule = typeof import('@arborium/arborium');
|
||||
type ArboriumModuleWithAvailability = ArboriumModule & {
|
||||
availableLanguages?: string[];
|
||||
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',
|
||||
};
|
||||
|
||||
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<boolean> {
|
||||
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<ArboriumModule>
|
||||
): Promise<ArboriumModule | null> {
|
||||
@@ -60,6 +109,43 @@ export async function highlightCode(
|
||||
deps?: HighlightCodeDeps
|
||||
): Promise<HighlightResult> {
|
||||
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) {
|
||||
|
||||
Reference in new issue
Block a user