From f394eaa7b8cb6d3f3deeffe3c4b5160353eceab4 Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Thu, 21 Feb 2019 11:46:55 +0700 Subject: [PATCH] Added optional support for HTML source closes TryGhost/Ghost-SDK/issues/51 - Due to JSDOM not supporting Node v6 the support for HTML conversion is now optional --- core/server/lib/mobiledoc/converters/index.js | 29 ++++++++++++++++++- .../v2/utils/serializers/input/posts_spec.js | 8 ++++- .../mobiledoc/converters/converters_spec.js | 22 ++++++++++++++ package.json | 2 +- yarn.lock | 1 + 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 core/test/unit/lib/mobiledoc/converters/converters_spec.js diff --git a/core/server/lib/mobiledoc/converters/index.js b/core/server/lib/mobiledoc/converters/index.js index 31790b3ea3..2a7561b5db 100644 --- a/core/server/lib/mobiledoc/converters/index.js +++ b/core/server/lib/mobiledoc/converters/index.js @@ -1,3 +1,5 @@ +const common = require('../../common'); + module.exports = { get mobiledocConverter() { return require('./mobiledoc-converter'); @@ -8,6 +10,31 @@ module.exports = { }, get htmlToMobiledocConverter() { - return require('@tryghost/html-to-mobiledoc').toMobiledoc; + try { + return require('@tryghost/html-to-mobiledoc').toMobiledoc; + } catch (err) { + if (process.versions.node.startsWith('v6.')) { + // NOTE: When Node v6 is dropped this code block should be removed + return () => { + throw new common.errors.InternalServerError({ + message: 'Unable to convert from source HTML to Mobiledoc', + context: 'The html-to-mobiledoc package was not installed', + help: 'Please upgrade to Node.js v10', + code: 'HTML_TO_MOBILEDOC_INSTALLATION', + err: err + }); + }; + } else { + return () => { + throw new common.errors.InternalServerError({ + message: 'Unable to convert from source HTML to Mobiledoc', + context: 'The html-to-mobiledoc package was not installed', + help: 'Please review any errors from the install process by checking the Ghost logs', + code: 'HTML_TO_MOBILEDOC_INSTALLATION', + err: err + }); + }; + } + } } }; diff --git a/core/test/unit/api/v2/utils/serializers/input/posts_spec.js b/core/test/unit/api/v2/utils/serializers/input/posts_spec.js index 35ddc94597..a61961575f 100644 --- a/core/test/unit/api/v2/utils/serializers/input/posts_spec.js +++ b/core/test/unit/api/v2/utils/serializers/input/posts_spec.js @@ -371,11 +371,17 @@ describe('Unit: v2/utils/serializers/input/posts', function () { }); describe('Ensure html to mobiledoc conversion', function () { + before(function () { + // NOTE: only supported in node v8 and higher + if (process.version.startsWith('v6.')) { + this.skip(); + } + }); + it('no transformation when no html source option provided', function () { const apiConfig = {}; const mobiledoc = '{"version":"0.3.1","atoms":[],"cards":[],"sections":[]}'; const frame = { - options: {}, data: { posts: [ { diff --git a/core/test/unit/lib/mobiledoc/converters/converters_spec.js b/core/test/unit/lib/mobiledoc/converters/converters_spec.js new file mode 100644 index 0000000000..a3f8ec8b78 --- /dev/null +++ b/core/test/unit/lib/mobiledoc/converters/converters_spec.js @@ -0,0 +1,22 @@ +const should = require('should'); +const converters = require('../../../../../server/lib/mobiledoc/converters'); + +describe('Unit: lib/mobiledoc/converters', function () { + describe('htmlToMobiledocConverter should be unsupported in node v6', function () { + before(function () { + if (!process.version.startsWith('v6.')) { + this.skip(); + } + }); + + it('should throw when running on node v6', function () { + try { + const thrower = converters.htmlToMobiledocConverter(); + thrower(); + throw new Error('should not execute'); + } catch (err) { + err.message.should.equal('Unable to convert from source HTML to Mobiledoc'); + } + }); + }); +}); diff --git a/package.json b/package.json index 7938ee4608..15441e8927 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "dependencies": { "@nexes/nql": "0.2.1", "ajv": "6.8.1", - "@tryghost/html-to-mobiledoc": "0.2.3", "amperize": "0.3.8", "analytics-node": "3.3.0", "archiver": "3.0.0", @@ -114,6 +113,7 @@ "xml": "1.0.1" }, "optionalDependencies": { + "@tryghost/html-to-mobiledoc": "0.2.3", "sharp": "0.21.3", "sqlite3": "4.0.6" }, diff --git a/yarn.lock b/yarn.lock index 892f71a75d..d0000cc0b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5986,6 +5986,7 @@ sprintf-js@~1.0.2: sqlite3@4.0.6, sqlite3@^4.0.6: version "4.0.6" resolved "https://registry.yarnpkg.com/sqlite3/-/sqlite3-4.0.6.tgz#e587b583b5acc6cb38d4437dedb2572359c080ad" + integrity sha512-EqBXxHdKiwvNMRCgml86VTL5TK1i0IKiumnfxykX0gh6H6jaKijAXvE9O1N7+omfNSawR2fOmIyJZcfe8HYWpw== dependencies: nan "~2.10.0" node-pre-gyp "^0.11.0"