From 327b99803151111bcec37af0d2bc5df5baa3506b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 14 May 2017 13:39:33 +0100 Subject: Extract data URI image converter to images module --- lib/images.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'lib/images.js') 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 + }; + }); +}); -- cgit v1.2.3