Skip to content

ytiurin/hyphen

Repository files navigation

Franklin M. Liang's hyphenation algorithm

hyphen

This is a 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 books and articles. Patterns for this library were taken from ctan.org and ported to Javascript.

var text = "A certain king had a beautiful garden";

var hyphenate = createHyphenator(hyphenationPatternsEnGb);
var hyphenatedText = hyphenate(text);

console.log(hyphenatedText);
// A cer-tain king had a beau-ti-ful garden

Check the demo

Install

npm install hyphen

Configuration

  • To change the default soft hyphen \u00AD character to something else, use hyphenChar: "-"
var hyphenate = createHyphenator(hyphenationPatternsEnGb, {hyphenChar: '-'});
  • To enable the console output of the hyphenation process, use debug: true
var hyphenate = createHyphenator(hyphenationPatternsEnGb, {debug: true});
  • There is an Async Mode available, when it's enabled, the script will work only 10ms on every event loop iteration, until finish processing. This is needed to prevent script from blocking UI, when processing large texts.
var hyphenate = createHyphenator(hyphenationPatternsEnGb, {async: true});
hyphenate(text).then(function(hyphenatedText) {
  console.log(hyphenatedText)
});

Available patterns for these languages

Afrikaans, Ancient Greek, Armenian, Assamese, Bahasa Indonesia, Basque, Bengali, Bulgarian, Catalan, Chinese pinyin syllables, Church Slavonic, Classical Latin, Coptic, Croatian, Czech, Danish, Dutch, English (GB), English (US), Estonian, Ethiopic, Finnish, French, Friulan, Galician, Georgian, German, Gujarati, Hindi, Hungarian, Icelandic Plain, Interlingua, Irish, Italian, Kannada, Kurmanji, Latin, Latvian, Lithuanian, Liturgical Latin, Malayalam, Marathi, Modern Monotonic, Modern Polytonic, Mongolian, Norwegian, Norwegian Bokmal, Norwegian Nynorsk, Occitan, Oriy, Pāli, Panjabi, Piedmontese, Polish, Portuguese, Romanian, Romansh, Russian, Sanskrit and Prakrit, Serbian Cyrillic, Serbocroatian Cyrillic, Serbocroatian Latin, Slovak, Slovenian, Spanish, Swedish, Swiss-German, Tamil, Telugu, Thai, Turkish, Turkmen, Ukrainian, Upper Sorbian, Welsh

Hyphenation in CSS

The CSS hyphens property is intended to add hyphenation support to modern browsers without Javascript:

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.

Alternatives

Check other great hyphenation libraries:

  • Hyphenator.js does client-side hyphenation of HTML-Documents.
  • Hypher A fast and small hyphenation engine.

License

MIT