Skip to content
Merged
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
18 changes: 16 additions & 2 deletions apps/server/src/provider/documentTextWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface XmlNode {
attrs: Record<string, string>;
children: Array<XmlNode | string>;
spaceCount?: number;
hasText?: boolean;
hasTextChoice?: boolean;
}

function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
Expand All @@ -47,10 +49,22 @@ function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
stack.push(node);
});
parser.on("closetag", () => {
stack.pop();
const node = stack.pop()!;
const parent = stack.at(-1)!;
// Markup-compatibility fallbacks repeat their mc:Choice content, e.g. Word text boxes.
// Keep one when no Choice carries text, such as a chart with a VML caption.
if (node.name === "Fallback" && parent.hasTextChoice) {
parent.children.pop();
return;
}
if (!node.hasText) return;
parent.hasText = true;
if (node.name === "Choice") parent.hasTextChoice = true;
});
const text = (value: string) => {
stack.at(-1)!.children.push(value);
const node = stack.at(-1)!;
node.children.push(value);
if (value.trim() !== "") node.hasText = true;
};
parser.on("text", text);
parser.on("cdata", text);
Expand Down
19 changes: 19 additions & 0 deletions apps/server/src/provider/userInputAttachments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,25 @@ describe("question answer paths", () => {
expected: ["Start", "[Extracted text truncated at document extraction limits.]"],
absent: ["Unneeded"],
},
{
ext: "docx",
bounded: true,
parts: {
"word/document.xml":
'<w:document xmlns:w="urn:w" xmlns:mc="urn:mc"><w:body><w:p><w:r><w:t>Body</w:t></w:r></w:p><w:p><w:r><mc:AlternateContent><mc:Choice Requires="wps"><w:drawing><wps:wsp><wps:txbx><w:txbxContent><w:p><w:r><w:t>Callout box</w:t></w:r></w:p></w:txbxContent></wps:txbx></wps:wsp></w:drawing></mc:Choice><mc:Fallback><w:pict><v:shape><v:textbox><w:txbxContent><w:p><w:r><w:t>Callout box</w:t></w:r></w:p></w:txbxContent></v:textbox></v:shape></w:pict></mc:Fallback></mc:AlternateContent></w:r></w:p></w:body></w:document>',
},
expected: ["Body\nCallout box"],
occurrences: { text: "Callout box", count: 1 },
},
{
ext: "docx",
bounded: true,
parts: {
"word/document.xml":
'<w:document xmlns:w="urn:w" xmlns:mc="urn:mc"><w:body><w:p><w:r><mc:AlternateContent><mc:Choice Requires="cx"><w:drawing><cx:chart/></w:drawing></mc:Choice><mc:Fallback><w:pict><v:shape><v:textbox><w:txbxContent><w:p><w:r><w:t>Chart caption</w:t></w:r></w:p></w:txbxContent></v:textbox></v:shape></w:pict></mc:Fallback></mc:AlternateContent></w:r></w:p></w:body></w:document>',
},
expected: ["Chart caption"],
},
{
ext: "docx",
bounded: true,
Expand Down
Loading