@rgglez/rehype-hyphenate
v0.1.0
Published
rehype plugin that inserts soft hyphens at build time with TeX (Liang) hyphenation patterns, following each element's lang attribute.
Downloads
272
Maintainers
Readme
rehype-hyphenate
rehype-hyphenate is a rehype plugin that inserts soft hyphens
(U+00AD) into the text at build time, using the TeX hyphenation patterns
(Franklin Liang's algorithm) from hyphen.
The patterns follow each element's lang attribute, so a German word inside
a Spanish paragraph is hyphenated as German.
The plugin only marks where a word may break. The browser still decides where each line ends and shows a hyphen only at a line break, so the result stays responsive. Nothing runs on the client.
Install
npm install @rgglez/rehype-hyphenateUsage
unified
import { unified } from "unified";
import rehypeParse from "rehype-parse";
import rehypeStringify from "rehype-stringify";
import rehypeHyphenate from "@rgglez/rehype-hyphenate";
const html = await unified()
.use(rehypeParse, { fragment: true })
.use(rehypeHyphenate, { lang: "es" })
.use(rehypeStringify)
.process("<p>La separación silábica funciona correctamente.</p>");
// <p>La se·pa·ra·ción si·lá·bi·ca fun·cio·na co·rrec·ta·men·te.</p>
// (· stands for the invisible U+00AD)Astro
// astro.config.mjs
import { defineConfig } from "astro/config";
import rehypeHyphenate from "@rgglez/rehype-hyphenate";
export default defineConfig({
markdown: {
rehypePlugins: [
// Language from each file's frontmatter (`lang: es`).
[rehypeHyphenate, { lang: file => file.data.astro?.frontmatter?.lang }],
],
},
});lang can also come from the file path, for example
file => file.path?.match(/\/(es|en|fr)\//)?.[1] for content stored in
language folders.
- MDX:
mdx({ extendMarkdownConfig: true })(the default) applies the same plugins to.mdxfiles. - Astro 7: Markdown uses Sätteri by default, and
markdown.rehypePluginsneeds the unified processor. Install it withnpm install @astrojs/markdown-remark. Astro 6 and earlier include it.
CSS
Soft hyphens work with the default hyphens: manual. Justified text with
text-wrap: pretty gets the closest result to TeX that browsers offer today:
article p,
article li {
text-align: justify;
hyphens: manual;
text-wrap: pretty;
}hyphens: none disables the soft hyphens. hyphens: auto adds the browser's
own hyphenation on top of them.
Options
| Option | Default | Description |
| :----- | :------ | :---------- |
| lang | none | Document language: a BCP 47 tag or (file) => tag. Without it, only text inside a lang attribute is hyphenated |
| minWordLength | 6 | Shorter words are left alone |
| hyphenChar | U+00AD | Inserted at each break point (soft hyphen) |
| exceptions | none | Fixed break points per language: { en: ["ta-ble"] } |
| ignoreTags | see below | Elements skipped with their subtree. Replaces the default |
| ignoreClassNames | [/^katex/, "expressive-code"] | Class names (string or RegExp) skipped with their subtree |
defaultIgnoreTags and defaultIgnoreClassNames are exported, so you can
extend them:
import rehypeHyphenate, { defaultIgnoreTags } from "@rgglez/rehype-hyphenate";
[rehypeHyphenate, { lang: "en", ignoreTags: [...defaultIgnoreTags, "figcaption"] }];Languages
The nearest lang attribute wins; the lang option is the fallback for text
with no lang ancestor. A tag tries its exact hyphen module first and then
its primary subtag: es-MX uses es, de-1996 uses de-1996. Patterns load
on first use, once per process.
These are left unhyphenated:
- Tags without patterns in
hyphen, andlang=""(unknown language). - Chinese, Japanese and Korean (
zh,ja,ko). Thezhmodule inhyphenis for pinyin; ask for it explicitly withzh-Latn-pinyin. - Malformed tags. The tag becomes part of an import path, so only
[a-z]{2,3}(-[a-z0-9]{1,8})*is accepted.
What is skipped
- Headings (
h1–h6). Heading-id plugins derive theidfrom the heading text, and a soft hyphen would end up inside the anchor. - Code and preformatted text (
code,pre,kbd,samp,var),head,script,style,template,textarea,svgandmath. - KaTeX output and Expressive Code frames.
- Any element with a
data-nohyphenateattribute. - Words that contain
://or@, or start withwww.. A soft hyphen in a URL or an e-mail address would be copied with it.
Search engines and copy-paste
Static search indexes read the hyphenated HTML. With Pagefind 1.5, searching for a word finds it even though the index stores it with soft hyphens, and the result excerpts keep them (they are invisible). Test other search engines before relying on them.
Most applications drop soft hyphens on paste. Some plain-text editors keep them as an invisible character.
Development
npm install
npm test
npm run buildMakefile
| Target | What it does |
| :----- | :----------- |
| make tags | Lists the existing git tags, newest first. |
| make test | Runs the tests (npm test). |
| make patch | Bumps the patch number in package.json (0.1.0 → 0.1.1), commits, tags v0.1.1, and pushes the branch and the tag to origin. |
| make publish | Runs make test, then npm publish --access public. npm runs prepublishOnly (tsc) first, so dist/ is rebuilt before upload. |
make patch only bumps the patch number. For a minor or major release, edit
version in package.json, then commit, tag and push by hand.
To publish a release:
In
package.json, change the scope inname(@rgglez) to an npm scope you have publish rights on, and log in withnpm login.Bump the version, tag it and push:
make patchTest and publish:
make publish
Live example
The plugin can be viewed live here.
You can run it locally from the example directory.
License
Copyright (c) 2026 Rodolfo González González.
Licensed under the Apache-2.0 license. Read the LICENSE file.
