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
This commit is contained in:
Daniel Lockyer 2022-08-03 14:47:37 +02:00
parent cd5a8979bf
commit 464b5ca426
16 changed files with 71 additions and 14 deletions

View File

@ -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) => {

View File

@ -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.');

View File

@ -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');

View File

@ -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`);

View File

@ -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');

View File

@ -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}) {

View File

@ -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');

View File

@ -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",

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/node'
]
};

View File

@ -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

View File

@ -0,0 +1 @@
module.exports = require('./lib/html-to-plaintext');

View File

@ -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"
}
}

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/test'
]
};

View File

@ -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 = '<p>Some text in a paragraph.</p><!--kg-card-begin: html--><blockquote class=\"instagram-media\" data-instgrm-captioned data-instgrm-permalink=\"https://www.instagram.com/p/AbC123dEf/?utm_source=ig_embed&amp;utm_campaign=loading\" data-instgrm-version=\"14\"><div> <a href=\"https://www.instagram.com/p/AbC123dEf/?utm_source=ig_embed&amp;utm_campaign=loading\" target=\"_blank\"><div><div></div><div><div></div><div></div></div></div><div></div><div><svg width=\"50px\" height=\"50px\" viewBox=\"0 0 60 60\" version=\"1.1\" xmlns=\"https://www.w3.org/2000/svg\" xmlns:xlink=\"https://www.w3.org/1999/xlink\"><!-- svg stuff --></svg></div><div><div>View this post on Instagram</div></div><div></div><div><div><div></div><div></div><div></div></div><div><div></div><div><div></div><div></div><div></div></div></div><div><div></div><div></div></div></a><p><a href=\"https://www.instagram.com/p/AbC123dEf/?utm_source=ig_embed&amp;utm_campaign=loading\" target=\"_blank\">A post shared by Some Dude (@somedude)</a></p></div></blockquote><script async src=\"//www.instagram.com/embed.js\"></script><!--kg-card-end: html-->';
const html = '<p>Some text in a paragraph.</p><!--kg-card-begin: html--><blockquote class="instagram-media" data-instgrm-captioned data-instgrm-permalink="https://www.instagram.com/p/AbC123dEf/?utm_source=ig_embed&amp;utm_campaign=loading" data-instgrm-version="14"><div> <a href="https://www.instagram.com/p/AbC123dEf/?utm_source=ig_embed&amp;utm_campaign=loading" target="_blank"><div><div></div><div><div></div><div></div></div></div><div></div><div><svg width="50px" height="50px" viewBox="0 0 60 60" version="1.1" xmlns="https://www.w3.org/2000/svg" xmlns:xlink="https://www.w3.org/1999/xlink"><!-- svg stuff --></svg></div><div><div>View this post on Instagram</div></div><div></div><div><div><div></div><div></div><div></div></div><div><div></div><div><div></div><div></div><div></div></div></div><div><div></div><div></div></div></a><p><a href="https://www.instagram.com/p/AbC123dEf/?utm_source=ig_embed&amp;utm_campaign=loading" target="_blank">A post shared by Some Dude (@somedude)</a></p></div></blockquote><script async src="//www.instagram.com/embed.js"></script><!--kg-card-end: html-->';
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);

View File

@ -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"