Skip to content
TypeScript

fix(web): recognize plain text aliases in code fences (#23446)

Victor Vogelb665f0b11c35
diff --git a/apps/web/src/composer-code-languages.test.ts b/apps/web/src/composer-code-languages.test.ts
new file mode 100644
index 00000000000..0e8c3f0188f
--- /dev/null
+++ b/apps/web/src/composer-code-languages.test.ts
@@ -0,0 +1,11 @@
+import { expect, it } from "vite-plus/test";
+
+import { codeLanguageEntry, languageOfInfoString } from "./composer-code-languages";
+
+it("recognizes plain-text fence info strings without treating metadata as the language", () => {
+ const entries = ["Text", "TEXT", "plaintext", "txt"].map((language) =>
+ codeLanguageEntry(languageOfInfoString(` ${language} title=notes.txt`)),
+ );
+
+ expect(entries).toEqual(Array.from({ length: 4 }, () => ({ id: "", label: "Plain text" })));
+});
diff --git a/apps/web/src/composer-code-languages.ts b/apps/web/src/composer-code-languages.ts
index 42558162d9f..641b75b6926 100644
--- a/apps/web/src/composer-code-languages.ts
+++ b/apps/web/src/composer-code-languages.ts
@@ -47,12 +47,15 @@ const LANGUAGE_ALIASES: Record<string, string> = {
"c++": "cpp",
js: "javascript",
md: "markdown",
+ plaintext: "",
py: "python",
rb: "ruby",
rs: "rust",
sh: "bash",
shell: "bash",
+ text: "",
ts: "typescript",
+ txt: "",
yml: "yaml",
};
@@ -69,7 +72,7 @@ export function withInfoStringLanguage(info: string, language: string): string {
}
export function codeLanguageEntry(language: string): CodeBlockLanguage | null {
- const id = language === "text" ? "" : (LANGUAGE_ALIASES[language.toLowerCase()] ?? language);
+ const id = LANGUAGE_ALIASES[language.toLowerCase()] ?? language;
return CODE_BLOCK_LANGUAGES.find((entry) => entry.id === id.toLowerCase()) ?? null;
}