summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-08-16 15:55:58 +0100
committerMichael Williamson <mike@zwobble.org>2015-08-16 15:55:58 +0100
commit26eb141ff6f0167a2506ffb5bf6a65cf2773f409 (patch)
tree2c369c08f36a78d080c7dca79f3191d8da10bf51
parent0363f064b05698eed9826570aad6985ef47a6322 (diff)
Add style map to relationships for document
-rw-r--r--lib/docx/style-map.js20
-rw-r--r--lib/xml/index.js1
-rw-r--r--tests/docx/style-map.tests.js41
3 files changed, 52 insertions, 10 deletions
diff --git a/lib/docx/style-map.js b/lib/docx/style-map.js
index 5020a87..bd770bd 100644
--- a/lib/docx/style-map.js
+++ b/lib/docx/style-map.js
@@ -1,4 +1,6 @@
var promises = require("../promises");
+var officeXml = require("./office-xml-reader");
+var xml = require("../xml");
exports.writeStyleMap = writeStyleMap;
exports.readStyleMap = readStyleMap;
@@ -8,6 +10,24 @@ var styleMapPath = "mammoth/style-map";
function writeStyleMap(docxFile, styleMap) {
docxFile.write(styleMapPath, styleMap);
+ return updateRelationships(docxFile);
+}
+
+function updateRelationships(docxFile) {
+ var path = "word/_rels/document.xml.rels";
+ 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",
+ "Target": "/" + styleMapPath
+ }));
+ var namespaces = {"": relationshipsUri};
+ return docxFile.write(path, xml.writeString(relationships, namespaces));
+ });
+
}
function readStyleMap(docxFile) {
diff --git a/lib/xml/index.js b/lib/xml/index.js
index 0320bc2..a2f572e 100644
--- a/lib/xml/index.js
+++ b/lib/xml/index.js
@@ -4,3 +4,4 @@ exports.Element = nodes.Element;
exports.element = nodes.element;
exports.text = nodes.text;
exports.readString = require("./reader").readString;
+exports.writeString = require("./writer").writeString;
diff --git a/tests/docx/style-map.tests.js b/tests/docx/style-map.tests.js
index d120289..99f1d99 100644
--- a/tests/docx/style-map.tests.js
+++ b/tests/docx/style-map.tests.js
@@ -8,28 +8,49 @@ var test = require("../testing").test;
describe("zipfile", function() {
test('embedded style map can be read after being written', function() {
- var zip = emptyZipFile();
+ var zip = normalDocx();
- styleMap.writeStyleMap(zip, "p => h1");
-
- return styleMap.readStyleMap(zip).then(function(contents) {
- assert.equal(contents, "p => h1");
+ return styleMap.writeStyleMap(zip, "p => h1").then(function() {
+ return styleMap.readStyleMap(zip).then(function(contents) {
+ assert.equal(contents, "p => h1");
+ });
});
});
test('embedded style map is written to separate file', function() {
- var zip = emptyZipFile();
+ var zip = normalDocx();
+
+ return styleMap.writeStyleMap(zip, "p => h1").then(function() {
+ return zip.read("mammoth/style-map", "utf8").then(function(contents) {
+ assert.equal(contents, "p => h1");
+ });
+ });
+ });
+
+ test('embedded style map is referenced in relationships', function() {
+ var zip = normalDocx();
- styleMap.writeStyleMap(zip, "p => h1");
+ var expectedRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
+ '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
+ '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' +
+ '<Relationship Id="rMammothStyleMap" Type="http://schemas.zwobble.org/mammoth/style-map" Target="/mammoth/style-map"/>' +
+ '</Relationships>';
- return zip.read("mammoth/style-map", "utf8").then(function(contents) {
- assert.equal(contents, "p => h1");
+ return styleMap.writeStyleMap(zip, "p => h1").then(function() {
+ return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) {
+ assert.equal(contents, expectedRelationshipsXml);
+ });
});
});
});
-function emptyZipFile() {
+function normalDocx() {
var zip = new JSZip();
+ var originalRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
+ '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
+ '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' +
+ '</Relationships>';
+ zip.file("word/_rels/document.xml.rels", originalRelationshipsXml);
var buffer = zip.generate({type: "arraybuffer"});
return zipfile.openArrayBuffer(buffer);
}