Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<w:rFonts w:eastAsia>) — 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,
Expand Down
11 changes: 6 additions & 5 deletions src/cli/edit/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () =>
Expand Down Expand Up @@ -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,
});
};
}

Expand Down
41 changes: 19 additions & 22 deletions src/cli/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
{
Expand All @@ -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,
{
Expand All @@ -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) {
Expand Down
78 changes: 35 additions & 43 deletions src/core/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class Edit {
);
this.reanchorComments(blockRef.node, commentMarkers);
// Mutated in place — the same <w:p> node is the result.
return { node: blockRef.node, resolvedAuthors: listResolvedAuthors() };
return { nodes: [blockRef.node], resolvedAuthors: listResolvedAuthors() };
}

if (spec.kind === "code") {
Expand Down Expand Up @@ -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
// `<w:ins>`. A following clear/format in a combined content+clear edit
// must target only those — never the transition itself, whose `<w:del>`
// 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 `<w:ins>` 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 `<w:del>` 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(),
};
}
Expand Down Expand Up @@ -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<string>,
/** 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<string> | 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 <pN> --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/
Expand All @@ -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 <pN> …` 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
Expand Down Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions tests/cli/edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<span style="color:#FF0000">Red placeholder</span>\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 <w:lang>", () => {
// The feature mutates rPr in place precisely so props we don't model
Expand Down
Loading