Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
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);
// result is "A cer\u00ADtain king had a beau\u00ADti\u00ADful garden"
})();
HTML tags will automaticly skip hyphenation.
import { hyphenateHTML as hyphenate } from "hyphen/en";
(async () => {
const text = "<blockquote>A certain king had a beautiful garden</blockquote>";
const result = await hyphenate(text);
// result is "<blockquote>A cer\u00ADtain king had a beau\u00ADti\u00ADful garden</blockquote>"
})();
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);
// result is "Ein ge\u00ADwis\u00ADser Kö\u00ADnig hat\u00ADte einen wun\u00ADder\u00ADschö\u00ADnen Gar\u00ADten"
})();
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);
// 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"
})();
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);
// result is "A cer\u00ADtain king had a beau\u00ADti\u00ADful garden"
npm install hyphen
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 { 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"
})();
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";
import { hyphenate } from "hyphen/de-1901";
import { hyphenate } from "hyphen/de-1996";
import { hyphenate } from "hyphen/de-CH-1901";
import { hyphenate } from "hyphen/el-monoton";
import { hyphenate } from "hyphen/el-polyton";
import { hyphenate } from "hyphen/en-gb";
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";
import { hyphenate } from "hyphen/id";
import { hyphenate } from "hyphen/is";
import { hyphenate } from "hyphen/it";
import { hyphenate } from "hyphen/ka";
import { hyphenate } from "hyphen/kmr";
import { hyphenate } from "hyphen/kn";
import { hyphenate } from "hyphen/la-x-classic";
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";
import { hyphenate } from "hyphen/mn-cyrl-x-lmc";
import { hyphenate } from "hyphen/mn-cyrl";
import { hyphenate } from "hyphen/mr";
import { hyphenate } from "hyphen/mul-ethi";
import { hyphenate } from "hyphen/nb";
import { hyphenate } from "hyphen/nl";
import { hyphenate } from "hyphen/nn";
import { hyphenate } from "hyphen/no";
import { hyphenate } from "hyphen/oc";
import { hyphenate } from "hyphen/or";
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";
import { hyphenate } from "hyphen/sh-cyrl";
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";
import { hyphenate } from "hyphen/zh-latn-pinyin";
hyphen/de-1996
import { hyphenate } from "hyphen/de";
hyphen/el-monoton
import { hyphenate } from "hyphen/el";
hyphen/en-us
import { hyphenate } from "hyphen/en";
hyphen/mul-ethi
import { hyphenate } from "hyphen/ethi";
hyphen/mn-cyrl
import { hyphenate } from "hyphen/mn";
hyphen/sh-cyrl
import { hyphenate } from "hyphen/sh";
hyphen/sr-cyrl
import { hyphenate } from "hyphen/sr";
hyphen/zh-latn-pinyin
import { hyphenate } from "hyphen/zh";
Factory function can be used to create hyphenate
function with changed default options.
import createHyphenator from "hyphen";
import patterns from "hyphen/patterns/en-us";
const hyphenate = createHyphenator(patterns, {
// result in Promise
async: true,
// prevent HTML tags from hyphenation
html: true,
// minimum word length is 7
minWordLength: 7
});
The following are predefined hyphenate
functions.
import createHyphenator from "hyphen";
import patterns from "hyphen/patterns/en-us";
const hyphenate = createHyphenator(patterns, { async: true });
const hyphenateHTML = createHyphenator(patterns, { async: true, html: true });
const hyphenateHTMLSync = createHyphenator(patterns, { html: true });
const hyphenateSync = createHyphenator(patterns);
Predefined hyphenate
functions are set in every language pack.
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/hyphen.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hyphen/patterns/en-us.min.js"></script>
After the script is added on your page, use createHyphenator
to create a hyphenate
function.
var hyphenate = createHyphenator(hyphenationPatternsEnUs);
Check other great hyphenation libraries:
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.
FAQs
Text hyphenation in Javascript.
The npm package hyphen receives a total of 442,146 weekly downloads. As such, hyphen popularity was classified as popular.
We found that hyphen demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.