UNPKG

mammoth-style

Version:

Convert Word documents (.docx files) to HTML (with style)

113 lines (98 loc) 3.75 kB
var _ = require("underscore"); var docxReader = require("./docx/docx-reader"); var docxStyleMap = require("./docx/style-map"); var DocumentConverter = require("./document-to-html").DocumentConverter; var readStyle = require("./style-reader").readStyle; var readOptions = require("./options-reader").readOptions; var unzip = require("./unzip"); var Result = require("./results").Result; exports.convertToHtml = convertToHtml; exports.convertToMarkdown = convertToMarkdown; exports.convert = convert; exports.extractRawText = extractRawText; exports.images = require("./images"); exports.transforms = require("./transforms"); exports.underline = require("./underline"); exports.embedStyleMap = embedStyleMap; exports.readEmbeddedStyleMap = readEmbeddedStyleMap; function convertToHtml(input, options) { return convert(input, options); } function convertToMarkdown(input, options) { var markdownOptions = Object.create(options || {}); markdownOptions.outputFormat = "markdown"; return convert(input, markdownOptions); } function convert(input, options) { options = Object.create(options || {}); return unzip.openZip(input) .tap(function(docxFile) { if (!options.styleMap) { return docxStyleMap.readStyleMap(docxFile).then(function(styleMap) { options.styleMap = styleMap || ""; }); } }) .then(function(docxFile) { var fullOptions = readOptions(options); return docxReader.read(docxFile, input) .then(function(documentResult) { return documentResult.map(fullOptions.transformDocument); }) .then(function(documentResult) { return convertDocumentToHtml(documentResult, fullOptions); }); }); } function readEmbeddedStyleMap(input) { return unzip.openZip(input) .then(docxStyleMap.readStyleMap); } function convertDocumentToHtml(documentResult, options) { var parsedOptions = Object.create(options); var styleMapResult = parseStyleMap(options.styleMap); parsedOptions.styleMap = styleMapResult.value; var documentConverter = new DocumentConverter(parsedOptions); return documentResult.flatMapThen(function(document) { return styleMapResult.flatMapThen(function(styleMap) { return documentConverter.convertToHtml(document); }); }); } function parseStyleMap(styleMap) { return Result.combine((styleMap || []).map(readStyle)) .map(function(styleMap) { return styleMap.filter(function(styleMapping) { return !!styleMapping; }); }); } function extractRawText(input) { return unzip.openZip(input) .then(docxReader.read) .then(function(documentResult) { return documentResult.map(convertElementToRawText); }); } function convertElementToRawText(element) { if (element.type === "text") { return element.value; } else { var tail = element.type === "paragraph" ? "\n\n" : ""; return (element.children || []).map(convertElementToRawText).join("") + tail; } } function embedStyleMap(input, styleMap) { return unzip.openZip(input) .tap(function(docxFile) { return docxStyleMap.writeStyleMap(docxFile, styleMap); }) .then(function(docxFile) { return { toBuffer: docxFile.toBuffer }; }); } exports.styleMapping = function() { throw new Error('Use a raw string instead of mammoth.styleMapping e.g. "p[style-name=\'Title\'] => h1" instead of mammoth.styleMapping("p[style-name=\'Title\'] => h1")'); };