diff --git a/shared/editor/commands/codeFence.test.ts b/shared/editor/commands/codeFence.test.ts index 9e946533cc..e67c5a048b 100644 --- a/shared/editor/commands/codeFence.test.ts +++ b/shared/editor/commands/codeFence.test.ts @@ -8,6 +8,7 @@ import { enterInCode, indentInCode, outdentInCode, + splitCodeBlockOnTripleBackticks, } from "./codeFence"; /** @@ -265,3 +266,58 @@ describe("arrowLeftInCode", () => { expect(selectionAfter("hello", 1, arrowLeftInCode).handled).toBe(false); }); }); + +describe("splitCodeBlockOnTripleBackticks", () => { + /** + * Runs the command with the cursor at the end of the given code and returns + * the text of each resulting block. + */ + function splitAtEnd(code: string) { + const testDoc = doc([codeBlock(code, "javascript")]); + let state = createEditorState(testDoc); + state = state.apply( + state.tr.setSelection(TextSelection.create(state.doc, code.length + 1)) + ); + + let result = state; + const handled = splitCodeBlockOnTripleBackticks(state, (tr) => { + result = state.apply(tr); + }); + expect(splitCodeBlockOnTripleBackticks(state)).toBe(handled); + + const blocks: { type: string; text: string; language: string }[] = []; + result.doc.forEach((node) => { + blocks.push({ + type: node.type.name, + text: node.textContent, + language: node.attrs.language, + }); + }); + + return { handled, blocks }; + } + + it("removes only the typed backticks when splitting", () => { + expect(splitAtEnd("hello``")).toEqual({ + handled: true, + blocks: [ + { type: "code_block", text: "hello", language: "javascript" }, + { type: "code_block", text: "", language: "javascript" }, + ], + }); + }); + + it("keeps a single character before the backticks", () => { + const { handled, blocks } = splitAtEnd("a``"); + expect(handled).toBe(true); + expect(blocks.map((block) => block.text)).toEqual(["a", ""]); + }); + + it("does not split when the backticks are at the start of the block", () => { + expect(splitAtEnd("``").handled).toBe(false); + }); + + it("does not split without two preceding backticks", () => { + expect(splitAtEnd("hello`").handled).toBe(false); + }); +}); diff --git a/shared/editor/commands/codeFence.ts b/shared/editor/commands/codeFence.ts index 5694b637ce..1b96be5beb 100644 --- a/shared/editor/commands/codeFence.ts +++ b/shared/editor/commands/codeFence.ts @@ -333,22 +333,19 @@ export const splitCodeBlockOnTripleBackticks: Command = (state, dispatch) => { return false; } + // The third backtick is not in the document yet, so only remove two. + const contentStart = (findParentNode(isCode)(selection)?.pos ?? 0) + 1; + const backticksStart = from - backticks.length; + + // Do not split when the backticks are at the start of the block. + if (backticksStart <= contentStart) { + return false; + } + if (dispatch) { - // Get position of parent node start - const codeBlockStart = findParentNode(isCode)(selection)?.pos || 0; - const backticksStart = Math.max(0, from - backticks.length - 1); - if (backticksStart <= codeBlockStart) { - return false; - } - tr.delete(backticksStart, from); - - // Split the node at the current position (minus the backticks) - const pos = tr.mapping.map(backticksStart); - tr.split(pos, 1); - + tr.split(tr.mapping.map(backticksStart), 1); dispatch(tr); - return true; } return true;