From 86564e37ae9433d9678e0c00c66e342afd8884b0 Mon Sep 17 00:00:00 2001 From: Chris Price Date: Sun, 17 Aug 2014 16:50:20 -0400 Subject: Add support for reading .docx files from Buffer instances --- README.md | 4 ++-- lib/unzip.js | 2 ++ tests/buffer-tests.js | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/buffer-tests.js diff --git a/README.md b/README.md index 9a90057..6e32dac 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,7 @@ Converts the source document to HTML. * `input`: an object describing the source document. While running on node.js, - to read the file found at `path`, pass in `{path: path}`. + to read the file found at `path`, pass in `{path: path}`; to read a node.js `Buffer` object, pass in `{buffer: buffer}`. While running in the browser, to read the file stored in an array buffer, pass in `{arrayBuffer: arrayBuffer}`. @@ -303,7 +303,7 @@ Each paragraph is followed by two newlines. * `input`: an object describing the source document. While running on node.js, - to read the file found at `path`, pass in `{path: path}`. + to read the file found at `path`, pass in `{path: path}`; to read a node.js `Buffer` object, pass in `{buffer: buffer}`. While running in the browser, to read the file stored in an array buffer, pass in `{arrayBuffer: arrayBuffer}`. diff --git a/lib/unzip.js b/lib/unzip.js index 97db80f..51c34a3 100644 --- a/lib/unzip.js +++ b/lib/unzip.js @@ -12,6 +12,8 @@ var readFile = promises.promisify(fs.readFile); function openZip(options) { if (options.path) { return readFile(options.path).then(zipfile.openArrayBuffer); + } else if (options.buffer) { + return zipfile.openArrayBuffer(options.buffer); } else if (options.file) { return options.file; } else { diff --git a/tests/buffer-tests.js b/tests/buffer-tests.js new file mode 100644 index 0000000..3b4a6c1 --- /dev/null +++ b/tests/buffer-tests.js @@ -0,0 +1,27 @@ +var assert = require("assert"); +var test = require("./testing").test; + +var fs = require("fs"); +var path = require("path"); + +var mammoth = require("../lib"); + +describe("buffer-tests", function() { + test('should convert docx as Buffer to one HTML paragraph', function() { + var docxPath = path.join(__dirname, "test-data/single-paragraph.docx"); + var buffer = fs.readFileSync(docxPath); + return mammoth.convertToHtml({buffer: buffer}).then(function(result) { + assert.equal(result.value, "

Walking on imported air

"); + assert.deepEqual(result.messages, []); + }); + }); + + test('should convert docx as Buffer to plain text', function() { + var docxPath = path.join(__dirname, "test-data/single-paragraph.docx"); + var buffer = fs.readFileSync(docxPath); + return mammoth.extractRawText({buffer: buffer}).then(function(result) { + assert.equal(result.value, "Walking on imported air\n\n"); + assert.deepEqual(result.messages, []); + }); + }); +}); -- cgit v1.2.3