aboutsummaryrefslogtreecommitdiff
path: root/lib/index.js
blob: 2d963dc5cb3afd8fe57f3ee495c78c375fc493f5 (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
54
55
56
57
58
59
var docxReader = require("./docx-reader");
var DocumentConverter = require("./document-to-html").DocumentConverter;
var htmlPaths = require("./html-paths");
var documentMatchers = require("./document-matchers");

exports.Converter = Converter;
exports.read = read;
exports.convertDocumentToHtml = convertDocumentToHtml;
exports.htmlPaths = htmlPaths;
exports.standardOptions = {
    styleMap: [
        {
            from: documentMatchers.paragraph("Heading1"),
            to: htmlPaths.topLevelElement("h1")
        },
        {
            from: documentMatchers.paragraph("Heading2"),
            to: htmlPaths.topLevelElement("h2")
        },
        {
            from: documentMatchers.paragraph("Heading3"),
            to: htmlPaths.topLevelElement("h3")
        },
        {
            from: documentMatchers.paragraph("Heading4"),
            to: htmlPaths.topLevelElement("h4")
        },
        {
            from: documentMatchers.paragraph("ListParagraph"),
            to: htmlPaths.elements([
                htmlPaths.element("ul"),
                htmlPaths.element("li", {}, {fresh: true})
            ])
        }
    ]
};

function Converter(options) {
    this._options = options;
}

Converter.prototype.convertToHtml = function(inputOptions) {
    var options = this._options;
    return read(inputOptions)
        .then(function(documentResult) {
            return convertDocumentToHtml(documentResult, options);
        });
}

function read(inputOptions) {
    return docxReader.read(inputOptions);
}

function convertDocumentToHtml(documentResult, options) {
    var documentConverter = new DocumentConverter(options);
    return documentResult.flatMapThen(function(document) {
        return documentConverter.convertToHtml(document);
    });
}