Skip to content

Commit

Permalink
refactor: use string-strip-html library for preventing widow words
Browse files Browse the repository at this point in the history
replaces the prevent-widows package
  • Loading branch information
cossssmin committed Oct 9, 2022
1 parent df27802 commit a5aef6c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 23 deletions.
36 changes: 25 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/index.js
Expand Up @@ -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)
Expand Down
30 changes: 23 additions & 7 deletions 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)
}
36 changes: 33 additions & 3 deletions test/test-transformers.js
Expand Up @@ -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(`
<!--[if mso]>
<p>A paragraph inside an Outlook MSO comment</p>
<![endif]-->
<div>Text following an MSO comment</div>
`)

t.is(basic, `
<!--[if mso]>
<p>A paragraph inside an Outlook MSO&nbsp;comment</p>
<![endif]-->
<div>Text following an MSO&nbsp;comment</div>
`)

const withOptions = await Maizzle.preventWidows(`
<div prevent-widows>
<!--[if mso]>
<p>A paragraph inside an Outlook MSO comment</p>
<![endif]-->
<div>Text following an MSO comment</div>
</div>
<p>Should not remove widows here</p>
`, {attrName: 'prevent-widows'}, false)

t.is(withOptions, `
<div>
<!--[if mso]>
<p>A paragraph inside an Outlook MSO&nbsp;comment</p>
<![endif]-->
<div>Text following an MSO&nbsp;comment</div>
</div>
<p>Should not remove widows here</p>
`)
})

test('markdown (disabled)', async t => {
Expand Down

0 comments on commit a5aef6c

Please sign in to comment.