
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
prevent-widows
Advanced tools
Prevent widows from appearing in a string.
This module replaces the spaces and hyphens at the end of a paragraph with non-breaking spaces and non-breaking hyphens to prevent widows.
It comes with support for strings and posthtml.
npm install prevent-widows
# - or -
yarn add prevent-widows
Prevent widows from appearing in a string.
const preventWidows = require("prevent-widows");
preventWidows("lorem ipsum dolar sit a met");
// lorem ipsum dolar sit a met
import preventWidows from "prevent-widows";
preventWidows("lorem ipsum dolar sit a met");
// lorem ipsum dolar sit a met
Defines the type of output to transform the spaces and hyphens.
1.0.0OptionalEncodings.HTMLEncodingThis table describes how values will be transformed depending on what type of pre-defined encoding you specify.
| Encoding | Space character | Hyphen character |
|---|---|---|
| html | | ‑ |
| unicode | \u00a0 | \u2011 |
For example:
preventWidows("lorem ipsum dolar sit a met", { encoding: Encodings.UNICODE });
Alternatively, a custom encoding can be defined using an object:
preventWidows("lorem ipsum dolar sit a met", {
encoding: { space: "_", hyphen: "~" },
});
// lorem ipsum dolar sit a_met
This module comes with out-of-the-box support for posthtml.
The posthtml function exposes an additional parameter: posthtmlOptions.
import posthtml from "posthtml";
import preventWidows from "prevent-widows";
posthtml().use(preventWidows.posthtml(posthtmlOptions, preventWidowsOptions));
import posthtml from "posthtml";
import preventWidows from "prevent-widows";
(async () => {
const input = "<div prevent-widows>lorem ipsum dolar sit a met</div>";
const { html } = await posthtml().use(preventWidows.posthtml()).process(input);
console.log(html);
})();
// <div>lorem ipsum dolar sit a met</div>
The posthtml method also comes with the following options:
The name of the attribute which identifies where widows should be prevented on its children.
1.0.0Optionalprevent-widows<div prevent-widows>Prevent widows</div>
Whether or not to remove the attribute (see: attrName) from the element after
the transform has been applied.
1.0.0Optionaltruetrue or falseWhen true and by default, this will output:
<div>Prevent widows</div>
When false, this will output:
<div prevent-widows>Prevent widows</div>
FAQs
Prevent widows from appearing in a string.
The npm package prevent-widows receives a total of 1,005 weekly downloads. As such, prevent-widows popularity was classified as popular.
We found that prevent-widows demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.