diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-05-28 19:05:05 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-05-28 19:05:05 +0100 |
| commit | 82f8d243b6aba94bd9ed083f242f2470e00fc122 (patch) | |
| tree | 1c673dc2ac16e293e3e659f1da35343f012d4469 | |
| parent | b2b2c35715985f624e6b696fd87140fa6056318f (diff) | |
Support readAsBuffer() on images
| -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) { |
