diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-08-19 21:33:14 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-08-19 21:33:14 +0100 |
| commit | a274a2f176303bfec529edecb8cc1773daf8ae31 (patch) | |
| tree | 53deca4545f51a17335d96c143716aad7e12dae5 /lib | |
| parent | 041ccdb2b378b233fab7d3c2688ee7875d9bcaa1 (diff) | |
Handle embedding a style map into a document that already has one
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/style-map.js | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/lib/docx/style-map.js b/lib/docx/style-map.js index 76f6903..04597a2 100644 --- a/lib/docx/style-map.js +++ b/lib/docx/style-map.js @@ -1,3 +1,5 @@ +var _ = require("underscore"); + var promises = require("../promises"); var officeXml = require("./office-xml-reader"); var xml = require("../xml"); @@ -6,6 +8,7 @@ exports.writeStyleMap = writeStyleMap; exports.readStyleMap = readStyleMap; +var schema = "http://schemas.zwobble.org/mammoth/style-map"; var styleMapPath = "mammoth/style-map"; function writeStyleMap(docxFile, styleMap) { @@ -18,14 +21,25 @@ function updateRelationships(docxFile) { var relationshipsUri = "http://schemas.openxmlformats.org/package/2006/relationships"; return officeXml.readXmlFromZipFile(docxFile, path) .then(function(relationshipsDocument) { - var relationships = relationshipsDocument.root; - relationships.children.push(xml.element("{" + relationshipsUri + "}Relationship", { - "Id": "rMammothStyleMap", - "Type": "http://schemas.zwobble.org/mammoth/style-map", + var relationshipsContainer = relationshipsDocument.root; + var relationships = relationshipsContainer.children; + var relationshipId = "rMammothStyleMap"; + var attributes = { + "Id": relationshipId, + "Type": schema, "Target": "/" + styleMapPath - })); + }; + + var existingRelationship = _.find(relationships, function(relationship) { + return relationship.attributes.Id === relationshipId; + }); + if (existingRelationship) { + existingRelationship.attributes = attributes; + } else { + relationships.push(xml.element("{" + relationshipsUri + "}Relationship", attributes)); + } var namespaces = {"": relationshipsUri}; - return docxFile.write(path, xml.writeString(relationships, namespaces)); + return docxFile.write(path, xml.writeString(relationshipsContainer, namespaces)); }); } |
