From e5e37a6f2cfa04e146d534028fbf291aac2a9fe2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 7 Sep 2015 20:04:58 +0100 Subject: Add support for external images --- lib/docx/body-reader.js | 15 ++++++++++++--- lib/docx/docx-reader.js | 12 +++++++++--- lib/index.js | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 25a263a..4126fde 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -11,6 +11,7 @@ function BodyReader(options) { var relationships = options.relationships; var contentTypes = options.contentTypes; var docxFile = options.docxFile; + var files = options.files; var numbering = options.numbering; var styles = options.styles; @@ -216,9 +217,17 @@ function BodyReader(options) { } function readBlip(element, blip) { - var relationshipId = blip.attributes["r:embed"]; - var imagePath = joinZipPath("word", relationships[relationshipId].target); - var readImage = docxFile.read.bind(docxFile, imagePath); + var embedRelationshipId = blip.attributes["r:embed"]; + var linkRelationshipid = blip.attributes["r:link"]; + var imagePath; + var readImage; + if (embedRelationshipId) { + imagePath = joinZipPath("word", relationships[embedRelationshipId].target); + readImage = docxFile.read.bind(docxFile, imagePath); + } else { + imagePath = relationships[linkRelationshipid].target; + readImage = files.read.bind(files, imagePath); + } var altText = element.first("wp:docPr").attributes.descr; var contentType = contentTypes.findContentType(imagePath); diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index 9b1ec9a..8627022 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -1,5 +1,7 @@ exports.read = read; +var path = require("path"); + var promises = require("../promises"); var documents = require("../documents"); var Result = require("../results").Result; @@ -12,14 +14,17 @@ var contentTypesReader = require("./content-types-reader"); var numberingXml = require("./numbering-xml"); var stylesReader = require("./styles-reader"); var notesReader = require("./notes-reader"); +var Files = require("./files").Files; -function read(docxFile) { +function read(docxFile, input) { + input = input || {}; return promises.props({ contentTypes: readContentTypesFromZipFile(docxFile), numbering: readNumberingFromZipFile(docxFile), styles: readStylesFromZipFile(docxFile), - docxFile: docxFile + docxFile: docxFile, + files: new Files(input.path ? path.dirname(input.path) : null) }).also(function(result) { return { footnotes: readXmlFileWithBody("footnotes", result, function(bodyReader, xml) { @@ -80,7 +85,8 @@ function readXmlFileWithBody(name, options, func) { contentTypes: options.contentTypes, docxFile: options.docxFile, numbering: options.numbering, - styles: options.styles + styles: options.styles, + files: options.files }); return readXmlFromZipFile(options.docxFile, "word/" + name + ".xml") .then(function(xml) { diff --git a/lib/index.js b/lib/index.js index 5613312..69ab111 100644 --- a/lib/index.js +++ b/lib/index.js @@ -39,7 +39,7 @@ function convert(input, options) { }) .then(function(docxFile) { var fullOptions = readOptions(options); - return docxReader.read(docxFile) + return docxReader.read(docxFile, input) .then(function(documentResult) { return documentResult.map(fullOptions.transformDocument); }) -- cgit v1.2.3