From a5aef6cb62d78725cde9b32b39a47e88456c6cd5 Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Sun, 9 Oct 2022 15:31:03 +0300 Subject: [PATCH] refactor: use string-strip-html library for preventing widow words replaces the prevent-widows package --- package-lock.json | 36 +++++++++++++++++++++---------- package.json | 2 +- src/transformers/index.js | 2 +- src/transformers/preventWidows.js | 30 ++++++++++++++++++++------ test/test-transformers.js | 36 ++++++++++++++++++++++++++++--- 5 files changed, 83 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4bef433e..2e11dfd6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -42,8 +42,8 @@ "posthtml-safe-class-names": "^1.0.8", "posthtml-url-parameters": "^1.0.4", "pretty": "^2.0.0", - "prevent-widows": "^1.0.2", "query-string": "^7.1.0", + "string-remove-widows": "^2.1.0", "string-strip-html": "^8.2.0", "tailwindcss": "^3.1.0" }, @@ -11410,11 +11410,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/prevent-widows": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/prevent-widows/-/prevent-widows-1.0.2.tgz", - "integrity": "sha512-SSnPvcliNDgu+R5QSB2lJMT5XsUpZzBdQVdReywjtw7ukQYiKB27YUcfNY7EiLSatEj8OYT27bs87k+ge8Rweg==" - }, "node_modules/progress": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz", @@ -13163,6 +13158,18 @@ "@babel/runtime": "^7.14.0" } }, + "node_modules/string-remove-widows": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/string-remove-widows/-/string-remove-widows-2.1.0.tgz", + "integrity": "sha512-GoBqfTvkc1uKeazfS/+hjLSi81IBDPmPPVBowk5x39Z+VvrvHLjJZFoYJbwe0JlPg1Mc4rX3Ak+5zPqAexWu5Q==", + "dependencies": { + "@babel/runtime": "^7.14.0", + "ranges-apply": "^5.1.0", + "ranges-push": "^5.1.0", + "string-left-right": "^4.1.0", + "string-match-left-right": "^7.1.0" + } + }, "node_modules/string-strip-html": { "version": "8.3.0", "resolved": "https://registry.npmjs.org/string-strip-html/-/string-strip-html-8.3.0.tgz", @@ -23670,11 +23677,6 @@ "parse-ms": "^2.1.0" } }, - "prevent-widows": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/prevent-widows/-/prevent-widows-1.0.2.tgz", - "integrity": "sha512-SSnPvcliNDgu+R5QSB2lJMT5XsUpZzBdQVdReywjtw7ukQYiKB27YUcfNY7EiLSatEj8OYT27bs87k+ge8Rweg==" - }, "progress": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz", @@ -25046,6 +25048,18 @@ "@babel/runtime": "^7.14.0" } }, + "string-remove-widows": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/string-remove-widows/-/string-remove-widows-2.1.0.tgz", + "integrity": "sha512-GoBqfTvkc1uKeazfS/+hjLSi81IBDPmPPVBowk5x39Z+VvrvHLjJZFoYJbwe0JlPg1Mc4rX3Ak+5zPqAexWu5Q==", + "requires": { + "@babel/runtime": "^7.14.0", + "ranges-apply": "^5.1.0", + "ranges-push": "^5.1.0", + "string-left-right": "^4.1.0", + "string-match-left-right": "^7.1.0" + } + }, "string-strip-html": { "version": "8.3.0", "resolved": "https://registry.npmjs.org/string-strip-html/-/string-strip-html-8.3.0.tgz", diff --git a/package.json b/package.json index 0f2f040b..4e47b3e3 100644 --- a/package.json +++ b/package.json @@ -69,8 +69,8 @@ "posthtml-safe-class-names": "^1.0.8", "posthtml-url-parameters": "^1.0.4", "pretty": "^2.0.0", - "prevent-widows": "^1.0.2", "query-string": "^7.1.0", + "string-remove-widows": "^2.1.0", "string-strip-html": "^8.2.0", "tailwindcss": "^3.1.0" }, diff --git a/src/transformers/index.js b/src/transformers/index.js index c6444ebd..61e38808 100644 --- a/src/transformers/index.js +++ b/src/transformers/index.js @@ -49,7 +49,7 @@ exports.prettify = (html, config) => prettify(html, config, true) exports.ensureSixHEX = (html, config) => ensureSixHEX(html, config) exports.withFilters = (html, config) => filters(html, config, true) exports.addURLParams = (html, config) => addURLParams(html, config, true) -exports.preventWidows = (html, config) => preventWidows(html, config, true) +exports.preventWidows = (html, config) => preventWidows(html, config) exports.replaceStrings = (html, config) => replaceStrings(html, config, true) exports.safeClassNames = (html, config) => safeClassNames(html, config, true) exports.removeUnusedCSS = (html, config) => removeUnusedCSS(html, config, true) diff --git a/src/transformers/preventWidows.js b/src/transformers/preventWidows.js index ffd2797f..9348e252 100644 --- a/src/transformers/preventWidows.js +++ b/src/transformers/preventWidows.js @@ -1,13 +1,29 @@ -const {get} = require('lodash') const posthtml = require('posthtml') -const preventWidows = require('prevent-widows') +const {get, isEmpty} = require('lodash') +const {removeWidows} = require('string-remove-widows') -module.exports = async (html, config = {}, direct = false) => { - const posthtmlOptions = get(config, 'build.posthtml.options', {}) +module.exports = async (html, config = {}) => { + if (isEmpty(config)) { + return removeWidows(html).res + } + + const options = get(config, 'attrName', 'prevent-widows') + const posthtmlOptions = get(config, 'build.posthtml.options', {recognizeNoValueAttribute: true}) + + return posthtml([removeWidowsPlugin(options)]).process(html, posthtmlOptions).then(result => result.html) +} + +const removeWidowsPlugin = attrName => tree => { + const process = node => { + if (node.attrs && node.attrs[attrName]) { + const widowsRemovedString = removeWidows(tree.render(node.content)).res + + node.content = tree.render(tree.parser(widowsRemovedString)) + node.attrs[attrName] = false + } - if (direct) { - return preventWidows(html) + return node } - return posthtml([preventWidows.posthtml()]).process(html, posthtmlOptions).then(result => result.html) + return tree.walk(process) } diff --git a/test/test-transformers.js b/test/test-transformers.js index be0828f0..abdb836d 100644 --- a/test/test-transformers.js +++ b/test/test-transformers.js @@ -373,9 +373,39 @@ test('attribute to style', async t => { }) test('prevent widows', async t => { - const html = await Maizzle.preventWidows('lorem ipsum dolor') - - t.is(html, 'lorem ipsum dolor') + const basic = await Maizzle.preventWidows(` + +
Text following an MSO comment
+ `) + + t.is(basic, ` + +
Text following an MSO comment
+ `) + + const withOptions = await Maizzle.preventWidows(` +
+ +
Text following an MSO comment
+
+

Should not remove widows here

+ `, {attrName: 'prevent-widows'}, false) + + t.is(withOptions, ` +
+ +
Text following an MSO comment
+
+

Should not remove widows here

+ `) }) test('markdown (disabled)', async t => {