From c28f9e05b4df170c741b9884265b73408f459a7c Mon Sep 17 00:00:00 2001 From: adarshsm <24850536+adarshsm@users.noreply.github.com> Date: Sat, 29 Aug 2026 19:16:04 +0530 Subject: [PATCH] fix(core): use the default language for a code block typed without one Typing ``` with no language suffix stored an empty string as the block's language: the input rule fell back to the raw match when getLanguageId found nothing to resolve. createLanguageSelect then threw "Language is not supported." because "" is not in supportedLanguages, and the block was left half-initialised, which is what broke the cursor and made Backspace and Delete misbehave inside it. Fall back to the configured defaultLanguage instead, so the block is created with a language a highlighter can actually render. The two tests that asserted the empty language encoded this behaviour and now assert the default. --- packages/core/src/blocks/Code/block.test.ts | 8 ++++---- .../helpers/extensions/CodeKeyboardShortcutsExtension.ts | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/core/src/blocks/Code/block.test.ts b/packages/core/src/blocks/Code/block.test.ts index 9ce39df387..00f3a71eee 100644 --- a/packages/core/src/blocks/Code/block.test.ts +++ b/packages/core/src/blocks/Code/block.test.ts @@ -82,12 +82,12 @@ describe("Code block input rule", () => { expect((block.props as any).language).toBe("ts"); }); - it("converts ``` + space into a codeBlock with empty language", () => { + it("converts ``` + space into a codeBlock with the default language", () => { typeString(editor, "``` "); const block = editor.document[0]; expect(block.type).toBe("codeBlock"); - expect((block.props as any).language).toBe(""); + expect((block.props as any).language).toBe("text"); }); it("converts ```javascript + space into a codeBlock", () => { @@ -138,13 +138,13 @@ describe("Code block input rule", () => { expect(block.content).toEqual([]); }); - it("converts ``` + Enter into a codeBlock with empty language", () => { + it("converts ``` + Enter into a codeBlock with the default language", () => { typeString(editor, "```"); pressKey(editor, "Enter"); const block = editor.document[0]; expect(block.type).toBe("codeBlock"); - expect((block.props as any).language).toBe(""); + expect((block.props as any).language).toBe("text"); }); it("converts ```javascript + Enter into a codeBlock", () => { diff --git a/packages/core/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts b/packages/core/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts index f6b69f6c69..a36e92159b 100644 --- a/packages/core/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +++ b/packages/core/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts @@ -103,8 +103,13 @@ export const CodeKeyboardShortcutsExtension = find: /^```(.*?)\s$/, replace: ({ match }) => { const languageName = match[1].trim(); + // Without a language suffix there is nothing to resolve, so fall + // back to the default rather than storing an empty language that + // no highlighter can render. const attributes = { - language: getLanguageId(options, languageName) ?? languageName, + language: + getLanguageId(options, languageName) ?? + (languageName || options.defaultLanguage || "text"), }; return {