Skip to content

Commit 100dab8

Browse files
saphidclaude
authored andcommitted
fix(server): document text skips tracked deletions and field codes
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d8a829e commit 100dab8

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎apps/server/src/provider/documentTextWorker.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ const CELL_END = Symbol("cell-end");
1414
type TextPart = string | typeof CELL_START | typeof CELL_END | { fallback: string };
1515

1616
const TRUNCATED = "\n\n[Extracted text truncated at document extraction limits.]";
17+
// Parts the document never displays: tracked deletions and moves, field codes, phonetic guides.
18+
const HIDDEN = new Set([
19+
"script",
20+
"style",
21+
"binary-data",
22+
"rPh",
23+
"delText",
24+
"moveFrom",
25+
"instrText",
26+
"delInstrText",
27+
"tracked-changes",
28+
]);
1729

1830
class Truncation extends Error {}
1931

@@ -67,7 +79,7 @@ function* all(node: XmlNode, name: string | readonly string[]): Generator<XmlNod
6779
}
6880
}
6981
function* text(node: XmlNode): Generator<string, void> {
70-
if (["script", "style", "binary-data", "rPh"].includes(node.name)) return;
82+
if (HIDDEN.has(node.name)) return;
7183
if (["tab", "cell"].includes(node.name)) yield "\t";
7284
else if (["br", "line-break", "cr"].includes(node.name)) yield "\n";
7385
else if (node.spaceCount !== undefined) {
@@ -80,7 +92,7 @@ function* text(node: XmlNode): Generator<string, void> {
8092
}
8193
}
8294
function* paragraphs(node: XmlNode): Generator<TextPart, void> {
83-
if (["script", "style", "binary-data"].includes(node.name)) return;
95+
if (HIDDEN.has(node.name)) return;
8496
for (const child of node.children) {
8597
if (typeof child === "string") yield child;
8698
else if (["br", "line-break", "cr"].includes(child.name)) yield "\n";
@@ -137,11 +149,7 @@ function odfValue(cell: XmlNode): string {
137149
function hasCellText(cell: XmlNode): boolean {
138150
if (odfValue(cell).trim()) return true;
139151
// Space markers can have enormous repeat counts; emptiness never requires expanding them.
140-
if (
141-
["script", "style", "binary-data", "rPh"].includes(cell.name) ||
142-
cell.spaceCount !== undefined
143-
)
144-
return false;
152+
if (HIDDEN.has(cell.name) || cell.spaceCount !== undefined) return false;
145153
return cell.children.some((child) =>
146154
typeof child === "string" ? !!child.trim() : hasCellText(child),
147155
);

‎apps/server/src/provider/userInputAttachments.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,26 @@ describe("question answer paths", () => {
572572
},
573573
expected: ["First\nSecond\nThird"],
574574
},
575+
{
576+
ext: "docx",
577+
bounded: true,
578+
parts: {
579+
"word/document.xml":
580+
'<w:document xmlns:w="urn:w"><w:body><w:p><w:r><w:t>Intro</w:t></w:r><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText> PAGEREF _Toc1 \\h </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r><w:t> 3</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p><w:r><w:t xml:space="preserve">Fee </w:t></w:r><w:del><w:r><w:delText>$500</w:delText></w:r></w:del><w:ins><w:r><w:t>$700</w:t></w:r></w:ins><w:moveFrom><w:r><w:t>Moved</w:t></w:r></w:moveFrom></w:p></w:body></w:document>',
581+
},
582+
expected: ["Intro 3\nFee $700"],
583+
absent: ["PAGEREF", "$500", "Moved"],
584+
},
585+
{
586+
ext: "odt",
587+
bounded: true,
588+
parts: {
589+
"content.xml":
590+
"<document><text><tracked-changes><changed-region><deletion><change-info><creator>Reviewer</creator></change-info><p>$500</p></deletion></changed-region></tracked-changes><p>Fee <change/>$700</p></text></document>",
591+
},
592+
expected: ["Fee $700"],
593+
absent: ["Reviewer", "$500"],
594+
},
575595
{
576596
ext: "docx",
577597
bounded: true,

0 commit comments

Comments
 (0)