UNPKG

file2html-ooxml

Version:
132 lines (131 loc) 6.54 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var file2html = require("file2html"); var mime = require("file2html/lib/mime"); var errors_1 = require("file2html/lib/errors"); var sax_1 = require("file2html-xml-tools/lib/sax"); var file2html_archive_tools_1 = require("file2html-archive-tools"); var parse_core_props_1 = require("./parse-core-props"); var parse_document_content_1 = require("./word/parse-document-content"); var parse_document_styles_1 = require("./word/parse-document-styles"); var parse_document_relations_1 = require("./word/parse-document-relations"); var documentMimeType = mime.lookup('.docx'); // TODO: support Presentations and Spreadsheets var supportedMimeTypes = [documentMimeType]; var OOXMLReader = (function (_super) { __extends(OOXMLReader, _super); function OOXMLReader() { return _super !== null && _super.apply(this, arguments) || this; } OOXMLReader.prototype.read = function (_a) { var fileInfo = _a.fileInfo; var content = fileInfo.content; var byteLength = content.byteLength; return file2html_archive_tools_1.readArchive(content).then(function (archive) { function getInvalidFileError(message) { var archiveTree = Object.keys(archive.files || {}).join(',\n'); return Promise.reject(new Error(errors_1.errorsNamespace + ".invalidFile. Archive: [" + archiveTree + "]." + (message || ''))); } var documentRelations = archive.file('_rels/.rels'); if (!documentRelations) { return getInvalidFileError(); } return documentRelations.async('string').then(function (documentRelationsContent) { var corePropertiesEntryPath; var documentEntryPath; sax_1.parseXML(documentRelationsContent, { onopentag: function (tagName, attrs) { if (tagName === 'Relationship') { var Type = attrs.Type, Target = attrs.Target; if (!Type || !Target) { return; } // `Target` might start from an excess slash symbol var path = Target.replace(/^\//, ''); if (Type.indexOf('officeDocument') > 0) { documentEntryPath = path; } else if (Type.indexOf('core-properties') > 0) { corePropertiesEntryPath = path; } } } }); var documentEntry = documentEntryPath && archive.file(documentEntryPath); if (!documentEntry) { return getInvalidFileError("\ndocumentEntry not found,\n" + documentRelationsContent); } var documentFilename = documentEntryPath.split('/').pop(); var meta = Object.assign({ fileType: file2html.FileTypes.document, mimeType: '', name: '', size: byteLength, creator: '', createdAt: '', modifiedAt: '' }, fileInfo.meta); var queue = []; var dataType = 'string'; var styles = ''; var content = '<div></div>'; var relations = {}; var relationsEntry = archive.file("word/_rels/" + documentFilename + ".rels"); if (relationsEntry) { queue.push(relationsEntry.async(dataType).then(function (data) { return parse_document_relations_1.default(data, archive).then(function (documentRelations) { relations = documentRelations; }); })); } return Promise.all(queue).then(function () { var queue = []; var coreProperties = (corePropertiesEntryPath && archive.file(corePropertiesEntryPath)); if (coreProperties) { queue.push(coreProperties.async(dataType).then(function (data) { return parse_core_props_1.default(data, meta); })); } // is WordProcessingML if (meta.mimeType === documentMimeType) { var stylesEntry = archive.file('word/styles.xml'); if (stylesEntry) { queue.push(stylesEntry.async(dataType).then(function (data) { return parse_document_styles_1.default(data); }).then(function (documentStyles) { styles += '\n' + documentStyles; })); } queue.push(documentEntry.async(dataType).then(function (data) { return parse_document_content_1.default(data, { relations: relations }); }).then(function (data) { styles += '\n' + data.styles; content = data.content; })); } return Promise.all(queue).then(function () { return new file2html.File({ meta: meta, styles: "<style>" + styles + "</style>", content: content }); }); }); }); }); }; OOXMLReader.testFileMimeType = function (mimeType) { return supportedMimeTypes.indexOf(mimeType) >= 0; }; return OOXMLReader; }(file2html.Reader)); exports.default = OOXMLReader;