mirror of
https://github.com/outline/outline.git
synced 2026-11-24 23:00:18 +03:00
fix: Keep preceding character when splitting code block on triple backticks (#14463)
* fix: Keep preceding character when splitting code block on triple backticks * test: Cover dry-run result of triple backtick split
This commit is contained in:
1 parent
4916524ee8
commit
634d8d6493
2 files changed
+66
-13
No files matched your search
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user