Skip to content

Commit 51f5909

Browse files
committed
Preserve DOCX manual line breaks
1 parent 3bf2621 commit 51f5909

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

sdk/typescript/src/knowledge-base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ function extractDocx(path: string, bytes: Uint8Array): string {
193193
return decodeXml(
194194
xml
195195
.replace(/<\/(?:\w+:)?p\s*>/gu, "\n")
196+
.replace(/<(?:\w+:)?br\b[^>]*\/>/gu, "\n")
196197
.replace(/<(?:\w+:)?tab\b[^>]*\/>/gu, "\t")
197198
.replace(/<[^>]+>/gu, ""),
198199
);

sdk/typescript/tests-ts/knowledge-base.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ async function extractedDocuments(path: string): Promise<string[]> {
4141
);
4242
}
4343

44-
function docx(text: string): Uint8Array {
44+
function docx(text: string, secondLine?: string): Uint8Array {
4545
return zipSync({
4646
"word/document.xml": strToU8(
47-
`<?xml version="1.0"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>${text}</w:t></w:r></w:p></w:body></w:document>`,
47+
`<?xml version="1.0"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>${text}</w:t></w:r>${secondLine === undefined ? "" : `<w:br/><w:r><w:t>${secondLine}</w:t></w:r>`}</w:p></w:body></w:document>`,
4848
),
4949
});
5050
}
@@ -111,14 +111,17 @@ describe("scan knowledge bases", () => {
111111
join(root, "architecture.pdf"),
112112
pdf("Payment service boundary"),
113113
);
114-
await writeFile(join(root, "threat-model.docx"), docx("SSRF &amp; IDOR"));
114+
await writeFile(
115+
join(root, "threat-model.docx"),
116+
docx("SSRF &amp; IDOR", "Review authentication"),
117+
);
115118

116119
const knowledgeBase = await prepareKnowledgeBase([root]);
117120
temporaryDirectories.push(knowledgeBase.path);
118121
const documents = await extractedDocuments(knowledgeBase.path);
119122

120123
expect(documents).toContain("Payment service boundary");
121-
expect(documents).toContain("SSRF & IDOR\n");
124+
expect(documents).toContain("SSRF & IDOR\nReview authentication\n");
122125
});
123126

124127
test("cleans up documents and rediscovers directory contents on later runs", async () => {

0 commit comments

Comments
 (0)