From d289f8b6a30539a4a14fd202fb4b067b789f4ef7 Mon Sep 17 00:00:00 2001 From: Omri SirComp Date: Mon, 18 May 2026 11:17:24 +0300 Subject: [PATCH 1/2] Normalize code fence languages --- src/lib/utils/code.test.ts | 15 +++++++++++++++ src/lib/utils/code.ts | 7 +++++-- 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 src/lib/utils/code.test.ts diff --git a/src/lib/utils/code.test.ts b/src/lib/utils/code.test.ts new file mode 100644 index 0000000000..aab14ba2ff --- /dev/null +++ b/src/lib/utils/code.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, test } from 'vitest'; +import { getCodeHtml, type Language } from './code'; + +describe('getCodeHtml', () => { + test('normalizes code fence language before highlighting', () => { + const html = getCodeHtml({ + content: 'using Appwrite;', + language: 'csharp ' as Language + }); + + expect(html).toContain('language-csharp'); + expect(html).not.toContain('language-csharp '); + expect(html).toContain('hljs-keyword'); + }); +}); diff --git a/src/lib/utils/code.ts b/src/lib/utils/code.ts index 87503c1066..70558f38cf 100644 --- a/src/lib/utils/code.ts +++ b/src/lib/utils/code.ts @@ -116,6 +116,8 @@ hljs.registerAliases(['hcl', 'terraform', 'tf'], { languageName: 'ini' }); export type Language = keyof typeof languages | Platform; +const normalizeLanguage = (language?: Language) => language?.trim() as Language | undefined; + type Args = { content: string; language?: Language; @@ -124,7 +126,8 @@ type Args = { export const getCodeHtml = (args: Args) => { const { content, language, withLineNumbers } = args; - const res = hljs.highlight(content, { language: language ?? 'sh' }).value; + const highlightedLanguage = normalizeLanguage(language) ?? 'sh'; + const res = hljs.highlight(content, { language: highlightedLanguage }).value; const lines = res.split(/\n/g); while (lines.length > 0 && lines[lines.length - 1] === '') { @@ -136,7 +139,7 @@ export const getCodeHtml = (args: Args) => { return carry; }, ''); - return `
${final}
`; }; From 418f9d9f183177ef8a88cb63a6b79bb0376599aa Mon Sep 17 00:00:00 2001 From: Omri SirComp Date: Mon, 18 May 2026 11:48:25 +0300 Subject: [PATCH 2/2] test: cover code block fallback language --- src/lib/utils/code.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib/utils/code.test.ts b/src/lib/utils/code.test.ts index aab14ba2ff..677daa8baa 100644 --- a/src/lib/utils/code.test.ts +++ b/src/lib/utils/code.test.ts @@ -12,4 +12,13 @@ describe('getCodeHtml', () => { expect(html).not.toContain('language-csharp '); expect(html).toContain('hljs-keyword'); }); + + test('uses the fallback language class when language is omitted', () => { + const html = getCodeHtml({ + content: 'echo appwrite' + }); + + expect(html).toContain('language-sh'); + expect(html).not.toContain('language-undefined'); + }); });