hirasso / prose
Format rich text / prose HTML: mark external links, strip tags, tidy empty elements 🐘
Fund package maintenance!
Requires
- php: ^8.4 | ^8.5
Requires (Dev)
- laravel/pint: ^1.20
- pestphp/pest: ^3.7
- pestphp/pest-plugin-watch: ^3.0
- phpstan/phpstan: ^2.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-26 13:12:20 UTC
README
Format rich text / prose HTML in PHP. Fully HTML5-compatible 🐘
A tiny, framework-agnostic formatter for WYSIWYG / rich text content. Pass a string, get a string back:
- Mark external links (
target="_blank", custom attributes) - Strip tags to an allowlist
- Remove whitespace-only elements (e.g. empty
<p>)
Installation
composer require hirasso/prose
Usage
use Hirasso\Prose\Prose; use Hirasso\Prose\ProseOptions; // Minimal: sensible defaults echo Prose::format($html); // Configured echo Prose::format($html, new ProseOptions( allowedTags: ['p', 'a', 'br', 'strong', 'em', 'ul', 'li'], siteUrl: 'https://example.com', // required to detect external links removeEmptyElements: ['p'], ));
Prose::format() accepts an HTML fragment or a full (or partial) HTML document. Only the
body is formatted; the doctype, <head>, body attributes, comments and anything wrapping
the body are preserved verbatim.
If you already have a Dom\HTMLDocument (e.g. from another DOM-based tool), pass it
directly. Only its body is formatted, in place, and the same document is returned:
$doc = Prose::format($doc, $options);
Options
| Option | Default | Purpose |
|---|---|---|
allowedTags |
null |
Tags to keep. Other elements are unwrapped (content kept), comments removed. null = don't strip. |
removeEmptyElements |
[] |
Selectors of whitespace-only elements to prune. |
siteUrl |
null |
Your site's URL. Required to detect external links. |
externalLinkAttributes |
['data-external' => '', 'target' => '_blank'] |
Attributes added to external links. |
WordPress / ACF
The package ships no framework glue. Wire it into an acf/format_value filter
in your project:
add_filter('acf/format_value/type=wysiwyg', function (mixed $value, $postID, array $field): mixed { if (!is_string($value) || trim($value) === '') { return $value; } return Prose::format($value, new ProseOptions( allowedTags: ['p', 'a', 'br', 'blockquote', 'strong', 'b', 'i', 'em', 'ul', 'li', 'sup'], siteUrl: home_url(), )); }, 11, 3);
Obfuscating email / phone links
Prose doesn't obfuscate anything itself. Combine it with
hirasso/html-obfuscator:
use function Hirasso\HTMLObfuscator\obfuscate; use function Hirasso\HTMLObfuscator\clientScript; echo obfuscate(Prose::format($html))->saveHTML(); // Render the client script once, in <head>: echo clientScript();
License
MIT © Rasso Hilber