

hyphen
Demo page
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 and ported to Javascript.
import { hyphenate } from "hyphen/en";
(async () => {
const text = "A certain king had a beautiful garden";
const result = await hyphenate(text);
})();
Hyphenate HTML
Processor will automaticly skip HTML tags hyphenation.
import { hyphenate } from "hyphen/en";
(async () => {
const text = "<blockquote>A certain king had a beautiful garden</blockquote>";
const result = await hyphenate(text);
})();
Multilingual hyphenation
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.
import { hyphenate } from "hyphen/de";
(async () => {
const text = "Ein gewisser König hatte einen wunderschönen Garten";
const result = await hyphenate(text);
})();
It is possible to use many langauges on the same page.
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);
const deutch = "Ein gewisser König hatte einen wunderschönen Garten";
const deutchResult = await hyphenateDe(deutch);
})();
Sync version
The hyphenate
function returns a Promise
, however a sync version of it returns a string
.
import { hyphenateSync as hyphenate } from "hyphen/en";
const text = "A certain king had a beautiful garden";
const result = hyphenate(text);
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
.
Example of using options
import { hyphenate } from "hyphen/en";
(async () => {
const text = "A certain king had a beautiful garden";
const result = await hyphenate(text, {
hyphenChar: "-"
});
})();
List of available languages
Check the list
import { hyphenate } from "hyphen/af";
import { hyphenate } from "hyphen/as";
import { hyphenate } from "hyphen/be";
import { hyphenate } from "hyphen/bg";
import { hyphenate } from "hyphen/bn";
import { hyphenate } from "hyphen/ca";
import { hyphenate } from "hyphen/cop";
import { hyphenate } from "hyphen/cs";
import { hyphenate } from "hyphen/cy";
import { hyphenate } from "hyphen/cu";
import { hyphenate } from "hyphen/da";
- German, traditional spelling
import { hyphenate } from "hyphen/de-1901";
- German, reformed spelling
import { hyphenate } from "hyphen/de-1996";
- German, traditional Swiss spelling
import { hyphenate } from "hyphen/de-CH-1901";
- Modern Greek, monotonic spelling
import { hyphenate } from "hyphen/el-monoton";
- Modern Greek, polytonic spelling
import { hyphenate } from "hyphen/el-polyton";
- English, British spelling language
import { hyphenate } from "hyphen/en-gb";
- English, American spelling language
import { hyphenate } from "hyphen/en-us";
import { hyphenate } from "hyphen/es";
import { hyphenate } from "hyphen/et";
import { hyphenate } from "hyphen/eu";
import { hyphenate } from "hyphen/fi";
import { hyphenate } from "hyphen/fr";
import { hyphenate } from "hyphen/fur";
import { hyphenate } from "hyphen/ga";
import { hyphenate } from "hyphen/gl";
import { hyphenate } from "hyphen/grc";
import { hyphenate } from "hyphen/gu";
import { hyphenate } from "hyphen/hi";
import { hyphenate } from "hyphen/hr";
import { hyphenate } from "hyphen/hsb";
import { hyphenate } from "hyphen/hu";
import { hyphenate } from "hyphen/hy";
import { hyphenate } from "hyphen/ia";
- Bahasa Indonesia, Indonesian language
import { hyphenate } from "hyphen/id";
import { hyphenate } from "hyphen/is";
import { hyphenate } from "hyphen/it";
import { hyphenate } from "hyphen/ka";
- Kurmanji, Northern Kurdish language
import { hyphenate } from "hyphen/kmr";
import { hyphenate } from "hyphen/kn";
import { hyphenate } from "hyphen/la-x-classic";
- Liturgical Latin language
import { hyphenate } from "hyphen/la-x-liturgic";
import { hyphenate } from "hyphen/la";
import { hyphenate } from "hyphen/lt";
import { hyphenate } from "hyphen/lv";
import { hyphenate } from "hyphen/ml";
- Mongolian, Cyrillic script, alternative patterns
import { hyphenate } from "hyphen/mn-cyrl-x-lmc";
- Mongolian, Cyrillic script
import { hyphenate } from "hyphen/mn-cyrl";
import { hyphenate } from "hyphen/mr";
- Multiple languages using the Ethiopic scripts
import { hyphenate } from "hyphen/mul-ethi";
- Norwegian Bokmål, bokmål, norsk bokmål language
import { hyphenate } from "hyphen/nb";
import { hyphenate } from "hyphen/nl";
- Norwegian Nynorsk, nynorsk language
import { hyphenate } from "hyphen/nn";
- Norwegian, norsk language
import { hyphenate } from "hyphen/no";
import { hyphenate } from "hyphen/oc";
import { hyphenate } from "hyphen/or";
- Panjabi, Punjabi language
import { hyphenate } from "hyphen/pa";
import { hyphenate } from "hyphen/pi";
import { hyphenate } from "hyphen/pl";
import { hyphenate } from "hyphen/pms";
import { hyphenate } from "hyphen/pt";
import { hyphenate } from "hyphen/rm";
import { hyphenate } from "hyphen/ro";
import { hyphenate } from "hyphen/ru";
import { hyphenate } from "hyphen/sa";
- Serbocroatian, Cyrillic script
import { hyphenate } from "hyphen/sh-cyrl";
- Serbocroatian, Latin script
import { hyphenate } from "hyphen/sh-latn";
import { hyphenate } from "hyphen/sk";
import { hyphenate } from "hyphen/sl";
import { hyphenate } from "hyphen/sr-cyrl";
import { hyphenate } from "hyphen/sv";
import { hyphenate } from "hyphen/ta";
import { hyphenate } from "hyphen/te";
import { hyphenate } from "hyphen/th";
import { hyphenate } from "hyphen/tk";
import { hyphenate } from "hyphen/tr";
import { hyphenate } from "hyphen/uk";
- Mandarin Chinese, pinyin transliteration
import { hyphenate } from "hyphen/zh-latn-pinyin";
Aliases for specific languages
import { hyphenate } from "hyphen/de";
- Alias for
hyphen/el-monoton
import { hyphenate } from "hyphen/el";
import { hyphenate } from "hyphen/en";
- Alias for
hyphen/mul-ethi
import { hyphenate } from "hyphen/ethi";
import { hyphenate } from "hyphen/mn";
import { hyphenate } from "hyphen/sh";
import { hyphenate } from "hyphen/sr";
- Alias for
hyphen/zh-latn-pinyin
import { hyphenate } from "hyphen/zh";
Factory function
Factory function can be used to create hyphenate
function with changed default options.
Create hyphenation function with predefined exception list
import createHyphenator from "hyphen";
import patterns from "hyphen/patterns/en-us";
const hyphenate = createHyphenator(patterns, {
async: true,
exceptions: ["present", "ta-ble"]
});
Predefined functions
The following are predefined hyphenate
functions.
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.
<script src="https://cdn.jsdelivr.net/npm/hyphen@1.10.6/patterns/en-us.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hyphen@1.10.6/hyphen.min.js"></script>
After the script is added on your page, use еру createHyphenator
to create a hyphenate
function.
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:
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
will be deprecated in further versions;
Migration
from 1.9.1 to 1.10.0
Option html
default value changed from false
to true
In cases when text parser should not skip HTML tags, apply the following code changes.
Default exported hyphenate
function
hyphenate(text);
hyphenate(text, { html: false });
Create hyphenate
function with pre 1.10.0 behavior using a factory function:
const hyphenate = createHyphenator(patterns, {
async: true,
html: false
});
hyphenate(text);
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!