jorisnoo / statamic-safe-entities
A Statamic addon that provides a Blade directive and Bard buttons for safe HTML entity insertion (soft hyphens, non-breaking spaces, etc.).
Package info
github.com/jorisnoo/statamic-safe-entities
Type:statamic-addon
pkg:composer/jorisnoo/statamic-safe-entities
Requires
- php: ^8.2
- statamic/cms: ^5.0 || ^6.0
Requires (Dev)
- orchestra/testbench: ^9.0 || ^10.0
- phpunit/phpunit: ^11.0
README
A Statamic addon for safe HTML entity insertion in Bard, text, and textarea fields. Adds a Bard toolbar button for inserting soft hyphens, non-breaking spaces, en dashes, and other typography entities, plus Blade directives to render them safely.
Installation
composer require jorisnoo/statamic-safe-entities
Configuration
Publish the config file to customize which entities are available:
php artisan vendor:publish --tag="safe-entities-config"
Default configuration:
return [ 'entities' => [ '­' => 'Soft Hyphen', ' ' => 'Non-Breaking Space', '–' => 'En Dash', ], ];
Each entry maps an entity code to a label shown in the Bard toolbar dropdown.
Usage
Bard toolbar
The addon adds an "&" button to the Bard toolbar. Clicking it shows a dropdown of configured entities. Selecting one inserts the entity code at the cursor position.
Blade directives
Two Blade directives are available for rendering entities safely:
@entities($text) — for text/textarea fields
Escapes the string for safe HTML output, then restores configured entity codes so they render as actual entities.
<p>@entities($entry->title)</p>
@entitiesHtml($html) — for Bard/rich-text fields
Restores double-encoded entities (&shy; back to ­) in already-rendered HTML.
<div>@entitiesHtml($entry->content)</div>
PHP
You can also call the methods directly:
use Noo\SafeEntities\SafeEntities; $text = SafeEntities::render($value); $html = SafeEntities::renderHtml($value);
Both methods accept an optional second argument to override which entities to restore:
SafeEntities::render($value, ['­', ' ']);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.