Text hyphenation in Javascript.
npm install hyphen!Franklin M. Liang's hyphenation algorithm


This is a text hyphenation library, based on Franklin M. Liang's hyphenation algorithm. In core of the algorithm lies a set of hyphenation patterns. They are extracted from hand-hyphenated dictionaries. Patterns for this library were taken from ctan.org is the central place for all kinds of material around TEX.") and ported to Javascript.
``javascript
import { hyphenate } from "hyphen/en";
(async () => {
const text = "A certain king had a beautiful garden";
const result = await hyphenate(text);
// result is "A cer\u00ADtain king had a beau\u00ADti\u00ADful garden"
})();
`
Processor will automaticly skip HTML tags hyphenation.
`javascript
import { hyphenate } from "hyphen/en";
(async () => {
const text = "
A certain king had a beautiful garden";
const result = await hyphenate(text);
// result is "
A cer\u00ADtain king had a beau\u00ADti\u00ADful garden"
Select language
To hypehante text in any other supported language, just change the
import source. For example for German language, import a hyphenation function from a "hyphen/de" source.`javascript
import { hyphenate } from "hyphen/de";(async () => {
const text = "Ein gewisser König hatte einen wunderschönen Garten";
const result = await hyphenate(text);
// result is "Ein ge\u00ADwis\u00ADser Kö\u00ADnig hat\u00ADte einen wun\u00ADder\u00ADschö\u00ADnen Gar\u00ADten"
})();
`Multilingual hyphenation
It is possible to use many langauges on the same page.
`javascript
import { hyphenate as hyphenateEn } from "hyphen/en";
import { hyphenate as hyphenateDe } from "hyphen/de";(async () => {
const english = "A certain king had a beautiful garden";
const englishResult = await hyphenateEn(english);
// result is "A cer\u00ADtain king had a beau\u00ADti\u00ADful garden"
const deutch = "Ein gewisser König hatte einen wunderschönen Garten";
const deutchResult = await hyphenateDe(deutch);
// result is "Ein ge\u00ADwis\u00ADser Kö\u00ADnig hat\u00ADte einen wun\u00ADder\u00ADschö\u00ADnen Gar\u00ADten"
})();
`Sync version
Use a sync version when you need to write a synchronous code.
`javascript
import { hyphenateSync as hyphenate } from "hyphen/en";const text = "A certain king had a beautiful garden";
const result = hyphenate(text);
// result is "A cer\u00ADtain king had a beau\u00ADti\u00ADful garden"
`Install
`
npm install hyphen
`Install types definitions for Typescript usage.
`
npm install --save-dev @types/hyphen
`Types definitions are created and maintained by Krisztián Balla.
Options
- exceptions
An
Array of values with exceptions of hyphenation in words. Hard hyphen symbol - should be used to mark the position of further configured hyphenation symbol. Default value is [].- hyphenChar
A
String sets a value of the soft hyphen character. Default value is \u00AD.- minWordLength
A
Number sets the minimum length of the word, intended for hyphenation. Default value is 5.$3
`javascript
import { hyphenate } from "hyphen/en";(async () => {
const text = "A certain king had a beautiful garden";
const result = await hyphenate(text, {
hyphenChar: "-"
});
// result is "A cer-tain king had a beau-ti-ful garden"
})();
`List of available languages
Check the list
- Afrikaans language
`javascript
import { hyphenate } from "hyphen/af";
`- Albanian language
`javascript
import { hyphenate } from "hyphen/sq";
`- Armenian language
`javascript
import { hyphenate } from "hyphen/hy";
`- Assamese language
`javascript
import { hyphenate } from "hyphen/as";
`- Basque language
`javascript
import { hyphenate } from "hyphen/eu";
`- Belarusian language
`javascript
import { hyphenate } from "hyphen/be";
`- Bengali language
`javascript
import { hyphenate } from "hyphen/bn";
`- Bulgarian language
`javascript
import { hyphenate } from "hyphen/bg";
`- Catalan language
`javascript
import { hyphenate } from "hyphen/ca";
`- Coptic language
`javascript
import { hyphenate } from "hyphen/cop";
`- Croatian language
`javascript
import { hyphenate } from "hyphen/hr";
`- Czech language
`javascript
import { hyphenate } from "hyphen/cs";
`- Danish language
`javascript
import { hyphenate } from "hyphen/da";
`- Dutch language
`javascript
import { hyphenate } from "hyphen/nl";
`- English, American spelling language
`javascript
import { hyphenate } from "hyphen/en-us";
`- English, British spelling language
`javascript
import { hyphenate } from "hyphen/en-gb";
`- Estonian language
`javascript
import { hyphenate } from "hyphen/et";
`- Multiple languages using the Ethiopic scripts
`javascript
import { hyphenate } from "hyphen/mul-ethi";
`- Finnish language
`javascript
import { hyphenate } from "hyphen/fi";
`- Finnish language (school hyphenation)
`javascript
import { hyphenate } from "hyphen/fi-x-school";
`- French language
`javascript
import { hyphenate } from "hyphen/fr";
`- Friulan language
`javascript
import { hyphenate } from "hyphen/fur";
`- Galician language
`javascript
import { hyphenate } from "hyphen/gl";
`- Georgian language
`javascript
import { hyphenate } from "hyphen/ka";
`- German, traditional spelling
`javascript
import { hyphenate } from "hyphen/de-1901";
`- German, reformed spelling
`javascript
import { hyphenate } from "hyphen/de-1996";
`- German, traditional Swiss spelling
`javascript
import { hyphenate } from "hyphen/de-ch-1901";
`- Modern Greek, monotonic spelling
`javascript
import { hyphenate } from "hyphen/el-monoton";
`- Modern Greek, polytonic spelling
`javascript
import { hyphenate } from "hyphen/el-polyton";
`- Ancient Greek language
`javascript
import { hyphenate } from "hyphen/grc";
`- Gujarati language
`javascript
import { hyphenate } from "hyphen/gu";
`- Hindi language
`javascript
import { hyphenate } from "hyphen/hi";
`- Hungarian language
`javascript
import { hyphenate } from "hyphen/hu";
`- Icelandic language
`javascript
import { hyphenate } from "hyphen/is";
`- Bahasa Indonesia, Indonesian language
`javascript
import { hyphenate } from "hyphen/id";
`- Interlingua language
`javascript
import { hyphenate } from "hyphen/ia";
`- Irish language
`javascript
import { hyphenate } from "hyphen/ga";
`- Italian language
`javascript
import { hyphenate } from "hyphen/it";
`- Kannada language
`javascript
import { hyphenate } from "hyphen/kn";
`- Kurmanji, Northern Kurdish language
`javascript
import { hyphenate } from "hyphen/kmr";
`- Latin language
`javascript
import { hyphenate } from "hyphen/la";
`- Classical Latin language
`javascript
import { hyphenate } from "hyphen/la-x-classic";
`- Liturgical Latin language
`javascript
import { hyphenate } from "hyphen/la-x-liturgic";
`- Latvian language
`javascript
import { hyphenate } from "hyphen/lv";
`- Lithuanian language
`javascript
import { hyphenate } from "hyphen/lt";
`- Macedonian language
`javascript
import { hyphenate } from "hyphen/mk";
`- Marathi language
`javascript
import { hyphenate } from "hyphen/mr";
`- Malayalam language
`javascript
import { hyphenate } from "hyphen/ml";
`- Mandarin Chinese, pinyin transliteration
`javascript
import { hyphenate } from "hyphen/zh-latn-pinyin";
`- Mongolian, Cyrillic script
`javascript
import { hyphenate } from "hyphen/mn-cyrl";
`- Mongolian, Cyrillic script, alternative patterns
`javascript
import { hyphenate } from "hyphen/mn-cyrl-x-lmc";
`- Norwegian, norsk language
`javascript
import { hyphenate } from "hyphen/no";
`- Norwegian Bokmål, bokmål, norsk bokmål language
`javascript
import { hyphenate } from "hyphen/nb";
`- Norwegian Nynorsk, nynorsk language
`javascript
import { hyphenate } from "hyphen/nn";
`- Occitan language
`javascript
import { hyphenate } from "hyphen/oc";
`- Odia, Oriya language
`javascript
import { hyphenate } from "hyphen/or";
`- Pāli language
`javascript
import { hyphenate } from "hyphen/pi";
`- Panjabi, Punjabi language
`javascript
import { hyphenate } from "hyphen/pa";
`- Piedmontese language
`javascript
import { hyphenate } from "hyphen/pms";
`- Polish language
`javascript
import { hyphenate } from "hyphen/pl";
`- Portuguese language
`javascript
import { hyphenate } from "hyphen/pt";
`- Romansh language
`javascript
import { hyphenate } from "hyphen/rm";
`- Romanian language
`javascript
import { hyphenate } from "hyphen/ro";
`- Russian language
`javascript
import { hyphenate } from "hyphen/ru";
`- Sanskrit language
`javascript
import { hyphenate } from "hyphen/sa";
`- Serbian, Cyrillic script
`javascript
import { hyphenate } from "hyphen/sr-cyrl";
`- Serbocroatian, Cyrillic script
`javascript
import { hyphenate } from "hyphen/sh-cyrl";
`- Serbocroatian, Latin script
`javascript
import { hyphenate } from "hyphen/sh-latn";
`- Church Slavonic language
`javascript
import { hyphenate } from "hyphen/cu";
`- Slovak language
`javascript
import { hyphenate } from "hyphen/sk";
`- Slovenian language
`javascript
import { hyphenate } from "hyphen/sl";
`- Upper Sorbian language
`javascript
import { hyphenate } from "hyphen/hsb";
`- Spanish language
`javascript
import { hyphenate } from "hyphen/es";
`- Swedish language
`javascript
import { hyphenate } from "hyphen/sv";
`- Tamil language
`javascript
import { hyphenate } from "hyphen/ta";
`- Telugu language
`javascript
import { hyphenate } from "hyphen/te";
`- Thai language
`javascript
import { hyphenate } from "hyphen/th";
`- Turkish language
`javascript
import { hyphenate } from "hyphen/tr";
`- Turkmen language
`javascript
import { hyphenate } from "hyphen/tk";
`- Ukrainian language
`javascript
import { hyphenate } from "hyphen/uk";
`- Welsh language
`javascript
import { hyphenate } from "hyphen/cy";
`$3
- Alias for
hyphen/de-1996`javascript
import { hyphenate } from "hyphen/de";
`- Alias for
hyphen/el-monoton`javascript
import { hyphenate } from "hyphen/el";
`- Alias for
hyphen/en-us`javascript
import { hyphenate } from "hyphen/en";
`- Alias for
hyphen/mul-ethi`javascript
import { hyphenate } from "hyphen/ethi";
`- Alias for
hyphen/mn-cyrl`javascript
import { hyphenate } from "hyphen/mn";
`- Alias for
hyphen/sh-cyrl`javascript
import { hyphenate } from "hyphen/sh";
`- Alias for
hyphen/sr-cyrl`javascript
import { hyphenate } from "hyphen/sr";
`- Alias for
hyphen/zh-latn-pinyin`javascript
import { hyphenate } from "hyphen/zh";
`Factory function
Factory function can be used to create
hyphenate function with changed default options.$3
`javascript
import createHyphenator from "hyphen";
import patterns from "hyphen/patterns/en-us";const hyphenate = createHyphenator(patterns, {
// result in Promise
async: true,
// exceptions of hyphenation
exceptions: ["present", "ta-ble"]
});
`$3
The following are predefined
hyphenate functions.`javascript
import createHyphenator from "hyphen";
import patterns from "hyphen/patterns/en-us";const hyphenate = createHyphenator(patterns, {
async: true
});
const hyphenateSync = createHyphenator(patterns);
`Predefined
hyphenate functions are set in every language pack.jsDelivr CDN for older websites
It is possible to use
hyphen on older websites with jsDelivr network. Check the package page on their website.`html
`After the script is added on your page, use the
createHyphenator to create a hyphenate function.`javascript
var hyphenate = createHyphenator(hyphenationPatternsEnUs, {
async: true
});
`Alternatives
Check other great hyphenation libraries:
- Hyphenopoly does client-side hyphenation of HTML-Documents.
- Hypher A fast and small hyphenation engine.
Text hyphenation in CSS
The CSS
hyphens property is intended to add hyphenation support to modern browsers without Javascript:`css
p {
hyphens: auto;
}
`It is part of the CSS Text Level 3 specification. The browser compatibility list can be found on the related MDN page.
DEPRECATED
- Option
debug was deprecated in version 1.13.0;Migration
from 1.9.1 to 1.10.0
Option
html default value changed from false to trueIn cases when text parser should not skip HTML tags, apply the following code changes.
Default exported
hyphenate function`javascript
// Code before 1.10.0
hyphenate(text);
``javascript
// Code after 1.10.0
hyphenate(text, { html: false });
`Create
hyphenate function with pre 1.10.0 behavior using a factory function:`javascript
// Code after 1.10.0
const hyphenate = createHyphenator(patterns, {
async: true,
html: false
});hyphenate(text);
``Thanks goes to these wonderful people (emoji key):
Eugene Tiurin 🤔 💻 🚧 | Krisztián Balla 🐛 🧑🏫 📣 | Robin Millette 💻 🐛 | Asko Soukka 💻 🐛 | Nicolas Sierra 💻 🐛 |
Jaume Ortolà 💻 🐛 | Simon Osterlehner 💻 | Jason Wohlgemuth 📖 | Kamil Mielnik 💻 🐛 | Oskar Köök 💻 🐛 |
Add your contributions | ||||
This project follows the all-contributors specification. Contributions of any kind welcome!