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 | |
| parent | 9196e8bc6f2ff2c59a9b4fde4813e7bc26b8c23d (diff) | |
Find comments part using document relationships
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/docx-reader.js | 63 | ||||
| -rw-r--r-- | lib/zipfile.js | 16 |
2 files changed, 63 insertions, 16 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]; } diff --git a/lib/zipfile.js b/lib/zipfile.js index ae51310..8d46d92 100644 --- a/lib/zipfile.js +++ b/lib/zipfile.js @@ -51,7 +51,19 @@ function splitPath(path) { } function joinPath() { - return Array.prototype.filter.call(arguments, function(path) { + var nonEmptyPaths = Array.prototype.filter.call(arguments, function(path) { return path; - }).join("/"); + }); + + var relevantPaths = []; + + nonEmptyPaths.forEach(function(path) { + if (/^\//.test(path)) { + relevantPaths = [path]; + } else { + relevantPaths.push(path); + } + }); + + return relevantPaths.join("/"); } |
