Hyphenation for node and Polyfill for client-side hyphenation.
The Hyphenopoly-package contains a file called hyphenopoly.module.js
.
This module provides hyphenation of plain text for node.js applications.
Note: The node module of Hyphenopoly does not support hyphenation of strings containing HTML - just plain text. If you need to hyphenate HTML-Strings parse them first.
npm install hyphenopoly
One language:
import hyphenopoly from "hyphenopoly";
const textHyphenators = hyphenopoly.config({
"require": ["en-us"],
"hyphen": "•"
});
textHyphenators.then(
function ff(hyphenateText) {
console.log(hyphenateText("Hyphenation enhances justification."));
}
).catch(
function err(e) {
console.log(e);
}
);
More then one language:
import hyphenopoly from "hyphenopoly";
const textHyphenators = hyphenopoly.config({
"require": ["de", "en-us"],
"hyphen": "•"
});
textHyphenators.get("de").then(
function ff(hyphenateText) {
console.log(hyphenateText("Silbentrennung verbessert den Blocksatz."));
}
);
textHyphenators.get("en-us").then(
function ff(hyphenateText) {
console.log(hyphenateText("Hyphenation enhances justification."));
}
);
By default, hyphenopoly.config
returns a promise (or a Map
of promises). Some code bases are not yet capable of handling async code.
By setting "sync" : true
the hyphenopoly module switches to a sync mode.
import hyphenopoly from "hyphenopoly";
const hyphenator = hyphenopoly.config({
"sync": true,
"require": ["de", "en-us"],
"hyphen": "•",
"exceptions": {
"en-us": "en-han-ces"
}
});
const hy1 = hyphenator.get("en-us")("hyphenation enhances justification.");
const hy2 = hyphenator.get("de")("Silbentrennung verbessert den Blocksatz.");
console.log(hy1);
console.log(hy2);
The .config
-method takes an object as argument:
Defaults:
{
"compound": "hyphen",
"exceptions": {},
"hyphen": String.fromCharCode(173),
"leftmin": 0,
"loader": "fs",
"minWordLength": 6,
"mixedCase": true,
"normalize": false,
"orphanControl": 1,
"paths": {
"maindir": `${__dirname}/`,
"patterndir": `${__dirname}/patterns/
},
"require": [],
"rightmin": 0,
"sync": false
}
The only option that Must be set is require
which takes an array of language-tags.
By default hyphenopoly.module.js loads pattern files and hyphenEngine by using nodes “fs”-module.
This can be changed to the “https”-module by setting the loader
to “https”:
const hyphenator = hyphenopoly.config({
"require": […],
"loader": "https"
});
This is useful if the module is transformed to a script used in a web browser (e.g. by using browserify).
For documentation about the other options see the Hyphenopoly.js
-documentation:
A list of supported languages can be programmatically obtained by looking at Hyphenopoly.supportedLanguages
:
import hyphenopoly from "hyphenopoly";
hyphenopoly.supportedLanguages.includes("en-us"); //true
hyphenopoly.supportedLanguages.includes("en"); //false
On my machine with node.js 13.7.0:
module | setup | hyphenate 270 en words |
---|---|---|
hyphenopoly | 10ms | 0.5ms |
hyphen | 18ms | 72ms |
hypher | 35ms | 1.2ms |