PostHTML plugin for preventing widow words in strings.
npm install posthtml-widowsPostHTML plugin for preventing widow words
[![Version][npm-version-shield]][npm]
[![Build][github-ci-shield]][github-ci]
[![License][license-shield]][license]
[![Downloads][npm-stats-shield]][npm-stats]
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:
`` The quick brown foxhtml`
Output:
` The quick brown foxhtml`
- [x] configurable attribute names
- [x] set the minimum number of words
- [x] ignore templating logic or expressions
- [x] reverse it: create widow words
``
npm i posthtml posthtml-widows
`js
import posthtml from 'posthtml'
import preventWidows from 'posthtml-widows'
posthtml([
preventWidows()
])
.process('
The quick brown fox
')Result:
`html
The quick brown fox
`Attributes
The plugin will only handle strings inside elements that have one of the following attributes:
-
prevent-widows
- no-widowsYou may also specify custom attributes to use:
`js
posthtml([
preventWidows({
attributes: ['fix-widows']
})
])
.process('The quick brown fox
')
`Options
$3
Type:
number\
Default: 4The minimum number of words a string must contain to be processed.
`js
posthtml([
preventWidows({
minWords: 3,
})
])
.process('Prevent widow words
')
`$3
Type:
Array\
Default: (array of objects)An array of objects that specify the
start and end delimiters of strings to ignore. Used to avoid processing templating logic or expressions.By default, the following templating delimiters are ignored:
-
{{ }} - Handlebars, Liquid, Nunjucks, Twig, Jinja2, Mustache
- {% %} - Liquid, Nunjucks, Twig, Jinja2
- <%= %> - EJS, ERB
- <% %> - EJS, ERB
- {$ } - Smarty
- - PHP
- = ?> - PHP
- #{ } - PugYou may add custom delimiters to ignore:
`js
posthtml([
preventWidows({
ignore: [
{ start: '[[', end: ']]' },
// Inside MSO comments
{ start: '<' }, //
]
})
])
.process(
Using the option to is being tested here.
)
`Result:
`html
Using the option to is being tested here.
`$3
Type:
boolean\
Default: falseYou may also use the plugin to do the opposite of preventing widow words by replacing the
between the last two words with a regular space.`js
posthtml([
preventWidows({
attributes: ['create-widows'],
createWidows: true,
})
])
.process('The quick brown fox
')
`Result:
`html
The quick brown fox
``[npm]: https://www.npmjs.com/package/posthtml-widows
[npm-version-shield]: https://img.shields.io/npm/v/posthtml-widows.svg
[npm-stats]: http://npm-stat.com/charts.html?package=posthtml-widows
[npm-stats-shield]: https://img.shields.io/npm/dt/posthtml-widows.svg
[github-ci]: https://github.com/posthtml/posthtml-widows/actions/workflows/nodejs.yml
[github-ci-shield]: https://github.com/posthtml/posthtml-widows/actions/workflows/nodejs.yml/badge.svg
[license]: ./LICENSE
[license-shield]: https://img.shields.io/npm/l/posthtml-widows.svg