From cd51f075a213f5b1fc43bd234cea73373fe042a6 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 19:04:05 +0000 Subject: [PATCH] fix(edit): apply ride-along clear/run-formatting to every block a markdown edit writes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A whole-paragraph markdown edit that splits one paragraph into several stamps the replaced paragraph's run formatting onto every new block, but the ride-along --clear / --bold / … only reached the first one, so a coloured template placeholder kept its colour on every line after the first. Edit.paragraph now returns all the new blocks and the new Edit.restyleBlocks clears then sets formatting on each paragraph among them (single-op and --batch). The node-targeted clear/set helpers are gone; span ride-alongs use the locator-based clearFormatting/setFormatting. Fixes #60 Triage-Routine: true Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LkctEPJa5FWBzPoeq7imqE --- REFERENCE.md | 5 +-- src/cli/edit/batch.ts | 11 +++--- src/cli/edit/index.tsx | 41 ++++++++++------------ src/core/edit/index.tsx | 78 ++++++++++++++++++----------------------- tests/cli/edit.test.ts | 69 ++++++++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 72 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index abad4e6..7210d29 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -245,8 +245,9 @@ docx delete FILE --batch drop.jsonl # { at } per line — whole blocks ( # --clear bold,italic,highlight,color,size,font,…|all (edit; strip run formatting, keep text) # --bold --italic --underline --strike --color HEX --highlight NAME --shade HEX --font NAME --size PT # --caps --smallcaps --superscript --subscript (edit; SET run formatting on EXISTING text — -# the inverse of --clear. Alone they format a span/paragraph/range in place; with --text they -# fill AND format. Like --clear, applied directly — not recorded as a tracked change.) +# the inverse of --clear. Alone they format a span/paragraph/range in place; with content they +# fill AND format — every paragraph the content writes, when markdown splits one paragraph into +# several. Like --clear, applied directly — not recorded as a tracked change.) # --font-east-asia NAME sets the East-Asian-script font () — separate from # --font because Word resolves w:eastAsia independently of w:ascii/w:hAnsi; --font alone has no # visible effect on Chinese/Japanese/Korean text. Supported by edit, edit --batch, diff --git a/src/cli/edit/batch.ts b/src/cli/edit/batch.ts index d7872c8..cea7561 100644 --- a/src/cli/edit/batch.ts +++ b/src/cli/edit/batch.ts @@ -639,9 +639,9 @@ async function buildApply( }); const replaced = { start: span.start, end: span.start + text.length }; if (clearTags) { - edit.clearFormattingNode(blockRef.node, replaced, clearTags); + edit.clearFormatting(blockRef, replaced, clearTags); } - if (ride) edit.setFormattingNode(blockRef.node, replaced, ride); + if (ride) edit.setFormatting(blockRef, replaced, ride); }; } return () => @@ -676,9 +676,10 @@ async function buildApply( const result = contentNode(); resolvedAuthors.push(...result.resolvedAuthors); if (!restyleFollows) return; - const edit = new Edit(document); - if (clearTags) edit.clearFormattingNode(result.node, null, clearTags); - if (ride) edit.setFormattingNode(result.node, null, ride); + new Edit(document).restyleBlocks(result.nodes, { + clearTags, + format: ride, + }); }; } diff --git a/src/cli/edit/index.tsx b/src/cli/edit/index.tsx index 7021e50..d4216ce 100644 --- a/src/cli/edit/index.tsx +++ b/src/cli/edit/index.tsx @@ -418,10 +418,10 @@ async function commitSpanEdit( end: spanTarget.span.start + spec.text.length, }; if (opts.clearTags) { - edit.clearFormattingNode(blockRef.node, replaced, opts.clearTags); + edit.clearFormatting(blockRef, replaced, opts.clearTags); } if (opts.setFormat) { - edit.setFormattingNode(blockRef.node, replaced, opts.setFormat); + edit.setFormatting(blockRef, replaced, opts.setFormat); } } } catch (error) { @@ -452,12 +452,12 @@ async function commitBlockEdit( let resolvedAuthors: string[] = []; try { const edit = new Edit(document); - // The paragraph node the content edit produced — a combined `--clear` - // strips formatting from THIS node afterward (it may be a freshly spliced - // node, not the original blockRef). - let resultNode: XmlNode | null = null; + // The blocks the content edit produced — a combined `--clear`/run-format + // restyles THESE afterward (freshly spliced nodes, not the original + // blockRef; several when markdown split the paragraph). + let resultNodes: XmlNode[] | null = null; if (opts.spec.kind === "text" || opts.spec.kind === "runs") { - ({ node: resultNode, resolvedAuthors } = edit.paragraph( + ({ nodes: resultNodes, resolvedAuthors } = edit.paragraph( blockRef, opts.spec, { @@ -469,7 +469,7 @@ async function commitBlockEdit( } else if (opts.spec.kind === "markdown") { const resolved = await resolveMarkdownBlocks(document, opts.spec); if (typeof resolved === "number") return resolved; - ({ node: resultNode, resolvedAuthors } = edit.paragraph( + ({ nodes: resultNodes, resolvedAuthors } = edit.paragraph( blockRef, resolved, { @@ -492,26 +492,23 @@ async function commitBlockEdit( } else if (opts.spec.kind === "setFormat") { edit.setFormatting(blockRef, null, opts.spec.format); } else if (opts.spec.kind === "paragraphProps") { - resultNode = edit.paragraphProperties( - blockRef, - opts.spec.paragraphOptions, - { + resultNodes = [ + edit.paragraphProperties(blockRef, opts.spec.paragraphOptions, { authorFlag: opts.authorFlag, track, - }, - ); + }), + ]; } else { return fail("USAGE", "Unsupported edit spec for single-block locator"); } // Combined content + `--clear`/run-formatting: strip then set the named - // formatting on the post-edit paragraph (e.g. `--text "June 8, 2026" - // --clear highlight` or `--text "Title" --bold`). Clear runs first so an - // explicit set wins on any shared property. - if (opts.clearTags && resultNode) { - edit.clearFormattingNode(resultNode, null, opts.clearTags); - } - if (opts.setFormat && resultNode) { - edit.setFormattingNode(resultNode, null, opts.setFormat); + // formatting on every post-edit paragraph (e.g. `--text "June 8, 2026" + // --clear highlight` or `--text "Title" --bold`). + if (resultNodes && (opts.clearTags || opts.setFormat)) { + edit.restyleBlocks(resultNodes, { + clearTags: opts.clearTags, + format: opts.setFormat, + }); } } catch (error) { if (error instanceof EditError) { diff --git a/src/core/edit/index.tsx b/src/core/edit/index.tsx index e60e403..ce152ef 100644 --- a/src/core/edit/index.tsx +++ b/src/core/edit/index.tsx @@ -222,7 +222,7 @@ export class Edit { ); this.reanchorComments(blockRef.node, commentMarkers); // Mutated in place — the same node is the result. - return { node: blockRef.node, resolvedAuthors: listResolvedAuthors() }; + return { nodes: [blockRef.node], resolvedAuthors: listResolvedAuthors() }; } if (spec.kind === "code") { @@ -321,13 +321,15 @@ export class Edit { newParagraphs, ); } - // The first NEW paragraph is the result: untracked it's the spliced-in - // node; tracked, its runs are the very nodes now inside the transition's - // ``. A following clear/format in a combined content+clear edit - // must target only those — never the transition itself, whose `` - // holds the original runs reject restores. + // The NEW blocks are the result: untracked they're the spliced-in nodes; + // tracked, the first one's runs are the very nodes now inside the + // transition's `` and the rest are spliced in after it. A following + // clear/format in a combined content+clear edit must target only those — + // never the transition itself, whose `` holds the original runs + // reject restores — and must reach EVERY new block (issue #60), since the + // run inheritance above stamped the old look on all of them. return { - node: anchorTarget ?? blockRef.node, + nodes: newParagraphs.length > 0 ? newParagraphs : [blockRef.node], resolvedAuthors: listResolvedAuthors(), }; } @@ -586,25 +588,30 @@ export class Edit { clearRunFormatting(blockRef.node, span, tags); } - /** Like `clearFormatting` but targets a paragraph node directly. Used by the - * combined content+clear edit, where the content step may have spliced in a - * fresh paragraph node (so the original blockRef is stale): `paragraph()` - * returns the resulting node and we clear THAT. The message differs from the - * locator path's: here the locator WAS a paragraph; it's the new content - * (e.g. `--markdown` that produced a table) that isn't clearable. */ - clearFormattingNode( - node: XmlNode, - span: { start: number; end: number } | null, - tags: Set, + /** Ride-along restyle of a whole-paragraph content edit's result: strip + * `clearTags`, then set `format` (so an explicit set wins on a shared + * property), on EVERY paragraph the edit produced — a markdown value that + * split into several blocks inherited the old look on each (issue #60). + * Non-paragraph blocks (a markdown table) have no top-level runs and are + * skipped; content with no paragraph at all is refused. */ + restyleBlocks( + nodes: XmlNode[], + restyle: { clearTags?: Set | null; format?: RunFormat | null }, ): void { - if (node.tag !== "w:p") { + const paragraphs = nodes.filter((node) => node.tag === "w:p"); + if (paragraphs.length === 0) { throw new EditError( "USAGE", - "--clear can't apply: the new content isn't a single paragraph", - "Drop --clear (a table/structural block has no run formatting to strip), or clear separately with `edit --at --clear …` after the content edit.", + "Run formatting can't apply: the new content has no paragraph", + "Drop --clear / the formatting flags (a table/structural block has no runs to format), or apply them to its cells with `edit --at tN:rRcC:pK …` after the content edit.", ); } - clearRunFormatting(node, span, tags); + for (const paragraph of paragraphs) { + if (restyle.clearTags) { + clearRunFormatting(paragraph, null, restyle.clearTags); + } + if (restyle.format) setRunFormatting(paragraph, null, restyle.format); + } } /** Set run-level formatting (bold/italic/underline/color/highlight/font/size/ @@ -631,25 +638,6 @@ export class Edit { setRunFormatting(blockRef.node, span, format); } - /** Like `setFormatting` but targets a paragraph node directly — used by the - * combined content+format edit, where the content step may have spliced in a - * fresh paragraph node (so the original blockRef is stale): `paragraph()` / - * `span()` returns the resulting node and we format THAT. */ - setFormattingNode( - node: XmlNode, - span: { start: number; end: number } | null, - format: RunFormat, - ): void { - if (node.tag !== "w:p") { - throw new EditError( - "USAGE", - "Run formatting can't apply: the new content isn't a single paragraph", - "Drop the formatting flags (a table/structural block has no runs to format), or set them separately with `edit --at …` after the content edit.", - ); - } - setRunFormatting(node, span, format); - } - /** Range replace: `pN-pM`. No formatting preservation (Word's empirical * model for paragraph-range replace is "del all old, ins all new"; no * cross-paragraph LCS, and we match it). Rejects tracked ranges that span @@ -733,9 +721,13 @@ export class Edit { } } -/** `Edit.paragraph`'s result: the paragraph node the content edit produced, - * and the other authors whose pending revisions the rebuild resolved. */ -export type ParagraphEditResult = { node: XmlNode; resolvedAuthors: string[] }; +/** `Edit.paragraph`'s result: the blocks the content edit produced (one + * paragraph, or several when markdown split it), and the other authors whose + * pending revisions the rebuild resolved. */ +export type ParagraphEditResult = { + nodes: XmlNode[]; + resolvedAuthors: string[]; +}; /** The paragraph-content specs that produce one or more new paragraphs. * Shared between `Edit.paragraph` (single block) and `Edit.range` (block diff --git a/tests/cli/edit.test.ts b/tests/cli/edit.test.ts index ed2c93f..cb597fd 100644 --- a/tests/cli/edit.test.ts +++ b/tests/cli/edit.test.ts @@ -1758,6 +1758,75 @@ describe("edit content + --clear combined (fill then strip)", () => { }); }); +// Issue #60: a markdown edit that splits one paragraph into several inherits +// the replaced paragraph's run formatting onto EVERY new block, so a ride-along +// clear/set must reach every block too — not just the first. +describe("edit markdown → several paragraphs: ride-along formatting hits every block", () => { + const RED = 'Red placeholder\n'; + + test("batch {markdown, clear} strips the inherited color from every block", async () => { + const path = await docFrom("multi-clear-batch", RED); + const batch = join(tempWorkspace("multi-clear-batch-src"), "b.jsonl"); + await Bun.write( + batch, + `${JSON.stringify({ at: "p0", markdown: "First\n\nSecond", clear: "color" })}\n`, + ); + expect((await runCli("edit", path, "--batch", batch)).exitCode).toBe(0); + const markdown = (await runCli("read", path)).stdout; + expect(markdown).toContain("Second"); + expect(markdown).not.toContain("FF0000"); + }); + + test("single op: markdown + --clear color strips every block", async () => { + const path = await docFrom("multi-clear-single", RED); + const result = await runCli( + "edit", + path, + "--at", + "p0", + "First\n\nSecond", + "--clear", + "color", + ); + expect(result.exitCode).toBe(0); + expect((await runCli("read", path)).stdout).not.toContain("FF0000"); + }); + + test("single op: markdown + --bold bolds every block", async () => { + const path = await docFrom("multi-bold", "placeholder\n"); + const result = await runCli( + "edit", + path, + "--at", + "p0", + "First\n\nSecond", + "--bold", + ); + expect(result.exitCode).toBe(0); + const markdown = (await runCli("read", path)).stdout; + expect(markdown).toContain("**First**"); + expect(markdown).toContain("**Second**"); + }); + + test("tracked: markdown + --clear color strips every inserted block", async () => { + const path = await docFrom("multi-clear-tracked", RED); + const result = await runCli( + "edit", + path, + "--at", + "p0", + "First\n\nSecond", + "--clear", + "color", + "--track", + ); + expect(result.exitCode).toBe(0); + const accepted = (await runCli("read", path)).stdout; + expect(accepted).toContain("Second"); + expect(accepted).not.toContain("FF0000"); + }); +}); + describe("clear-formatting preserves unmodeled rPr children (in-place invariant)", () => { test("--clear all strips formatting tags but keeps an unmodeled ", () => { // The feature mutates rPr in place precisely so props we don't model