aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2018-01-16 22:21:21 +0000
committerMichael Williamson <mike@zwobble.org>2018-01-16 22:21:21 +0000
commitcc1b5b19480befc5611a469424a5ae16f9ebce21 (patch)
tree4774f2cbbae87b2b723902d7154b171c2060db78 /lib
parent062eb4ff3fea9c49cfce841327d01ddd0099264e (diff)
Read types of relationships
Diffstat (limited to 'lib')
-rw-r--r--lib/docx/body-reader.js8
-rw-r--r--lib/docx/docx-reader.js2
-rw-r--r--lib/docx/relationships-reader.js37
3 files changed, 38 insertions, 9 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js
index 5958efe..5264d55 100644
--- a/lib/docx/body-reader.js
+++ b/lib/docx/body-reader.js
@@ -236,7 +236,7 @@ function BodyReader(options) {
}
if (relationshipId) {
- var href = relationships[relationshipId].target;
+ var href = relationships.findTargetByRelationshipId(relationshipId);
if (anchor) {
href = uris.replaceFragment(href, anchor);
}
@@ -427,11 +427,11 @@ function BodyReader(options) {
function findBlipImageFile(blip) {
var embedRelationshipId = blip.attributes["r:embed"];
- var linkRelationshipid = blip.attributes["r:link"];
+ var linkRelationshipId = blip.attributes["r:link"];
if (embedRelationshipId) {
return findEmbeddedImageFile(embedRelationshipId);
} else {
- var imagePath = relationships[linkRelationshipid].target;
+ var imagePath = relationships.findTargetByRelationshipId(linkRelationshipId);
return {
path: imagePath,
read: files.read.bind(files, imagePath)
@@ -452,7 +452,7 @@ function BodyReader(options) {
}
function findEmbeddedImageFile(relationshipId) {
- var path = uris.uriToZipEntryName("word", relationships[relationshipId].target);
+ var path = uris.uriToZipEntryName("word", relationships.findTargetByRelationshipId(relationshipId));
return {
path: path,
read: docxFile.read.bind(docxFile, path)
diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js
index db15b95..54f2a3b 100644
--- a/lib/docx/docx-reader.js
+++ b/lib/docx/docx-reader.js
@@ -91,7 +91,7 @@ function readXmlFileWithBody(filename, options, func) {
var readRelationshipsFromZipFile = xmlFileReader({
filename: relationshipsFilename(filename),
readElement: relationshipsReader.readRelationships,
- defaultValue: {}
+ defaultValue: relationshipsReader.defaultValue
});
return readRelationshipsFromZipFile(options.docxFile).then(function(relationships) {
diff --git a/lib/docx/relationships-reader.js b/lib/docx/relationships-reader.js
index 93fc618..4db172e 100644
--- a/lib/docx/relationships-reader.js
+++ b/lib/docx/relationships-reader.js
@@ -1,14 +1,43 @@
exports.readRelationships = readRelationships;
+exports.defaultValue = new Relationships([]);
+exports.Relationships = Relationships;
function readRelationships(element) {
- var relationships = {};
+ var relationships = [];
element.children.forEach(function(child) {
if (child.name === "{http://schemas.openxmlformats.org/package/2006/relationships}Relationship") {
- relationships[child.attributes.Id] = {
- target: child.attributes.Target
+ var relationship = {
+ relationshipId: child.attributes.Id,
+ target: child.attributes.Target,
+ type: child.attributes.Type
};
+ relationships.push(relationship);
}
});
- return relationships;
+ return new Relationships(relationships);
+}
+
+function Relationships(relationships) {
+ var targetsByRelationshipId = {};
+ relationships.forEach(function(relationship) {
+ targetsByRelationshipId[relationship.relationshipId] = relationship.target;
+ });
+
+ var targetsByType = {};
+ relationships.forEach(function(relationship) {
+ if (!targetsByType[relationship.type]) {
+ targetsByType[relationship.type] = [];
+ }
+ targetsByType[relationship.type].push(relationship.target);
+ });
+
+ return {
+ findTargetByRelationshipId: function(relationshipId) {
+ return targetsByRelationshipId[relationshipId];
+ },
+ findTargetsByType: function(type) {
+ return targetsByType[type] || [];
+ }
+ };
}