From 6a4cc9231f219c0047272c56081f881eab800d74 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 2 Apr 2016 17:14:31 +0100 Subject: Rename images.inline to images.imgElement --- NEWS | 3 +++ README.md | 24 +++++++++++++----------- lib/document-to-html.js | 2 +- lib/images.js | 9 +++++++-- lib/main.js | 2 +- tests/images.tests.js | 13 +++++++++++++ tests/mammoth.tests.js | 2 +- 7 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 tests/images.tests.js diff --git a/NEWS b/NEWS index a0703d2..b7dadec 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,9 @@ * The default ID prefix is now the empty string rather than a random number followed by a hyphen. +* Rename mammoth.images.inline to mammoth.images.imgElement to better reflect + its behaviour. + # 0.3.33 * Improve collapsing of elements when there are empty elements. diff --git a/README.md b/README.md index 00fa1b2..98079c6 100644 --- a/README.md +++ b/README.md @@ -212,10 +212,10 @@ For instance, the following would replicate the default behaviour: ```javascript var options = { - convertImage: mammoth.images.inline(function(element) { - return element.read("base64").then(function(imageBuffer) { + convertImage: mammoth.images.imgElement(function(image) { + return image.read("base64").then(function(imageBuffer) { return { - src: "data:" + element.contentType + ";base64," + imageBuffer + src: "data:" + image.contentType + ";base64," + imageBuffer }; }); }) @@ -459,9 +459,9 @@ Each message has the following properties: #### Image converters -An inline image converter can be created by calling `mammoth.images.inline(func)`. -This creates an inline `` element for each image in the original docx. -`func` should be a function that has one argument called `element`. +An image converter can be created by calling `mammoth.images.imgElement(func)`. +This creates an `` element for each image in the original docx. +`func` should be a function that has one argument `image`. This argument is the image element being converted, and has the following properties: @@ -470,16 +470,18 @@ and has the following properties: * `contentType`: the content type of the image, such as `image/png`. -`func` should return an object (or a promise containing an object) with a `src` property, -which will be used as the `src` attribute on the `` element. +`func` should return an object (or a promise of an object) of attributes for the `` element. +At a minimum, this should include the `src` attribute. +If any alt text is found for the image, +this will be automatically added to the element's attributes. For instance, the following replicates the default image conversion: ```javascript -mammoth.images.inline(function(element) { - return element.read("base64").then(function(imageBuffer) { +mammoth.images.imgElement(function(image) { + return image.read("base64").then(function(imageBuffer) { return { - src: "data:" + element.contentType + ";base64," + imageBuffer + src: "data:" + image.contentType + ";base64," + imageBuffer }; }); }) diff --git a/lib/document-to-html.js b/lib/document-to-html.js index 83c3b5f..c084403 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -159,7 +159,7 @@ function DocumentConversion(options) { } } - var defaultConvertImage = images.inline(function(element) { + var defaultConvertImage = images.imgElement(function(element) { return element.read("base64").then(function(imageBuffer) { return { src: "data:" + element.contentType + ";base64," + imageBuffer diff --git a/lib/images.js b/lib/images.js index d7c2ebc..7529673 100644 --- a/lib/images.js +++ b/lib/images.js @@ -1,10 +1,12 @@ +var _ = require("underscore"); + var promises = require("./promises"); var Html = require("./html"); -exports.inline = function(func) { +exports.imgElement = function(func) { return function(element, messages, callback) { return promises.when(func(element)).then(function(result) { - var attributes = {src: result.src}; + var attributes = _.clone(result); if (element.altText) { attributes.alt = element.altText; } @@ -14,3 +16,6 @@ exports.inline = function(func) { }, callback); }; }; + +// Undocumented, but retained for backwards-compatibility with 0.3.x +exports.inline = exports.imgElement; diff --git a/lib/main.js b/lib/main.js index 8be5a3f..f59064f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -22,7 +22,7 @@ function main(argv) { var basename = path.basename(docxPath, ".docx"); outputPath = path.join(outputDir, basename + ".html"); var imageIndex = 0; - options.convertImage = images.inline(function(element) { + options.convertImage = images.imgElement(function(element) { imageIndex++; var extension = element.contentType.split("/")[1]; var filename = imageIndex + "." + extension; diff --git a/tests/images.tests.js b/tests/images.tests.js new file mode 100644 index 0000000..7aac5a2 --- /dev/null +++ b/tests/images.tests.js @@ -0,0 +1,13 @@ +var assert = require("assert"); + +var mammoth = require("../"); + +var testing = require("./testing"); +var test = testing.test; + + +describe('images', function() { + test('mammoth.images.inline() should be an alias of mammoth.images.imgElement()', function() { + assert.ok(mammoth.images.inline === mammoth.images.imgElement); + }); +}); diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js index 9d057f6..801bb02 100644 --- a/tests/mammoth.tests.js +++ b/tests/mammoth.tests.js @@ -174,7 +174,7 @@ describe('mammoth', function() { test('src of inline images can be changed', function() { var docxPath = path.join(__dirname, "test-data/tiny-picture.docx"); - var convertImage = mammoth.images.inline(function(element) { + var convertImage = mammoth.images.imgElement(function(element) { return element.read("base64").then(function(encodedImage) { return {src: encodedImage.substring(0, 2) + "," + element.contentType}; }); -- cgit v1.2.3