Text Hyphenation Library

1.14.1 · active · verified Sun Apr 19

The `hyphen` library provides robust text hyphenation capabilities in JavaScript, based on Franklin M. Liang's widely adopted hyphenation algorithm. It leverages pre-compiled hyphenation patterns sourced from ctan.org for various languages. Currently stable at version 1.14.1, the package is primarily feature-driven with an irregular release cadence. Key differentiators include its extensive language support via separate pattern imports (e.g., `hyphen/en`, `hyphen/de`), automatic skipping of HTML tags during hyphenation, and the provision of both asynchronous (`hyphenate`) and synchronous (`hyphenateSync`) functions to suit different application contexts. Users can configure hyphenation with options for exceptions, the soft hyphen character, and minimum word length for processing.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates asynchronous and synchronous hyphenation for English and German text, including custom options like exceptions and hyphenation character.

import { hyphenate } from "hyphen/en";

(async () => {
  const text = "A certain king had a beautiful garden that needed careful tending and regular watering.";

  // Example with custom options
  const options = {
    exceptions: ["beautiful"], // Don't hyphenate 'beautiful'
    hyphenChar: '·', // Use a middle dot instead of soft hyphen
    minWordLength: 6 // Only hyphenate words 6 characters or longer
  };

  const result = await hyphenate(text, options);
  console.log(result);
  // Expected output (approx): "A cer·tain king had a beautiful gar·den that need·ed care·ful tend·ing and reg·u·lar wa·ter·ing."

  // Using the synchronous version for a different language
  const { hyphenateSync } = await import('hyphen/de');
  const germanText = "Ein gewisser König hatte einen wunderschönen Garten, der sorgfältige Pflege und regelmäßiges Gießen benötigte.";
  const germanResult = hyphenateSync(germanText);
  console.log(germanResult);
  // Expected output (approx): "Ein ge·wis·ser Kö·nig hat·te einen wun·der·schö·nen Gar·ten, der sorg·fäl·ti·ge Pfle·ge und re·gel·mä·ßi·ges Gie·ßen be·nö·tig·te."
})();

view raw JSON →