hyphen

Text hyphenation in Javascript.

README

Franklin M. Liang's hyphenation algorithm

hyphen



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.

  1. ```javascript
  2. import { hyphenate } from "hyphen/en";

  3. hyphenate("A certain king had a beautiful garden").then(result => {
  4.   // A cer[-]tain king had a beau[-]ti[-]ful garden
  5.   // [-] is soft hyphen
  6. });
  7. ```

Install


  1. ```
  2. npm install hyphen
  3. ```

or

  1. ```
  2. yarn add hyphen
  3. ```

Usage


  1. ```javascript
  2. import {
  3.   hyphenate,
  4.   hyphenateHTML,
  5.   hyphenateHTMLSync,
  6.   hyphenateSync
  7. } from "hyphen/en";

  8. hyphenate("Plain text - hyphenate everything").then(result => {
  9.   // Plain text - hy[-]phen[-]ate every[-]thing
  10. });

  11. hyphenateHTML("<blockquote>HTML tags are NOT hyphenated</blockquote>").then(
  12.   result => {
  13.     //
    HTML tags are NOT hy[-]phen[-]at[-]ed
  14.   }
  15. );

  16. hyphenateHTMLSync("<blockquote>Sync version of `hyphenateHTML`</blockquote>");
  17. //
    Sync ver[-]sion of `hy[-]phen[-]ate[-]HTML`

  18. hyphenateSync("Sync version of `hyphenate`");
  19. // Sync ver[-]sion of `hy[-]phen[-]ate`
  20. ```

Options


  1. ```javascript
  2. hyphenate("Options", { debug: true, hyphenChar: "%", minWordLength: 5 });
  3. // Op%tions
  4. ```

- debug

  A Boolean indicating, if script should output debug info to console. Default is false.

- 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.

Import available languages


- hyphen/af Afrikaans
- hyphen/as Assamese
- hyphen/be Belarusian
- hyphen/bg Bulgarian
- hyphen/bn Bengali
- hyphen/ca Catalan
- hyphen/cop Coptic
- hyphen/cs Czech
- hyphen/cu Church Slavonic
- hyphen/cy Welsh
- hyphen/da Danish
- hyphen/de-1901 German, traditional spelling
- hyphen/de-1996 German, reformed spelling
- hyphen/de-CH-1901 German, traditional Swiss spelling
- hyphen/de aliases hyphen/de-1996
- hyphen/el-monoton Modern Greek, monotonic spelling
- hyphen/el-polyton Modern Greek, polytonic spelling
- hyphen/el aliases hyphen/el-monoton
- hyphen/en-gb English, British spelling
- hyphen/en-us English, American spelling
- hyphen/en aliases hyphen/en-us
- hyphen/es Spanish
- hyphen/et Estonian
- hyphen/ethi aliases hyphen/mul-ethi
- hyphen/eu Basque
- hyphen/fi Finnish
- hyphen/fr French
- hyphen/fur Friulan
- hyphen/ga Irish
- hyphen/gl Galician
- hyphen/grc Ancient Greek
- hyphen/gu Gujarati
- hyphen/hi Hindi
- hyphen/hr Croatian
- hyphen/hsb Upper Sorbian
- hyphen/hu Hungarian
- hyphen/hy Armenian
- hyphen/ia Interlingua
- hyphen/id Bahasa Indonesia, Indonesian
- hyphen/is Icelandic
- hyphen/it Italian
- hyphen/ka Georgian
- hyphen/kmr Kurmanji, Northern Kurdish
- hyphen/kn Kannada
- hyphen/la-x-classic Classical Latin
- hyphen/la-x-liturgic Liturgical Latin
- hyphen/la Latin
- hyphen/lt Lithuanian
- hyphen/lv Latvian
- hyphen/ml Malayalam
- hyphen/mn-cyrl-x-lmc Mongolian, Cyrillic script, alternative patterns
- hyphen/mn-cyrl Mongolian, Cyrillic script
- hyphen/mn aliases hyphen/mn-cyrl
- hyphen/mr Marathi
- hyphen/mul-ethi Multiple languages using the Ethiopic scripts
- hyphen/nb Norwegian Bokmål, bokmål, norsk bokmål
- hyphen/nl Dutch
- hyphen/nn Norwegian Nynorsk, nynorsk
- hyphen/no Norwegian, norsk
- hyphen/oc Occitan
- hyphen/or Odia, Oriya
- hyphen/pa Panjabi, Punjabi
- hyphen/pi Pāli
- hyphen/pl Polish
- hyphen/pms Piedmontese
- hyphen/pt Portuguese
- hyphen/rm Romansh
- hyphen/ro Romanian
- hyphen/ru Russian
- hyphen/sa Sanskrit
- hyphen/sh-cyrl Serbocroatian, Cyrillic script
- hyphen/sh-latn Serbocroatian, Latin script
- hyphen/sh aliases hyphen/sh-cyrl
- hyphen/sk Slovak
- hyphen/sl Slovenian
- hyphen/sr-cyrl Serbian, Cyrillic script
- hyphen/sr aliases hyphen/sr-cyrl
- hyphen/sv Swedish
- hyphen/ta Tamil
- hyphen/te Telugu
- hyphen/th Thai
- hyphen/tk Turkmen
- hyphen/tr Turkish
- hyphen/uk Ukrainian
- hyphen/zh-latn-pinyin Mandarin Chinese, pinyin transliteration
- hyphen/zh aliases hyphen/zh-latn-pinyin

Factory function


  1. ```javascript
  2. import createHyphenator from "hyphen";
  3. import patterns from "hyphen/patterns/en-us";

  4. const hyphenate = createHyphenator(patterns, { async: true });
  5. const hyphenateHTML = createHyphenator(patterns, { async: true, html: true });
  6. const hyphenateHTMLSync = createHyphenator(patterns, { html: true });
  7. const hyphenateSync = createHyphenator(patterns);
  8. ```

Note: This original factory function surves mostly for the backwards compatibility reasons.


Text hyphenation in CSS


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

  1. ```css
  2. p {
  3.   hyphens: auto;
  4. }
  5. ```

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.