aboutsummaryrefslogtreecommitdiff
path: root/lib/docx/style-map.js
blob: 04597a2756b280143aa3f2e67c8eda4f016ee910 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var _ = require("underscore");

var promises = require("../promises");
var officeXml = require("./office-xml-reader");
var xml = require("../xml");

exports.writeStyleMap = writeStyleMap;
exports.readStyleMap = readStyleMap;


var schema = "http://schemas.zwobble.org/mammoth/style-map";
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 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(relationshipsContainer, namespaces));
        });
    
}

function readStyleMap(docxFile) {
    if (docxFile.exists(styleMapPath)) {
        return docxFile.read(styleMapPath, "utf8");
    } else {
        return promises.resolve(null);
    }
}