diff options
| -rw-r--r-- | lib/documents.js | 5 | ||||
| -rw-r--r-- | test/mammoth.tests.js | 15 |
2 files changed, 20 insertions, 0 deletions
diff --git a/lib/documents.js b/lib/documents.js index 9088ed9..cc6a2a3 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -168,6 +168,11 @@ function Image(options) { readAsBase64String: function() { return options.readImage("base64"); }, + readAsBuffer: function() { + return options.readImage().then(function(arrayBuffer) { + return Buffer.from(arrayBuffer); + }); + }, altText: options.altText, contentType: options.contentType }; diff --git a/test/mammoth.tests.js b/test/mammoth.tests.js index bbbbe37..9f0e902 100644 --- a/test/mammoth.tests.js +++ b/test/mammoth.tests.js @@ -275,6 +275,21 @@ test('src of inline images can be changed using read()', function() { }); }); +test('src of inline images can be changed using readAsBuffer()', function() { + var docxPath = path.join(__dirname, "test-data/tiny-picture.docx"); + var convertImage = mammoth.images.imgElement(function(element) { + return element.readAsBuffer().then(function(buffer) { + assert.ok(Buffer.isBuffer(buffer)); + var encodedImage = buffer.toString("base64"); + return {src: encodedImage.substring(0, 2) + "," + element.contentType}; + }); + }); + return mammoth.convertToHtml({path: docxPath}, {convertImage: convertImage}).then(function(result) { + assert.deepEqual(result.messages, []); + assert.equal(result.value, '<p><img src="iV,image/png" /></p>'); + }); +}); + test('images stored outside of document are included in output', function() { var docxPath = path.join(__dirname, "test-data/external-picture.docx"); return mammoth.convertToHtml({path: docxPath}).then(function(result) { |
