summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-05-14 13:39:33 +0100
committerMichael Williamson <mike@zwobble.org>2017-05-14 13:39:33 +0100
commit327b99803151111bcec37af0d2bc5df5baa3506b (patch)
tree6fddd7f45c42b3b123de5ec81ee56f4bcf537516
parentbdf33ea9172f3c32f42197135053e932f725fd91 (diff)
Extract data URI image converter to images module
-rw-r--r--NEWS4
-rw-r--r--README.md2
-rw-r--r--lib/document-to-html.js10
-rw-r--r--lib/images.js14
-rw-r--r--test/images.tests.js24
5 files changed, 43 insertions, 11 deletions
diff --git a/NEWS b/NEWS
index 5a12dca..3d30193 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+# 1.3.7
+
+* Extract the default data URI image converter to the images module.
+
# 1.3.6
* Handle complex fields that do not have a "separate" fldChar.
diff --git a/README.md b/README.md
index 678e846..7cad477 100644
--- a/README.md
+++ b/README.md
@@ -479,6 +479,8 @@ mammoth.images.imgElement(function(image) {
})
```
+`mammoth.images.dataUri` is the default image converter.
+
### Document transforms
**The API for document transforms should be considered unstable,
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index 3a30ed2..d56ad1d 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -181,14 +181,6 @@ function DocumentConversion(options, comments) {
}
}
- var defaultConvertImage = images.imgElement(function(element) {
- return element.read("base64").then(function(imageBuffer) {
- return {
- src: "data:" + element.contentType + ";base64," + imageBuffer
- };
- });
- });
-
function recoveringConvertImage(convertImage) {
return function(image, messages) {
return promises.attempt(function() {
@@ -362,7 +354,7 @@ function DocumentConversion(options, comments) {
},
"commentReference": convertCommentReference,
"comment": convertComment,
- "image": deferredConversion(recoveringConvertImage(options.convertImage || defaultConvertImage)),
+ "image": deferredConversion(recoveringConvertImage(options.convertImage || images.dataUri)),
"table": convertTable,
"tableRow": convertTableRow,
"tableCell": convertTableCell,
diff --git a/lib/images.js b/lib/images.js
index fef0ca3..c7d3ef5 100644
--- a/lib/images.js
+++ b/lib/images.js
@@ -3,7 +3,9 @@ var _ = require("underscore");
var promises = require("./promises");
var Html = require("./html");
-exports.imgElement = function(func) {
+exports.imgElement = imgElement;
+
+function imgElement(func) {
return function(element, messages) {
return promises.when(func(element)).then(function(result) {
var attributes = _.clone(result);
@@ -13,7 +15,15 @@ exports.imgElement = function(func) {
return [Html.freshElement("img", attributes)];
});
};
-};
+}
// Undocumented, but retained for backwards-compatibility with 0.3.x
exports.inline = exports.imgElement;
+
+exports.dataUri = imgElement(function(element) {
+ return element.read("base64").then(function(imageBuffer) {
+ return {
+ src: "data:" + element.contentType + ";base64," + imageBuffer
+ };
+ });
+});
diff --git a/test/images.tests.js b/test/images.tests.js
index 7ba3ec9..a66cdee 100644
--- a/test/images.tests.js
+++ b/test/images.tests.js
@@ -1,6 +1,13 @@
var assert = require("assert");
+var hamjest = require("hamjest");
+var assertThat = hamjest.assertThat;
+var contains = hamjest.contains;
+var hasProperties = hamjest.hasProperties;
+
var mammoth = require("../");
+var documents = require("../lib/documents");
+var promises = require("../lib/promises");
var test = require("./test")(module);
@@ -8,3 +15,20 @@ var test = require("./test")(module);
test('mammoth.images.inline() should be an alias of mammoth.images.imgElement()', function() {
assert.ok(mammoth.images.inline === mammoth.images.imgElement);
});
+
+
+test('mammoth.images.dataUri() encodes images in base64', function() {
+ var imageBuffer = new Buffer("abc");
+ var image = new documents.Image({
+ readImage: function(encoding) {
+ return promises.when(imageBuffer.toString(encoding));
+ },
+ contentType: "image/jpeg"
+ });
+
+ return mammoth.images.dataUri(image).then(function(result) {
+ assertThat(result, contains(
+ hasProperties({tag: hasProperties({attributes: {"src": "data:image/jpeg;base64,YWJj"}})})
+ ));
+ });
+});