diff --git a/plugs/editor/outline.test.ts b/plugs/editor/outline.test.ts index aa8201e3..5955fb8c 100644 --- a/plugs/editor/outline.test.ts +++ b/plugs/editor/outline.test.ts @@ -13,12 +13,17 @@ import { parseMarkdown } from "../../client/markdown_parser/parser.ts"; const CURSOR = "|^|"; function applyOp( - op: (text: string, cursor: number) => OutlineResult, + op: ( + text: string, + tree: ReturnType, + cursor: number, + ) => OutlineResult, input: string, ): string { const pos = input.indexOf(CURSOR); const clean = input.slice(0, pos) + input.slice(pos + CURSOR.length); - const result = op(clean, pos); + const tree = parseMarkdown(clean); + const result = op(clean, tree, pos); if (result === null || result === "blocked") { return input; // no-op returns unchanged } @@ -394,11 +399,11 @@ Body 1b ## Second Body 2a `; - const after = moveDown(original, 4); + const after = moveDown(original, parseMarkdown(original), 4); if (after === null || after === "blocked") { throw new Error(`moveDown returned ${after}`); } - const back = moveUp(after.text, after.cursor); + const back = moveUp(after.text, parseMarkdown(after.text), after.cursor); if (back === null || back === "blocked") { throw new Error(`moveUp returned ${back}`); } diff --git a/plugs/editor/outline.ts b/plugs/editor/outline.ts index 4a5e8df1..1cdcd219 100644 --- a/plugs/editor/outline.ts +++ b/plugs/editor/outline.ts @@ -1,4 +1,5 @@ -import { editor } from "@silverbulletmd/silverbullet/syscalls"; +import { editor, markdown } from "@silverbulletmd/silverbullet/syscalls"; +import type { ParseTree } from "@silverbulletmd/silverbullet/lib/tree"; import { indent as indentOp, moveDown as moveDownOp, @@ -9,13 +10,15 @@ import { async function applyOutlineOp( op: ( text: string, + tree: ParseTree, cursor: number, ) => { text: string; cursor: number } | "blocked" | null, fallback?: () => void, ) { const cursorPos = await editor.getCursor(); const text = await editor.getText(); - const result = op(text, cursorPos); + const tree = await markdown.parseMarkdown(text); + const result = op(text, tree, cursorPos); if (result === "blocked") { await editor.flashNotification("Cannot move item further", "error"); return; diff --git a/plugs/editor/outline_ops.ts b/plugs/editor/outline_ops.ts index ece913ab..9a5216f9 100644 --- a/plugs/editor/outline_ops.ts +++ b/plugs/editor/outline_ops.ts @@ -1,4 +1,3 @@ -import { parseMarkdown } from "../../client/markdown_parser/parser.ts"; import { addParentPointers, findParentMatching, @@ -228,10 +227,10 @@ function renumberOrderedList( */ function move( text: string, + tree: ParseTree, cursor: number, direction: "up" | "down", ): OutlineResult { - const tree = parseMarkdown(text); const ctx = detectContext(tree, cursor); if (!ctx) { return null; @@ -248,10 +247,10 @@ function move( } } -export const moveUp = (text: string, cursor: number) => - move(text, cursor, "up"); -export const moveDown = (text: string, cursor: number) => - move(text, cursor, "down"); +export const moveUp = (text: string, tree: ParseTree, cursor: number) => + move(text, tree, cursor, "up"); +export const moveDown = (text: string, tree: ParseTree, cursor: number) => + move(text, tree, cursor, "down"); /** * Indents or outdents the outline element at cursor, dispatching to the @@ -259,10 +258,10 @@ export const moveDown = (text: string, cursor: number) => */ function adjustLevel( text: string, + tree: ParseTree, cursor: number, delta: 1 | -1, ): OutlineResult { - const tree = parseMarkdown(text); const ctx = detectContext(tree, cursor); if (!ctx) { return null; @@ -282,10 +281,10 @@ function adjustLevel( } } -export const indent = (text: string, cursor: number) => - adjustLevel(text, cursor, 1); -export const outdent = (text: string, cursor: number) => - adjustLevel(text, cursor, -1); +export const indent = (text: string, tree: ParseTree, cursor: number) => + adjustLevel(text, tree, cursor, 1); +export const outdent = (text: string, tree: ParseTree, cursor: number) => + adjustLevel(text, tree, cursor, -1); /** * Swaps a list item with its adjacent sibling, renumbering if in an ordered