From 464b5ca426680e291b8166fe4621a27df4e9bcdc Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Wed, 3 Aug 2022 14:47:37 +0200 Subject: [PATCH] Extracted `html-to-plaintext` shared lib into package refs https://github.com/TryGhost/Toolbox/issues/363 - this shared library is standalone, and it used in various places of Ghost core, so we can pull it out to keep it easier to reason about - we also use the `html-to-text` dependency in another package but it's outdated and could now switch to this new package --- .../serializers/output/utils/post-gating.js | 2 +- .../versions/4.0/23-regenerate-posts-html.js | 2 +- .../05-fix-missed-mobiledoc-url-transforms.js | 2 +- .../2022-05-21-00-00-regenerate-posts-html.js | 2 +- ghost/core/core/server/models/post.js | 2 +- .../core/server/services/comments/emails.js | 2 +- .../services/mega/post-email-serializer.js | 2 +- ghost/core/package.json | 2 +- ghost/html-to-plaintext/.eslintrc.js | 6 +++++ ghost/html-to-plaintext/README.md | 21 +++++++++++++++++ ghost/html-to-plaintext/index.js | 1 + .../lib}/html-to-plaintext.js | 0 ghost/html-to-plaintext/package.json | 23 +++++++++++++++++++ ghost/html-to-plaintext/test/.eslintrc.js | 6 +++++ .../test}/html-to-plaintext.test.js | 4 ++-- yarn.lock | 8 +++---- 16 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 ghost/html-to-plaintext/.eslintrc.js create mode 100644 ghost/html-to-plaintext/README.md create mode 100644 ghost/html-to-plaintext/index.js rename ghost/{core/core/shared => html-to-plaintext/lib}/html-to-plaintext.js (100%) create mode 100644 ghost/html-to-plaintext/package.json create mode 100644 ghost/html-to-plaintext/test/.eslintrc.js rename ghost/{core/test/unit/shared => html-to-plaintext/test}/html-to-plaintext.test.js (88%) diff --git a/ghost/core/core/server/api/endpoints/utils/serializers/output/utils/post-gating.js b/ghost/core/core/server/api/endpoints/utils/serializers/output/utils/post-gating.js index 571af1a4e1..07655f311a 100644 --- a/ghost/core/core/server/api/endpoints/utils/serializers/output/utils/post-gating.js +++ b/ghost/core/core/server/api/endpoints/utils/serializers/output/utils/post-gating.js @@ -1,5 +1,5 @@ const membersService = require('../../../../../../services/members'); -const htmlToPlaintext = require('../../../../../../../shared/html-to-plaintext'); +const htmlToPlaintext = require('@tryghost/html-to-plaintext'); // @TODO: reconsider the location of this - it's part of members and adds a property to the API const forPost = (attrs, frame) => { diff --git a/ghost/core/core/server/data/migrations/versions/4.0/23-regenerate-posts-html.js b/ghost/core/core/server/data/migrations/versions/4.0/23-regenerate-posts-html.js index 1854c21683..a3d4761146 100644 --- a/ghost/core/core/server/data/migrations/versions/4.0/23-regenerate-posts-html.js +++ b/ghost/core/core/server/data/migrations/versions/4.0/23-regenerate-posts-html.js @@ -1,7 +1,7 @@ const logging = require('@tryghost/logging'); const {createIrreversibleMigration} = require('../../utils'); const mobiledocLib = require('../../../../lib/mobiledoc'); -const htmlToPlaintext = require('../../../../../shared/html-to-plaintext'); +const htmlToPlaintext = require('@tryghost/html-to-plaintext'); module.exports = createIrreversibleMigration(async (knex) => { logging.info('Starting re-generation of posts html.'); diff --git a/ghost/core/core/server/data/migrations/versions/4.9/05-fix-missed-mobiledoc-url-transforms.js b/ghost/core/core/server/data/migrations/versions/4.9/05-fix-missed-mobiledoc-url-transforms.js index 10e0bfda9a..3b606a35b8 100644 --- a/ghost/core/core/server/data/migrations/versions/4.9/05-fix-missed-mobiledoc-url-transforms.js +++ b/ghost/core/core/server/data/migrations/versions/4.9/05-fix-missed-mobiledoc-url-transforms.js @@ -1,6 +1,6 @@ const logging = require('@tryghost/logging'); const urlUtils = require('../../../../../shared/url-utils'); -const htmlToPlaintext = require('../../../../../shared/html-to-plaintext'); +const htmlToPlaintext = require('@tryghost/html-to-plaintext'); const mobiledocLib = require('../../../../lib/mobiledoc'); const {createTransactionalMigration} = require('../../utils'); diff --git a/ghost/core/core/server/data/migrations/versions/5.0/2022-05-21-00-00-regenerate-posts-html.js b/ghost/core/core/server/data/migrations/versions/5.0/2022-05-21-00-00-regenerate-posts-html.js index e6d0255c5a..bd713a8c73 100644 --- a/ghost/core/core/server/data/migrations/versions/5.0/2022-05-21-00-00-regenerate-posts-html.js +++ b/ghost/core/core/server/data/migrations/versions/5.0/2022-05-21-00-00-regenerate-posts-html.js @@ -1,7 +1,7 @@ const logging = require('@tryghost/logging'); const {createIrreversibleMigration} = require('../../utils'); const mobiledocLib = require('../../../../lib/mobiledoc'); -const htmlToPlaintext = require('../../../../../shared/html-to-plaintext'); +const htmlToPlaintext = require('@tryghost/html-to-plaintext'); module.exports = createIrreversibleMigration(async (knex) => { logging.info(`Starting regeneration of posts HTML`); diff --git a/ghost/core/core/server/models/post.js b/ghost/core/core/server/models/post.js index 2dfbcabe2d..d737878111 100644 --- a/ghost/core/core/server/models/post.js +++ b/ghost/core/core/server/models/post.js @@ -7,7 +7,7 @@ const {sequence} = require('@tryghost/promise'); const tpl = require('@tryghost/tpl'); const errors = require('@tryghost/errors'); const nql = require('@tryghost/nql'); -const htmlToPlaintext = require('../../shared/html-to-plaintext'); +const htmlToPlaintext = require('@tryghost/html-to-plaintext'); const ghostBookshelf = require('./base'); const config = require('../../shared/config'); const settingsCache = require('../../shared/settings-cache'); diff --git a/ghost/core/core/server/services/comments/emails.js b/ghost/core/core/server/services/comments/emails.js index 5520de78b8..98decd507e 100644 --- a/ghost/core/core/server/services/comments/emails.js +++ b/ghost/core/core/server/services/comments/emails.js @@ -1,7 +1,7 @@ const {promises: fs} = require('fs'); const path = require('path'); const moment = require('moment'); -const htmlToPlaintext = require('../../../shared/html-to-plaintext'); +const htmlToPlaintext = require('@tryghost/html-to-plaintext'); class CommentsServiceEmails { constructor({config, logging, models, mailer, settingsCache, urlService, urlUtils}) { diff --git a/ghost/core/core/server/services/mega/post-email-serializer.js b/ghost/core/core/server/services/mega/post-email-serializer.js index 7b3f096ba8..3d8d8f33d4 100644 --- a/ghost/core/core/server/services/mega/post-email-serializer.js +++ b/ghost/core/core/server/services/mega/post-email-serializer.js @@ -8,7 +8,7 @@ const api = require('../../api').endpoints; const apiShared = require('../../api').shared; const {URL} = require('url'); const mobiledocLib = require('../../lib/mobiledoc'); -const htmlToPlaintext = require('../../../shared/html-to-plaintext'); +const htmlToPlaintext = require('@tryghost/html-to-plaintext'); const membersService = require('../members'); const {isUnsplashImage, isLocalContentImage} = require('@tryghost/kg-default-cards/lib/utils'); const {textColorForBackgroundColor, darkenToContrastThreshold} = require('@tryghost/color-utils'); diff --git a/ghost/core/package.json b/ghost/core/package.json index 35678ac089..61b5e53f77 100644 --- a/ghost/core/package.json +++ b/ghost/core/package.json @@ -73,6 +73,7 @@ "@tryghost/errors": "1.2.14", "@tryghost/express-dynamic-redirects": "0.0.0", "@tryghost/helpers": "1.1.71", + "@tryghost/html-to-plaintext": "0.0.0", "@tryghost/image-transform": "1.2.1", "@tryghost/job-manager": "0.0.0", "@tryghost/kg-card-factory": "3.1.3", @@ -145,7 +146,6 @@ "glob": "8.0.3", "got": "9.6.0", "gscan": "4.34.0", - "html-to-text": "8.2.0", "human-number": "2.0.0", "image-size": "1.0.2", "intl": "1.2.5", diff --git a/ghost/html-to-plaintext/.eslintrc.js b/ghost/html-to-plaintext/.eslintrc.js new file mode 100644 index 0000000000..c9c1bcb522 --- /dev/null +++ b/ghost/html-to-plaintext/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/node' + ] +}; diff --git a/ghost/html-to-plaintext/README.md b/ghost/html-to-plaintext/README.md new file mode 100644 index 0000000000..c86d9cc932 --- /dev/null +++ b/ghost/html-to-plaintext/README.md @@ -0,0 +1,21 @@ +# Html To Plaintext + + +## Usage + + +## Develop + +This is a monorepo package. + +Follow the instructions for the top-level repo. +1. `git clone` this repo & `cd` into it as usual +2. Run `yarn` to install top-level dependencies. + + + +## Test + +- `yarn lint` run just eslint +- `yarn test` run lint and tests + diff --git a/ghost/html-to-plaintext/index.js b/ghost/html-to-plaintext/index.js new file mode 100644 index 0000000000..bb4a0723fc --- /dev/null +++ b/ghost/html-to-plaintext/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/html-to-plaintext'); diff --git a/ghost/core/core/shared/html-to-plaintext.js b/ghost/html-to-plaintext/lib/html-to-plaintext.js similarity index 100% rename from ghost/core/core/shared/html-to-plaintext.js rename to ghost/html-to-plaintext/lib/html-to-plaintext.js diff --git a/ghost/html-to-plaintext/package.json b/ghost/html-to-plaintext/package.json new file mode 100644 index 0000000000..f4aee57617 --- /dev/null +++ b/ghost/html-to-plaintext/package.json @@ -0,0 +1,23 @@ +{ + "name": "@tryghost/html-to-plaintext", + "version": "0.0.0", + "repository": "https://github.com/TryGhost/Ghost/tree/main/packages/html-to-plaintext", + "author": "Ghost Foundation", + "private": true, + "main": "index.js", + "scripts": { + "dev": "echo \"Implement me!\"", + "test": "NODE_ENV=testing c8 --all --check-coverage --reporter text --reporter cobertura mocha './test/**/*.test.js'", + "lint:code": "eslint *.js lib/ --ext .js --cache", + "lint": "yarn lint:code && yarn lint:test", + "lint:test": "eslint -c test/.eslintrc.js test/ --ext .js --cache" + }, + "devDependencies": { + "c8": "7.12.0", + "mocha": "10.0.0" + }, + "dependencies": { + "html-to-text": "8.2.1", + "lodash": "4.17.21" + } +} diff --git a/ghost/html-to-plaintext/test/.eslintrc.js b/ghost/html-to-plaintext/test/.eslintrc.js new file mode 100644 index 0000000000..829b601eb0 --- /dev/null +++ b/ghost/html-to-plaintext/test/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/test' + ] +}; diff --git a/ghost/core/test/unit/shared/html-to-plaintext.test.js b/ghost/html-to-plaintext/test/html-to-plaintext.test.js similarity index 88% rename from ghost/core/test/unit/shared/html-to-plaintext.test.js rename to ghost/html-to-plaintext/test/html-to-plaintext.test.js index 60b4c33c54..59ae4b4156 100644 --- a/ghost/core/test/unit/shared/html-to-plaintext.test.js +++ b/ghost/html-to-plaintext/test/html-to-plaintext.test.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const htmlToPlaintext = require('../../../core/shared/html-to-plaintext'); +const htmlToPlaintext = require('../'); describe('Html to Plaintext', function () { function getEmailandExcert(input) { @@ -74,7 +74,7 @@ describe('Html to Plaintext', function () { describe('Special cases', function () { it('Instagram (blockquotes)', function () { // This is an instagram embed, but with all the style attributes & svg content removed for brevity - const html = '

