diff options
| author | Michael Williamson <mike@zwobble.org> | 2018-01-26 23:30:13 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2018-01-26 23:30:13 +0000 |
| commit | 8b9ca6ca68fec89926cfb02a96506bbee54a5b2c (patch) | |
| tree | 34f701ec9078d533d86ad63fc6aa11dbd91ad278 /lib/docx/docx-reader.js | |
| parent | 9196e8bc6f2ff2c59a9b4fde4813e7bc26b8c23d (diff) | |
Find comments part using document relationships
Diffstat (limited to 'lib/docx/docx-reader.js')
| -rw-r--r-- | lib/docx/docx-reader.js | 63 |
1 files changed, 49 insertions, 14 deletions
diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index cbeaccd..e6dd798 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -83,29 +83,64 @@ function read(docxFile, input) { function findPartPaths(docxFile) { return readPackageRelationships(docxFile).then(function(packageRelationships) { - var documentFilename = findDocumentFilename(docxFile, packageRelationships); - return { - mainDocument: documentFilename, - comments: "word/comments.xml", - endnotes: "word/endnotes.xml", - footnotes: "word/footnotes.xml", - numbering: "word/numbering.xml", - styles: "word/styles.xml" - }; + var mainDocumentPath = findPartPath({ + docxFile: docxFile, + relationships: packageRelationships, + relationshipType: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", + basePath: "", + fallbackPath: "word/document.xml" + }); + + if (!docxFile.exists(mainDocumentPath)) { + throw new Error("Could not find main document part. Are you sure this is a valid .docx file?"); + } + + return xmlFileReader({ + filename: relationshipsFilename(mainDocumentPath), + readElement: relationshipsReader.readRelationships, + defaultValue: relationshipsReader.defaultValue + })(docxFile).then(function(documentRelationships) { + function findPartRelatedToMainDocument(options) { + return findPartPath({ + docxFile: docxFile, + relationships: documentRelationships, + relationshipType: options.relationshipType, + basePath: zipfile.splitPath(mainDocumentPath).dirname, + fallbackPath: options.fallbackPath + }); + } + + return { + mainDocument: mainDocumentPath, + comments: findPartRelatedToMainDocument({ + relationshipType: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments", + fallbackPath: "word/comments.xml" + }), + endnotes: "word/endnotes.xml", + footnotes: "word/footnotes.xml", + numbering: "word/numbering.xml", + styles: "word/styles.xml" + }; + }); }); } -function findDocumentFilename(docxFile, relationships) { - var officeDocumentType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"; - var targets = relationships.findTargetsByType(officeDocumentType).concat(["word/document.xml"]); +function findPartPath(options) { + var docxFile = options.docxFile; + var relationships = options.relationships; + var relationshipType = options.relationshipType; + var basePath = options.basePath; + var fallbackPath = options.fallbackPath; + + var targets = relationships.findTargetsByType(relationshipType); var normalisedTargets = targets.map(function(target) { - return stripPrefix(target, "/"); + return stripPrefix(zipfile.joinPath(basePath, target), "/"); }); var validTargets = normalisedTargets.filter(function(target) { return docxFile.exists(target); }); if (validTargets.length === 0) { - throw new Error("Could not find main document part. Are you sure this is a valid .docx file?"); + return fallbackPath; } else { return validTargets[0]; } |
