jenswittmann / kirby-jolitypo
Microtypography for Kirby: JoliTypo as a field and blocks method (quotes, dashes, ellipsis, no-break spaces between numbers and units).
Package info
github.com/jenswittmann/kirby-jolitypo
Type:kirby-plugin
pkg:composer/jenswittmann/kirby-jolitypo
Requires
- php: >=8.4
- getkirby/composer-installer: ^1.2
- jolicode/jolitypo: ^2.0
Requires (Dev)
- getkirby/cms: ^5.0
- phpunit/phpunit: ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Beta: this plugin is at an early stage, and its options and behavior may still change.
Microtypography for Kirby CMS, built on JoliTypo. It fixes the small things editors never get right by hand, when the page is rendered. The stored content stays exactly as it was written.
| Before | After |
|---|---|
Er sagte "Hallo" - und ging. |
Er sagte „Hallo“ – und ging. |
Der “Film” läuft. (German) |
Der „Film“ läuft. |
Kino's, 3 x 4, Und dann... |
Kino’s, 3 × 4, Und dann… |
10-12 Uhr |
10–12 Uhr |
6 %, 40,00 Euro, 5 km |
number and unit joined by a no-break space |
z.B., § 3 Abs. 1, Art. 6, 2 Mio. Euro |
no line break inside (German) |
Kirby Typographer or Kirby JoliTypo?
There is already a very good microtypography plugin for Kirby: Kirby Typographer by hana+nils. If you only need quotes, dashes and the like, it may be all you need. This plugin exists because we needed more around numbers, units and German abbreviations, and wanted a maintained library underneath.
The comparison is based on the READMEs and source of both projects. Where Typographer is concerned, it is not a test result.
| Kirby Typographer | Kirby JoliTypo | |
|---|---|---|
| Based on | Own rules, PHP DOMDocument |
JoliTypo (JoliCode), PHP's HTML5 parser |
| Kirby API | ->typographer() on fields and blocks, plus filter(), setAttributes(), rename(), level() and other manipulations |
->jolitypo() on fields, blocks and single blocks, plus Typo::fix() |
Quotes, apostrophes, ellipsis, dashes, ×, ™ |
yes | yes |
Primes (41° 43′ 32.99″) |
yes | no, JoliTypo leaves foot and inch marks alone |
| Widows (widont) | yes | no |
Number and unit joined by a no-break space (6 %, 5 km) |
not in the README | yes (Units) |
German abbreviations and references (z. B., § 3, Art. 6) |
detects abbreviations | joins them with a no-break space (Abbreviations) |
Repairs “…” in German text |
not in the README | yes (NormalizeQuotes) |
ISO dates in running text (2027-04-13) |
not in the README | kept as they are (SafeDash) |
| Long words | wraps them in <span class="long-word"> for CSS hyphenation |
optional soft hyphens (hyphenation, hyphenationMinLength) |
| Languages | German, English, French | quote styles and rules for many locales |
| Kirby | README written for Kirby 3 | Kirby 5 |
| PHP | no requirement set | 8.4+ |
If you are on PHP 8.3 or older, or you want widow control and primes, use Typographer. If you work with numbers, units and German legal or funding texts, or you need the HTML5 parser, use this one. You can also run both, but there is no need: they fix the same characters.
Requirements
- Kirby 5
- PHP 8.4+ (JoliTypo 2 uses PHP's HTML5 parser, which keeps Alpine attributes like
@clickand SVG attributes likeviewBoxintact. JoliTypo 1 uses the older DOM parser and does not.) - PHP extensions
domandmbstring
Installation
composer require jenswittmann/kirby-jolitypo
Usage
// A field that holds HTML (writer, textarea with kirbytext, markdown) <?= $page->text()->kirbytext()->jolitypo() ?> <?= $page->intro()->jolitypo() ?> // Blocks and single blocks: renders them, then fixes the HTML <?= $page->builder()->toBlocks()->jolitypo() ?> <?= $block->jolitypo() ?> // Anywhere else <?= jolitypo($html) ?> <?= jolitypo($html, 'en') ?>
The locale comes from the current Kirby language (de becomes de_DE, en becomes en_GB, …). Pass a language code or locale to override it: ->jolitypo('fr').
The input is HTML, and so is the result. A plain-text field such as $page->title()->jolitypo() works too, but & and < come back escaped (&, <), so do not escape the result a second time.
Put it in one place. Call it once on the output you want fixed (a block snippet, a text field), not on a field that you pass through again.
Options
// site/config/config.php return [ 'jenswittmann.kirby-jolitypo' => [ 'hyphenation' => false, 'hyphenationMinLength' => null, 'strictUnits' => true, 'units' => [], 'locales' => [], 'fallbackLocale' => 'en_GB', 'protectedTags' => null, 'rules' => null, ], ];
| Option | Default | |
|---|---|---|
rules |
null |
JoliTypo's recommended rules for the locale (see below). A list applies to every language. A map keyed by language or locale ('de' => [...], '*' => [...]) picks per language. Entries are JoliTypo fixer names, class names or FixerInterface instances. |
strictUnits |
true |
Bind a number only to a known unit. false uses JoliTypo's own Unit fixer, which binds a number to any following word. |
units |
[] |
Extra units for the strict list, e.g. ['Std.', 'Tage']. |
hyphenation |
false |
Adds JoliTypo's Hyphen fixer (soft hyphens). |
hyphenationMinLength |
null |
Only words with at least this many characters get soft hyphens (JoliTypo's own default is 6). Set it to e.g. 18 to hyphenate only very long words. |
locales |
[] |
Language code → locale, merged over the built-in map (de, en, fr, es, it, nl, pl, pt). |
fallbackLocale |
en_GB |
Used for unknown languages and for single-language sites. |
protectedTags |
null |
Tags that are never touched. null keeps JoliTypo's head, link, pre, code, script, style. |
Default rules
JoliTypo's recommended rules for the locale, with these changes:
Hyphenis left out (seehyphenation).Dashis replaced bySafeDash.Unitis replaced byUnits(seestrictUnits).NormalizeQuotesruns beforeSmartQuotes.Abbreviationsruns last.
The four own fixers live in JensWittmann\JoliTypo\Fixer:
SafeDashwraps JoliTypo'sDash. The stock fixer turns every hyphen between digits into an en dash, which would break ISO dates in running text (2027-04-13).SafeDashhides them. It also treats a hyphen between no-break spaces as a dash.Unitsbinds%, currency,km,kg,Uhr,Mio.and similar to the number before them, and nothing else.NormalizeQuotesturns complete“…”pairs into„…“forde_DEandde_AT. Straight quotes are left toSmartQuotes. A lone quote is left alone.Abbreviationsis German only.z.B.andz. B.becomez.<nbsp>B., and§,Art.,Abs.,Nr.,S.,ca.,Satz,Zifferkeep their number on the same line.
Things to know
- Hyphens between numbers always become en dashes (
10-12). That is right for ranges and wrong for phone numbers like030-123456. ISO dates are safe. - JoliTypo needs complete pairs. A quote that is never closed stays as it is.
- Output uses
for no-break spaces and real characters for everything else. - Cost: every call parses the HTML once. Kirby's page cache makes this a one-time cost per page and language. Do not call it on content you cannot cache.
- The stored content is not changed. If you also need the text itself fixed (search index, exports), run the same fixers over it yourself with
Typo::fix().
Development
composer install vendor/bin/phpunit
Thanks
Thanks to hana+nils for Kirby Typographer. It showed how microtypography fits into Kirby, and the field and blocks methods of this plugin follow its example. And thanks to JoliCode for JoliTypo, which does the actual work.
License
MIT. JoliTypo is © JoliCode, also MIT.