https://github.com/posthtml/posthtml-widows
Prevent widow words
https://github.com/posthtml/posthtml-widows
Last synced: 10 months ago
JSON representation
Prevent widow words
- Host: GitHub
- URL: https://github.com/posthtml/posthtml-widows
- Owner: posthtml
- License: mit
- Created: 2024-11-16T00:25:26.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-04-01T21:45:43.000Z (11 months ago)
- Last Synced: 2025-04-10T08:33:59.997Z (10 months ago)
- Language: JavaScript
- Size: 507 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Prevent Widows
PostHTML 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]
## Introduction
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:
```html
The quick brown fox
```
Output:
```html
The quick brown fox
```
- [x] configurable attribute names
- [x] set the minimum number of words
- [x] ignore templating logic or expressions
- [x] reverse it: create widow words
## Install
```
npm i posthtml posthtml-widows
```
## Usage
```js
import posthtml from 'posthtml'
import preventWidows from 'posthtml-widows'
posthtml([
preventWidows()
])
.process('
The quick brown fox
')
.then(result => console.log(result.html))
```
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-widows`
You may also specify custom attributes to use:
```js
posthtml([
preventWidows({
attributes: ['fix-widows']
})
])
.process('
The quick brown fox
')
```
## Options
### `minWords`
Type: `number`\
Default: `4`
The minimum number of words a string must contain to be processed.
```js
posthtml([
preventWidows({
minWords: 3,
})
])
.process('
Prevent widow words
')
```
### `ignore`
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
- `#{ }` - Pug
You 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.
```
### `createWidows`
Type: `boolean`\
Default: `false`
You 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
```
## License
[MIT](./LICENSE)
[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