From e12c1e59acd5fc66ec274395f3c0f88713daf5b4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 28 May 2023 19:16:44 +0100 Subject: Add toArrayBuffer when embedding a style map --- README.md | 1 + lib/index.d.ts | 5 ++++- lib/index.js | 9 ++++++--- lib/zipfile.js | 6 +++--- test/mammoth.tests.js | 23 +++++++++++++++++++++-- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4e8486b..b167336 100644 --- a/README.md +++ b/README.md @@ -444,6 +444,7 @@ it will use the embedded style map. * `styleMap`: the style map to embed. * Returns a promise. + Call `toArrayBuffer()` on the value inside the promise to get an `ArrayBuffer` representing the new document. Call `toBuffer()` on the value inside the promise to get a `Buffer` representing the new document. For instance: diff --git a/lib/index.d.ts b/lib/index.d.ts index c73eeb5..705606f 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,7 +1,10 @@ interface Mammoth { convertToHtml: (input: Input, options?: Options) => Promise; extractRawText: (input: Input) => Promise; - embedStyleMap: (input: Input, styleMap: string) => Promise<{toBuffer: () => Buffer}>; + embedStyleMap: (input: Input, styleMap: string) => Promise<{ + toArrayBuffer: () => ArrayBuffer, + toBuffer: () => Buffer, + }>; images: Images; } diff --git a/lib/index.js b/lib/index.js index ac9f634..03302c2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -92,12 +92,15 @@ function embedStyleMap(input, styleMap) { return docxStyleMap.writeStyleMap(docxFile, styleMap); }) .then(function(docxFile) { - return docxFile.toBuffer(); + return docxFile.toArrayBuffer(); }) - .then(function(buffer) { + .then(function(arrayBuffer) { return { + toArrayBuffer: function() { + return arrayBuffer; + }, toBuffer: function() { - return buffer; + return Buffer.from(arrayBuffer); } }; }); diff --git a/lib/zipfile.js b/lib/zipfile.js index 85dde81..2129193 100644 --- a/lib/zipfile.js +++ b/lib/zipfile.js @@ -28,15 +28,15 @@ function openArrayBuffer(arrayBuffer) { zipFile.file(name, contents); } - function toBuffer() { - return zipFile.generateAsync({type: "nodebuffer"}); + function toArrayBuffer() { + return zipFile.generateAsync({type: "arraybuffer"}); } return { exists: exists, read: read, write: write, - toBuffer: toBuffer + toArrayBuffer: toArrayBuffer }; }); } diff --git a/test/mammoth.tests.js b/test/mammoth.tests.js index 9f0e902..3b1726e 100644 --- a/test/mammoth.tests.js +++ b/test/mammoth.tests.js @@ -122,14 +122,33 @@ test('embedded style maps can be disabled', function() { }); }); -test('embedded style map can be written and then read', function() { +test('embedded style map can be written using toBuffer() and then read', function() { var docxPath = path.join(__dirname, "test-data/single-paragraph.docx"); return promises.nfcall(fs.readFile, docxPath) .then(function(buffer) { return mammoth.embedStyleMap({buffer: buffer}, "p => h1"); }) .then(function(docx) { - return mammoth.convertToHtml({buffer: docx.toBuffer()}); + var buffer = docx.toBuffer(); + assert.ok(Buffer.isBuffer(buffer)); + return mammoth.convertToHtml({buffer: buffer}); + }) + .then(function(result) { + assert.equal(result.value, "

Walking on imported air

"); + assert.deepEqual(result.messages, []); + }); +}); + +test('embedded style map can be written using toArrayBuffer() and then read', function() { + var docxPath = path.join(__dirname, "test-data/single-paragraph.docx"); + return promises.nfcall(fs.readFile, docxPath) + .then(function(buffer) { + return mammoth.embedStyleMap({buffer: buffer}, "p => h1"); + }) + .then(function(docx) { + var arrayBuffer = docx.toArrayBuffer(); + assert.ok(!Buffer.isBuffer(arrayBuffer)); + return mammoth.convertToHtml({buffer: Buffer.from(arrayBuffer)}); }) .then(function(result) { assert.equal(result.value, "

Walking on imported air

"); -- cgit v1.2.3