This plugin helps prevent widow words by replacing the space between the last two words in a string with a non-breaking space. By default, the string must contain at least 4 words to be processed.
Input:
<div prevent-widows> <p>The quick brown fox</p></div>
Output:
<div> <p>The quick brown fox</p></div>
configurable attribute names
set the minimum number of words
ignore templating logic or expressions
reverse it: create widow words
Install
npm i posthtml posthtml-widows
Usage
import posthtml from 'posthtml'import preventWidows from 'posthtml-widows'posthtml([ preventWidows()]) .process('<p prevent-widows>The quick brown fox</p>') .then(result => console.log(result.html))
Result:
<p>The quick brown fox</p>
Attributes
The plugin will only handle strings inside elements that have one of the following attributes:
prevent-widows
no-widows
You may also specify custom attributes to use:
posthtml([ preventWidows({ attributes: ['fix-widows'] })]) .process('<p fix-widows>The quick brown fox</p>')
Options
minWords
Type: number
Default: 4
The minimum number of words a string must contain to be processed.