mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* fix: Keep preceding character when splitting code block on triple backticks * test: Cover dry-run result of triple backtick split
324 lines
9.4 KiB
TypeScript
324 lines
9.4 KiB
TypeScript
import { baseKeymap, chainCommands } from "prosemirror-commands";
|
||
import { NodeSelection, TextSelection } from "prosemirror-state";
|
||
import { createEditorState, codeBlock, doc, p } from "@shared/test/editor";
|
||
import {
|
||
arrowLeftInCode,
|
||
backspaceInCode,
|
||
deleteInCode,
|
||
enterInCode,
|
||
indentInCode,
|
||
outdentInCode,
|
||
splitCodeBlockOnTripleBackticks,
|
||
} from "./codeFence";
|
||
|
||
/**
|
||
* Helper that runs a command against a code block document with the given
|
||
* selection and returns the resulting code block text content.
|
||
*/
|
||
function runInCodeBlock(
|
||
code: string,
|
||
from: number,
|
||
to: number,
|
||
command: typeof outdentInCode
|
||
) {
|
||
const testDoc = doc([codeBlock(code)]);
|
||
let state = createEditorState(testDoc);
|
||
state = state.apply(
|
||
state.tr.setSelection(TextSelection.create(state.doc, from, to))
|
||
);
|
||
|
||
let result = state;
|
||
command(state, (tr) => {
|
||
result = state.apply(tr);
|
||
});
|
||
|
||
return result.doc.firstChild?.textContent;
|
||
}
|
||
|
||
/**
|
||
* Helper that runs a command against a code block document with a cursor at
|
||
* the given position and returns the resulting cursor position.
|
||
*/
|
||
function selectionAfter(
|
||
code: string,
|
||
pos: number,
|
||
command: typeof outdentInCode
|
||
) {
|
||
const testDoc = doc([codeBlock(code)]);
|
||
let state = createEditorState(testDoc);
|
||
state = state.apply(
|
||
state.tr.setSelection(TextSelection.create(state.doc, pos))
|
||
);
|
||
|
||
let result = state;
|
||
const handled = command(state, (tr) => {
|
||
result = state.apply(tr);
|
||
});
|
||
|
||
return { handled, from: result.selection.from };
|
||
}
|
||
|
||
/**
|
||
* Helper that selects the entire code block, indents it, then outdents it and
|
||
* returns the resulting text content (which should match the input).
|
||
*/
|
||
function indentThenOutdentAll(code: string) {
|
||
const testDoc = doc([codeBlock(code)]);
|
||
let state = createEditorState(testDoc);
|
||
|
||
const selectAll = () => {
|
||
const block = state.doc.firstChild;
|
||
const end = block ? block.nodeSize - 1 : 1;
|
||
return state.apply(
|
||
state.tr.setSelection(TextSelection.create(state.doc, 1, end))
|
||
);
|
||
};
|
||
|
||
state = selectAll();
|
||
indentInCode(state, (tr) => {
|
||
state = state.apply(tr);
|
||
});
|
||
|
||
state = selectAll();
|
||
outdentInCode(state, (tr) => {
|
||
state = state.apply(tr);
|
||
});
|
||
|
||
return state.doc.firstChild?.textContent;
|
||
}
|
||
|
||
describe("outdentInCode", () => {
|
||
it("removes leading spaces from a single line", () => {
|
||
// " a" – cursor after the content (tab size 2)
|
||
const text = runInCodeBlock(" a", 4, 4, outdentInCode);
|
||
expect(text).toBe("a");
|
||
});
|
||
|
||
it("outdents all selected lines", () => {
|
||
const code = " a\n b\n c";
|
||
// select from start of line 1 to end of line 3
|
||
const text = runInCodeBlock(code, 1, code.length + 1, outdentInCode);
|
||
expect(text).toBe("a\nb\nc");
|
||
});
|
||
|
||
it("does not affect lines above the selection", () => {
|
||
// Four lines, each indented two spaces. Select only the last two lines.
|
||
const code = " a\n b\n c\n d";
|
||
// line three starts at position 9 (1-indexed inside the block)
|
||
const startOfLine3 = " a\n b\n".length + 1;
|
||
const text = runInCodeBlock(
|
||
code,
|
||
startOfLine3,
|
||
code.length + 1,
|
||
outdentInCode
|
||
);
|
||
// Lines 1 and 2 must keep their indentation, lines 3 and 4 outdented.
|
||
expect(text).toBe(" a\n b\nc\nd");
|
||
});
|
||
|
||
it("does not affect the line above when selecting a single later line", () => {
|
||
const code = " a\n b\n c";
|
||
const startOfLine3 = " a\n b\n".length + 1;
|
||
const text = runInCodeBlock(
|
||
code,
|
||
startOfLine3,
|
||
code.length + 1,
|
||
outdentInCode
|
||
);
|
||
expect(text).toBe(" a\n b\nc");
|
||
});
|
||
|
||
it("removes only one indent step from already-indented nested lines", () => {
|
||
// Two-space code with a deeper (four-space) nested line. Outdent must
|
||
// remove two spaces per line, not four, even though a four-space run exists.
|
||
const code = "a\n b\n c";
|
||
const text = runInCodeBlock(code, 1, code.length + 1, outdentInCode);
|
||
expect(text).toBe("a\nb\n c");
|
||
});
|
||
});
|
||
|
||
describe("indent then outdent", () => {
|
||
it("round-trips two-space code without removing too many spaces", () => {
|
||
const code = "function () {\n return 1;\n}";
|
||
expect(indentThenOutdentAll(code)).toBe(code);
|
||
});
|
||
|
||
it("round-trips four-space code", () => {
|
||
const code = "def foo():\n return 1";
|
||
expect(indentThenOutdentAll(code)).toBe(code);
|
||
});
|
||
|
||
it("round-trips deeply nested two-space code", () => {
|
||
const code = "function () {\n if (x) {\n return 1;\n }\n}";
|
||
expect(indentThenOutdentAll(code)).toBe(code);
|
||
});
|
||
});
|
||
|
||
describe("indentInCode", () => {
|
||
it("indents all selected lines without touching others", () => {
|
||
const code = "a\nb\nc";
|
||
const startOfLine2 = "a\n".length + 1;
|
||
const text = runInCodeBlock(
|
||
code,
|
||
startOfLine2,
|
||
code.length + 1,
|
||
(state, dispatch) => indentInCode(state, dispatch)
|
||
);
|
||
expect(text).toBe("a\n b\n c");
|
||
});
|
||
});
|
||
|
||
describe("enterInCode", () => {
|
||
it("adds a paragraph below when the code block is selected", () => {
|
||
const testDoc = doc([
|
||
p("before"),
|
||
codeBlock("graph TD; A-->B", "mermaidjs"),
|
||
]);
|
||
let state = createEditorState(testDoc);
|
||
const pos = state.doc.firstChild!.nodeSize;
|
||
state = state.apply(
|
||
state.tr.setSelection(NodeSelection.create(state.doc, pos))
|
||
);
|
||
|
||
const handled = chainCommands(enterInCode, baseKeymap.Enter)(
|
||
state,
|
||
(tr) => {
|
||
state = state.apply(tr);
|
||
}
|
||
);
|
||
|
||
expect(handled).toBe(true);
|
||
expect(state.doc.child(1).textContent).toBe("graph TD; A-->B");
|
||
expect(state.doc.lastChild?.type.name).toBe("paragraph");
|
||
expect(state.doc.childCount).toBe(3);
|
||
});
|
||
});
|
||
|
||
describe("backspaceInCode", () => {
|
||
it("deletes an empty last line", () => {
|
||
const text = runInCodeBlock("hello\n", 7, 7, backspaceInCode);
|
||
expect(text).toBe("hello");
|
||
});
|
||
|
||
it("deletes an empty line between lines", () => {
|
||
const text = runInCodeBlock("hello\n\nworld", 8, 8, backspaceInCode);
|
||
expect(text).toBe("hello\nworld");
|
||
});
|
||
|
||
it("joins a line with the line above", () => {
|
||
const text = runInCodeBlock("hello\nworld", 7, 7, backspaceInCode);
|
||
expect(text).toBe("helloworld");
|
||
});
|
||
|
||
it("does not handle a character that is not a line break", () => {
|
||
const testDoc = doc([codeBlock("hello")]);
|
||
let state = createEditorState(testDoc);
|
||
state = state.apply(
|
||
state.tr.setSelection(TextSelection.create(state.doc, 6))
|
||
);
|
||
expect(backspaceInCode(state)).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe("deleteInCode", () => {
|
||
it("deletes an empty last line", () => {
|
||
const text = runInCodeBlock("hello\n", 6, 6, deleteInCode);
|
||
expect(text).toBe("hello");
|
||
});
|
||
|
||
it("joins a line with the line below", () => {
|
||
const text = runInCodeBlock("hello\nworld", 6, 6, deleteInCode);
|
||
expect(text).toBe("helloworld");
|
||
});
|
||
|
||
it("does not handle a character that is not a line break", () => {
|
||
const testDoc = doc([codeBlock("hello")]);
|
||
let state = createEditorState(testDoc);
|
||
state = state.apply(
|
||
state.tr.setSelection(TextSelection.create(state.doc, 1))
|
||
);
|
||
expect(deleteInCode(state)).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe("arrowLeftInCode", () => {
|
||
it("moves to the end of the line above", () => {
|
||
expect(selectionAfter("hello\nworld", 7, arrowLeftInCode)).toEqual({
|
||
handled: true,
|
||
from: 6,
|
||
});
|
||
});
|
||
|
||
it("moves out of an empty line", () => {
|
||
expect(selectionAfter("hello\n\nworld", 8, arrowLeftInCode)).toEqual({
|
||
handled: true,
|
||
from: 7,
|
||
});
|
||
});
|
||
|
||
it("does not handle a character that is not a line break", () => {
|
||
expect(selectionAfter("hello\nworld", 8, arrowLeftInCode).handled).toBe(
|
||
false
|
||
);
|
||
});
|
||
|
||
it("does not handle the start of the block", () => {
|
||
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);
|
||
});
|
||
});
|