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
25 changes: 16 additions & 9 deletions apps/server/src/provider/documentTextWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,16 +665,19 @@ async function* document(path: string, extension: string): AsyncGenerator<TextPa
.filter((rel) => rel.attrs.TargetMode !== "External")
.map((rel) => [rel.attrs.Id, rel.attrs]),
);
if (extension === ".docx") {
// The package relationship names the main part; Word for the web saves word/document2.xml.
// The package relationship names the main part; Word for the web saves word/document2.xml.
const mainPart = (fallback: string) => {
const target = has("_rels/.rels")
? [...all(read("_rels/.rels"), "Relationship")].find(
(rel) =>
rel.attrs.Type?.endsWith("/officeDocument") && rel.attrs.TargetMode !== "External",
)?.attrs.Target
: undefined;
const main = target && NodePath.posix.normalize(target).replace(/^\//, "");
yield* paragraphs(read(main && has(main) ? main : "word/document.xml"));
return main && has(main) ? main : fallback;
};
if (extension === ".docx") {
yield* paragraphs(read(mainPart("word/document.xml")));
} else if ([".odt", ".ods", ".odp"].includes(extension)) {
const content = read("content.xml");
if (extension === ".odt") yield* paragraphs(content);
Expand All @@ -700,12 +703,18 @@ async function* document(path: string, extension: string): AsyncGenerator<TextPa
}
}
} else if (extension === ".xlsx") {
const workbook = read("xl/workbook.xml");
const rels = relations("xl/_rels/workbook.xml.rels");
const workbookPath = mainPart("xl/workbook.xml");
const workbookDir = NodePath.posix.dirname(workbookPath);
const resolveWorkbookPart = (target: string) =>
NodePath.posix.resolve(`/${workbookDir}`, target).slice(1);
const workbook = read(workbookPath);
const rels = relations(
NodePath.posix.join(workbookDir, "_rels", `${NodePath.posix.basename(workbookPath)}.rels`),
);
const workbookRelations = [...rels.values()];
const workbookPartPath = (part: string) => {
const target = workbookRelations.find((rel) => rel.Type?.endsWith(`/${part}`))?.Target;
return target ? NodePath.posix.resolve("/xl", target).slice(1) : `xl/${part}.xml`;
return resolveWorkbookPart(target || `${part}.xml`);
};
const dateSystem = ["1", "true"].includes(
all(workbook, "workbookPr").next().value?.attrs.date1904 ?? "",
Expand All @@ -731,9 +740,7 @@ async function* document(path: string, extension: string): AsyncGenerator<TextPa
skip(`sheet relationship ${sheet.attrs.name}`);
continue;
}
const name = target.startsWith("/")
? target.slice(1)
: NodePath.posix.normalize(`xl/${target}`);
const name = resolveWorkbookPart(target);
const populated = (cell: XmlNode) => {
const value = all(cell, "v").next().value;
if (cell.attrs.t === "s") {
Expand Down
21 changes: 21 additions & 0 deletions apps/server/src/provider/userInputAttachments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,27 @@ describe("question answer paths", () => {
"45293 [Excel numFmtId=164; format=yyyy-mm-dd; date system 1900]",
],
},
{
ext: "xlsx",
bounded: true,
parts: {
"_rels/.rels":
'<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="docs/book.xml"/></Relationships>',
"docs/book.xml": '<workbook><sheets><sheet name="Data" r:id="a"/></sheets></workbook>',
"docs/_rels/book.xml.rels":
'<Relationships><Relationship Id="a" Target="sheets/data.xml"/><Relationship Id="strings" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="strings.xml"/></Relationships>',
"docs/strings.xml": "<sst><si><t>Harbor</t></si></sst>",
"docs/styles.xml":
'<styleSheet><numFmts><numFmt numFmtId="164" formatCode="yyyy-mm-dd"/></numFmts><cellXfs><xf numFmtId="0"/><xf numFmtId="164"/></cellXfs></styleSheet>',
"docs/sheets/data.xml":
'<worksheet><sheetData><row><c r="A1" t="s"><v>0</v></c><c r="B1" s="1"><v>45293</v></c></row></sheetData></worksheet>',
},
expected: [
"## Sheet: Data",
"Harbor\t45293 [Excel numFmtId=164; format=yyyy-mm-dd; date system 1900]",
],
absent: ["[Skipped document part"],
},
{
ext: "xlsx",
bounded: true,
Expand Down
Loading