diff options
| -rw-r--r-- | .eslintrc.json | 1 | ||||
| -rw-r--r-- | lib/documents.js | 20 | ||||
| -rw-r--r-- | lib/images.js | 2 | ||||
| -rw-r--r-- | lib/index.d.ts | 2 | ||||
| -rw-r--r-- | lib/zipfile.js | 19 | ||||
| -rw-r--r-- | package-lock.json | 138 | ||||
| -rw-r--r-- | package.json | 7 | ||||
| -rw-r--r-- | test/mammoth.tests.js | 46 | ||||
| -rw-r--r-- | test/testing.js | 6 |
9 files changed, 131 insertions, 110 deletions
diff --git a/.eslintrc.json b/.eslintrc.json index 86784b5..75ad86c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -69,6 +69,7 @@ "exports": true, "module": true, "require": false, + "TextDecoder": false, "Uint8Array": false } } diff --git a/lib/documents.js b/lib/documents.js index 26c0ce7..9088ed9 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -151,12 +151,22 @@ function noteKey(noteType, id) { function Image(options) { return { type: types.image, - read: options.readImage, - readAsText: function(encoding) { - if (!encoding) { - throw new Error("Missing encoding argument"); + // `read` is retained for backwards compatibility, but other read + // methods should be preferred. + read: function(encoding) { + if (encoding) { + return options.readImage(encoding); + } else { + return options.readImage().then(function(arrayBuffer) { + return Buffer.from(arrayBuffer); + }); } - return options.readImage(encoding); + }, + readAsArrayBuffer: function() { + return options.readImage(); + }, + readAsBase64String: function() { + return options.readImage("base64"); }, altText: options.altText, contentType: options.contentType diff --git a/lib/images.js b/lib/images.js index 07cfc2d..40aa378 100644 --- a/lib/images.js +++ b/lib/images.js @@ -23,7 +23,7 @@ function imgElement(func) { exports.inline = exports.imgElement; exports.dataUri = imgElement(function(element) { - return element.readAsText("base64").then(function(imageBuffer) { + return element.readAsBase64String().then(function(imageBuffer) { return { src: "data:" + element.contentType + ";base64," + imageBuffer }; diff --git a/lib/index.d.ts b/lib/index.d.ts index 9666a9e..c73eeb5 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -39,7 +39,7 @@ interface ImageConverter { interface Image { contentType: string; - readAsText: (encoding: string) => Promise<string>; + readAsBase64String: () => Promise<string>; read: ImageRead; } diff --git a/lib/zipfile.js b/lib/zipfile.js index d38a9a4..85dde81 100644 --- a/lib/zipfile.js +++ b/lib/zipfile.js @@ -1,3 +1,4 @@ +var base64js = require("base64-js"); var JSZip = require("jszip"); exports.openArrayBuffer = openArrayBuffer; @@ -12,11 +13,13 @@ function openArrayBuffer(arrayBuffer) { function read(name, encoding) { return zipFile.file(name).async("uint8array").then(function(array) { - var buffer = uint8ArrayToBuffer(array); - if (encoding) { - return buffer.toString(encoding); + if (encoding === "base64") { + return base64js.fromByteArray(array); + } else if (encoding) { + var decoder = new TextDecoder(encoding); + return decoder.decode(array); } else { - return buffer; + return array; } }); } @@ -38,14 +41,6 @@ function openArrayBuffer(arrayBuffer) { }); } -function uint8ArrayToBuffer(array) { - if (Buffer.from && Buffer.from !== Uint8Array.from) { - return Buffer.from(array); - } else { - return new Buffer(array); - } -} - function splitPath(path) { var lastIndex = path.lastIndexOf("/"); if (lastIndex === -1) { diff --git a/package-lock.json b/package-lock.json index 8052a3e..165d145 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@xmldom/xmldom": "^0.8.6", "argparse": "~1.0.3", + "base64-js": "^1.5.1", "bluebird": "~3.4.0", "dingbat-to-unicode": "^1.0.1", "jszip": "^3.7.1", @@ -27,10 +28,13 @@ "browserify-prepend-licenses": "~1.0.0", "duck": "^0.1.12", "eslint": "2.13.1", - "hamjest": "2.13.0", + "hamjest": "^4.0.1", "mocha": "~2.2.5", "temp": "^0.9.4", "uglify-js": "~2.4.8" + }, + "engines": { + "node": ">=12.0.0" } }, "node_modules/@xmldom/xmldom": { @@ -233,9 +237,9 @@ } }, "node_modules/assertion-error": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.0.2.tgz", - "integrity": "sha1-E8pRXYYgbaC6xm6DTdOX2HWBCUw=", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", "dev": true, "engines": { "node": "*" @@ -263,10 +267,23 @@ "dev": true }, "node_modules/base64-js": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.2.0.tgz", - "integrity": "sha1-o5mS1yNYSBGYK+XikLtqU9hnAPE=", - "dev": true + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] }, "node_modules/bluebird": { "version": "3.4.7", @@ -1077,12 +1094,6 @@ "jsonify": "~0.0.0" } }, - "node_modules/eslint/node_modules/lodash": { - "version": "4.17.4", - "resolved": "http://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", - "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", - "dev": true - }, "node_modules/espree": { "version": "3.4.1", "resolved": "https://registry.npmjs.org/espree/-/espree-3.4.1.tgz", @@ -1347,29 +1358,18 @@ "dev": true }, "node_modules/hamjest": { - "version": "2.13.0", - "resolved": "http://registry.npmjs.org/hamjest/-/hamjest-2.13.0.tgz", - "integrity": "sha1-SF/31RG4Gl3KZQkbu9QA+Xqas4c=", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/hamjest/-/hamjest-4.0.1.tgz", + "integrity": "sha512-YDA8nHdy2iBJkSVZH4kMA3ggqhccYNUbfqouQTh/G7ICdajx5vh+4WLaKJck1lvCYEw2WOIgOne95zoA2BzYSA==", "dev": true, "dependencies": { - "assertion-error": "~1.0.0", - "lodash": "^2.4.1", - "q": "~0.9.7" + "assertion-error": "^1.1.0", + "lodash": "^4.17.15" }, "engines": { - "node": ">=0.10.0" + "node": ">=8.0.0" } }, - "node_modules/hamjest/node_modules/lodash": { - "version": "2.4.2", - "resolved": "http://registry.npmjs.org/lodash/-/lodash-2.4.2.tgz", - "integrity": "sha1-+t2DS5aDBz2hebPq5tnA0VBT9z4=", - "dev": true, - "engines": [ - "node", - "rhino" - ] - }, "node_modules/has": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/has/-/has-1.0.1.tgz", @@ -1506,12 +1506,6 @@ "through": "^2.3.6" } }, - "node_modules/inquirer/node_modules/lodash": { - "version": "4.17.4", - "resolved": "http://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", - "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", - "dev": true - }, "node_modules/insert-module-globals": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/insert-module-globals/-/insert-module-globals-7.0.1.tgz", @@ -1826,6 +1820,12 @@ "immediate": "~3.0.5" } }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, "node_modules/lodash.memoize": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-3.0.4.tgz", @@ -2762,12 +2762,6 @@ "node": ">=4" } }, - "node_modules/table/node_modules/lodash": { - "version": "4.17.4", - "resolved": "http://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", - "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", - "dev": true - }, "node_modules/table/node_modules/string-width": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.0.0.tgz", @@ -3270,9 +3264,9 @@ } }, "assertion-error": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.0.2.tgz", - "integrity": "sha1-E8pRXYYgbaC6xm6DTdOX2HWBCUw=", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", "dev": true }, "astw": { @@ -3297,10 +3291,9 @@ "dev": true }, "base64-js": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.2.0.tgz", - "integrity": "sha1-o5mS1yNYSBGYK+XikLtqU9hnAPE=", - "dev": true + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==" }, "bluebird": { "version": "3.4.7", @@ -4041,12 +4034,6 @@ "requires": { "jsonify": "~0.0.0" } - }, - "lodash": { - "version": "4.17.4", - "resolved": "http://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", - "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", - "dev": true } } }, @@ -4262,22 +4249,13 @@ "dev": true }, "hamjest": { - "version": "2.13.0", - "resolved": "http://registry.npmjs.org/hamjest/-/hamjest-2.13.0.tgz", - "integrity": "sha1-SF/31RG4Gl3KZQkbu9QA+Xqas4c=", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/hamjest/-/hamjest-4.0.1.tgz", + "integrity": "sha512-YDA8nHdy2iBJkSVZH4kMA3ggqhccYNUbfqouQTh/G7ICdajx5vh+4WLaKJck1lvCYEw2WOIgOne95zoA2BzYSA==", "dev": true, "requires": { - "assertion-error": "~1.0.0", - "lodash": "^2.4.1", - "q": "~0.9.7" - }, - "dependencies": { - "lodash": { - "version": "2.4.2", - "resolved": "http://registry.npmjs.org/lodash/-/lodash-2.4.2.tgz", - "integrity": "sha1-+t2DS5aDBz2hebPq5tnA0VBT9z4=", - "dev": true - } + "assertion-error": "^1.1.0", + "lodash": "^4.17.15" } }, "has": { @@ -4402,14 +4380,6 @@ "string-width": "^1.0.1", "strip-ansi": "^3.0.0", "through": "^2.3.6" - }, - "dependencies": { - "lodash": { - "version": "4.17.4", - "resolved": "http://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", - "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", - "dev": true - } } }, "insert-module-globals": { @@ -4682,6 +4652,12 @@ "immediate": "~3.0.5" } }, + "lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, "lodash.memoize": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-3.0.4.tgz", @@ -5456,12 +5432,6 @@ "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", "dev": true }, - "lodash": { - "version": "4.17.4", - "resolved": "http://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", - "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", - "dev": true - }, "string-width": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.0.0.tgz", diff --git a/package.json b/package.json index 4f59829..2432a16 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dependencies": { "@xmldom/xmldom": "^0.8.6", "argparse": "~1.0.3", + "base64-js": "^1.5.1", "bluebird": "~3.4.0", "dingbat-to-unicode": "^1.0.1", "jszip": "^3.7.1", @@ -32,7 +33,7 @@ "browserify-prepend-licenses": "~1.0.0", "duck": "^0.1.12", "eslint": "2.13.1", - "hamjest": "2.13.0", + "hamjest": "^4.0.1", "mocha": "~2.2.5", "temp": "^0.9.4", "uglify-js": "~2.4.8" @@ -50,8 +51,8 @@ "prepare": "make mammoth.browser.min.js" }, "license": "BSD-2-Clause", - "engines" : { - "node" : ">=12.0.0" + "engines": { + "node": ">=12.0.0" }, "volta": { "node": "16.18.1", diff --git a/test/mammoth.tests.js b/test/mammoth.tests.js index 3ce96d4..bbbbe37 100644 --- a/test/mammoth.tests.js +++ b/test/mammoth.tests.js @@ -219,7 +219,7 @@ test('inline images referenced by path relative to base are included in output', }); }); -test('src of inline images can be changed', function() { +test('src of inline images can be changed using read("base64")', function() { var docxPath = path.join(__dirname, "test-data/tiny-picture.docx"); var convertImage = mammoth.images.imgElement(function(element) { return element.read("base64").then(function(encodedImage) { @@ -227,6 +227,50 @@ test('src of inline images can be changed', function() { }); }); 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('src of inline images can be changed using readAsBase64String()', function() { + var docxPath = path.join(__dirname, "test-data/tiny-picture.docx"); + var convertImage = mammoth.images.imgElement(function(element) { + return element.readAsBase64String().then(function(encodedImage) { + 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('src of inline images can be changed using readAsArrayBuffer()', function() { + var docxPath = path.join(__dirname, "test-data/tiny-picture.docx"); + var convertImage = mammoth.images.imgElement(function(element) { + return element.readAsArrayBuffer().then(function(arrayBuffer) { + assert.ok(!Buffer.isBuffer(arrayBuffer)); + var encodedImage = Buffer.from(arrayBuffer).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('src of inline images can be changed using read()', function() { + var docxPath = path.join(__dirname, "test-data/tiny-picture.docx"); + var convertImage = mammoth.images.imgElement(function(element) { + return element.read().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>'); }); }); diff --git a/test/testing.js b/test/testing.js index cb37777..3948556 100644 --- a/test/testing.js +++ b/test/testing.js @@ -22,7 +22,7 @@ function createFakeDocxFile(files) { function exists(path) { return !!files[path]; } - + return { read: createRead(files), exists: exists @@ -41,13 +41,13 @@ function createRead(files) { if (_.isString(buffer)) { buffer = new Buffer(buffer); } - + if (!Buffer.isBuffer(buffer)) { return promises.reject(new Error("file was not a buffer")); } else if (encoding) { return promises.when(buffer.toString(encoding)); } else { - return promises.when(buffer); + return promises.when(buffer.buffer); } }); } |
