Apply common copy-editing fixes for text, specifically to control how lines wrap. We address widows at the end of a sentence, hyphens in the last word of a sentence, and patterns of word groups that should always remain together.
Assumes we're working with HTML content.
(This has a bias towards patterns seen in medical/health information.)
To control the wraps, we insert a
entity between words.
If you're installing via npm
... Ensure your project has an .npmrc
file in the project root to tell npm
where to find the package. Ensure the following line exists:
@kpander:registry=https://npm.pkg.github.com/
Then:
$ npm install @kpander/wordwraps-js
Then, annoyingly, you'll need to copy the files you need into your project's source folder, depending on where that is. E.g., assuming your source files are in ./src/js
:
$ cp "node_modules/@kpander/wordwraps-js/dist/Wordwraps.js" ./src/js
const input = `<p>Pretend this is a long sentence with a widow.</p>`;
console.log(Wordwraps.process(input));
<p>Pretend this is a long sentence with a widow.</p>
- The last two words in an element will be joined together
- If the last word in an element has a hyphen, replace it with a non-breaking hyphen
- Make certain word patterns non-breaking
Example | Pattern | Output |
---|---|---|
n = 25 |
"n = {###}" | n = 25 |
5 days |
"{###} {seconds|minutes|hours|days|weeks|months|years|times}" | 5 days; |
Phase 5 |
"Phase {###} | Phase 5 |
Phase II |
"Phase {I|II|III|IV}" | Phase II |
Target 3 |
"Target {###} | Target 3 |
TARGET 3 |
"TARGET {###} | TARGET 3 |
P value |
"P value" | P value |
<em>P</em> value |
"<em>P</em> value" |
<em>P</em> value |
25% of |
"{###}% of" | 25% of |
Day 5 |
"Day {###}" | Day 5 |
day 5 |
"day {###}" | day 5 |
$ npm run build
This will build the distribution files in the /dist/
folder. Run this before publishing a new release.
This assumes you have an .npmrc
file in the folder with a valid Github token for creating packages.
$ npm run build
$ npm publish
You'll find Jest test cases in ./test
. To ensure all tests pass:
$ npm run test
Note: This is definitely not a robust and production-ready library. It's a quick-and-dirty solution to a specific problem.
- allow options for which elements are processed by each rule
- allow providing custom word patterns to apply
- allow defining how to make things non-breaking (
vs wrapping in a<span>
, etc.)
- Kendall Anderson ([email protected])