Some text in a paragraph.

View this post on Instagram

A post shared by Some Dude (@somedude)

'; + const html = '

Some text in a paragraph.

View this post on Instagram

A post shared by Some Dude (@somedude)

'; const expected = 'Some text in a paragraph.\n\nView this post on Instagram\n\nA post shared by Some Dude (@somedude)'; const {excerpt} = getEmailandExcert(html); assert.equal(excerpt, expected); diff --git a/yarn.lock b/yarn.lock index 2133fd0f8c..87c4966867 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13273,10 +13273,10 @@ html-to-text@5.1.1: lodash "^4.17.11" minimist "^1.2.0" -html-to-text@8.2.0: - version "8.2.0" - resolved "https://registry.yarnpkg.com/html-to-text/-/html-to-text-8.2.0.tgz#8b35e280ba7fc27710b7aa76d4500aab30731924" - integrity sha512-CLXExYn1b++Lgri+ZyVvbUEFwzkLZppjjZOwB7X1qv2jIi8MrMEvxWX5KQ7zATAzTvcqgmtO00M2kCRMtEdOKQ== +html-to-text@8.2.1: + version "8.2.1" + resolved "https://registry.yarnpkg.com/html-to-text/-/html-to-text-8.2.1.tgz#4a75b8a1b646149bd71c50527adb568990bf459b" + integrity sha512-aN/3JvAk8qFsWVeE9InWAWueLXrbkoVZy0TkzaGhoRBC2gCFEeRLDDJN3/ijIGHohy6H+SZzUQWN/hcYtaPK8w== dependencies: "@selderee/plugin-htmlparser2" "^0.6.0" deepmerge "^4.2.2"