import assert from "node:assert/strict"; import test from "node:test"; import { EditorState } from "@codemirror/state"; import { indentUnit } from "@codemirror/language"; import { markdown, markdownLanguage } from "@codemirror/lang-markdown"; import { noteFormats } from "../../../src/lib/editor/noteFormats.ts"; import { indentListItems, listLabels, orderedLabel, outdentListItems } from "../../../src/lib/editor/markdownLists.ts"; const extensions = [markdown({ base: markdownLanguage, extensions: [noteFormats] }), indentUnit.of(" ")]; /** A document with `|` at the caret, or two `|` around a selection. */ function editor(marked: string) { const anchor = marked.indexOf("|"), head = marked.indexOf("|", anchor + 1); const doc = marked.replaceAll("|", ""); return EditorState.create({ doc, selection: { anchor, head: head < 0 ? anchor : head - 1 }, extensions }); } function run(move: typeof indentListItems, marked: string) { const state = editor(marked), specs = move(state); if (!specs) return null; const next = state.update(...specs).state; const { from, to } = next.selection.main; const doc = next.doc.toString(); return from === to ? doc.slice(0, from) + "|" + doc.slice(from) : doc.slice(0, from) + "|" + doc.slice(from, to) + "|" + doc.slice(to); } test("tab makes a nested list and renumbers the items after it", () => { assert.equal(run(indentListItems, "1. Topic\n2. B|\n3. A"), "1. Topic\n 1. B|\n2. A"); }); test("tab on an empty item keeps the caret after its marker", () => { assert.equal(run(indentListItems, "1. Topic\n2.|\n3. A"), "1. Topic\n 1.|\n2. A"); assert.equal(run(indentListItems, "1. Topic\n2. |\n3. A"), "1. Topic\n 1. |\n2. A"); }); test("tab adds the item to the nested list of the previous item", () => { assert.equal(run(indentListItems, "1. A\n 1. x\n2. B|\n3. C"), "1. A\n 1. x\n 2. B|\n2. C"); }); test("tab moves the children of an item with it", () => { assert.equal(run(indentListItems, "1. A\n2. B|\n 1. b\n3. C"), "1. A\n 1. B|\n 1. b\n2. C"); }); test("tab nests each selected item under the first unselected item", () => { assert.equal(run(indentListItems, "1. A\n|2. B\n3. C|\n4. D"), "1. A\n| 1. B\n 2. C|\n2. D"); }); test("tab nests bullet items and keeps their marker", () => { assert.equal(run(indentListItems, "- a\n- b|"), "- a\n - b|"); assert.equal(run(indentListItems, "- [ ] a\n- [x] b|"), "- [ ] a\n - [x] b|"); }); test("tab keeps the first item in place and ignores text outside lists", () => { assert.deepEqual(indentListItems(editor("1. A|\n2. B")), []); assert.equal(indentListItems(editor("Plain| text")), null); assert.equal(indentListItems(editor("1. A\n\n ```\n co|de\n ```")), null); }); test("shift+tab moves a nested item up and makes the later items its children", () => { assert.equal(run(outdentListItems, "1. A\n 1. x\n 2. y|\n 3. z\n2. B"), "1. A\n 1. x\n2. y|\n 1. z\n3. B"); }); test("shift+tab gives the item the marker style of the outer list", () => { assert.equal(run(outdentListItems, "1. A\n - x|\n2. B"), "1. A\n2. x|\n3. B"); assert.equal(run(outdentListItems, "- A\n 1. x|\n- B"), "- A\n- x|\n- B"); }); test("shift+tab keeps a top-level item in place", () => { assert.deepEqual(outdentListItems(editor("1. A|\n2. B")), []); assert.equal(outdentListItems(editor("Plain| text")), null); }); test("ordered labels use numbers, letters, and Roman numerals by level", () => { assert.equal(orderedLabel(1, 3), "3"); assert.equal(orderedLabel(2, 1), "a"); assert.equal(orderedLabel(2, 27), "aa"); assert.equal(orderedLabel(3, 4), "iv"); assert.equal(orderedLabel(4, 2), "2"); assert.equal(orderedLabel(2, 0), "0"); }); test("nested ordered markers get labels and the first level keeps its source", () => { const state = editor("1. A\n 1. x\n 1. y\n 1. z\n2. B|"); assert.deepEqual(listLabels(state).map(label => [state.sliceDoc(label.from, label.to), label.label]), [["1.", "a."], ["1.", "b."], ["1.", "i."]]); }); test("an empty nested marker gets its label before the item has text", () => { const state = editor("1. Topic\n 1.\n2. A|"); assert.deepEqual(listLabels(state).map(label => label.label), ["a."]); });