rehype-widont

Rehype plugin to prevent lines with single words

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
rehype-widont
280.1.13 years ago3 years agoMinified + gzip package size for rehype-widont in KB

Readme

rehype-widont
This Rehype plugin prevents what is commonly called "widows" in typesetting lingo, i.e., lines of text made of only one word. This is achieved by replacing whitespace before the last word with a non-breaking space (U+00A0), which makes that word and the preceding one stick together.
Although rehype-widont uses a different approach, it borrows the name of Shaun Inman's WordPress plugin.

Installation

npm install rehype-widont

# or, if using Yarn:
yarn add rehype-widont

Usage

NOTE: rehype-widont inserts an actual non-breaking space character instead of  , but the entity is used in the following example for clarity's sake.
var rehype = require("rehype");
var widont = require("rehype-widont");

var processor = rehype()
  .data("settings", { fragment: true })
  .use(widont);

processor.processSync("<p>This shall be just fine.</p>");
// returns "<p>This shall be just fine.</p>"

processor.processSync("<p>It can handle <em>nested tags</em>.</p>");
// returns "<p>It can handle <em>nested tags</em>.</p>"

processor.processSync("<p>See? This <em>is</em> awesome.</p>");
// returns "<p>See? This <em>is</em> awesome.</p>"

Options

selector

Which tags to look inside and prevent widows (string, default: "p, h1, h2, h3, h4, h5, h6, li, dt, dd"). Accepts any value that hast-util-select does.

License

MIT.