Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use string-strip-html library for preventing widow words
replaces the prevent-widows package
- Loading branch information
Showing
5 changed files
with
83 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters