Search by

wysio / laravel

EveraertCooremanYorlia

Laravel integration for Wysio Editor, a ProseMirror-based WYSIWYG editor.

v0.1.0-alpha.1 2026-12-02 16:17 UTC

README

First-class Laravel support for Wysio Editor, a self-hosted, MIT-licensed WYSIWYG editor built on ProseMirror: a Blade component, a server-side HTML sanitiser, a validation rule, an Eloquent cast and server-side rendering of the editor's HTML, JSON and Markdown.

Alpha. The API can still change between alpha releases. Livewire and Filament support come later.

  • Blade component: <x-wysio-editor name="body" /> in any form. Assets load once per page, only where an editor is used.
  • Sanitiser: an allow-list that mirrors the editor's schema, built on symfony/html-sanitizer.
  • Validation: WysioContent checks format, real (non-empty) content and plain-text length.
  • Eloquent cast: WysioHtml sanitises on the way into the database.
  • Rendering: ProseMirror JSON and Markdown to sanitised HTML, and HTML to plain text.
  • Vue / Inertia: use @wysio/vue from npm with the PHP helpers above. The package never loads editor JavaScript or CSS unless you use the Blade component.

Requires PHP 8.2+ and Laravel 12 or 13.

Contents

Installation

composer require wysio/laravel:^0.1@alpha

The service provider and the Wysio facade are registered automatically.

For the Blade component, publish the editor's script and stylesheet to public/vendor/wysio, or load them from a CDN (see Configuration):

php artisan vendor:publish --tag=wysio-assets

The published files are also tagged laravel-assets. The default Laravel composer.json republishes that tag after every composer update, so the published editor stays in step with the package. If your app doesn't have that post-update-cmd hook, add it or republish wysio-assets after updating.

To change the defaults, publish the config:

php artisan vendor:publish --tag=wysio-config

Quick start: Blade

Make sure your layout has styles and scripts stacks. The component pushes the editor's assets to them:

<head>
    …
    @stack('styles')
</head>
<body>
    {{ $slot }}
    @stack('scripts')
</body>

Add the editor to a form:

<form method="POST" action="{{ route('posts.store') }}">
    @csrf
    <x-wysio-editor name="body" :value="$post->body" placeholder="Start writing…" label="Body" />
    @error('body') <p class="error">{{ $message }}</p> @enderror
    <button>Save</button>
</form>

Validate the content and let the cast sanitise it:

use Wysio\Laravel\Rules\WysioContent;

public function store(Request $request)
{
    $data = $request->validate([
        'body' => [new WysioContent(maxLength: 20_000, required: true)],
    ]);

    Post::create($data);
}
use Wysio\Laravel\Casts\WysioHtml;

class Post extends Model
{
    protected function casts(): array
    {
        return ['body' => WysioHtml::class];
    }
}

Render it. The stored HTML is already sanitised:

<article class="wysio-content">{!! $post->body !!}</article>

Pages that display content need the editor's rules for alignment, image and colour classes. Include vendor/wysio/style.css there too, or copy those rules into your own CSS. Image alignment is scoped to the wysio-content class, which also applies the editor's padding, minimum height and white-space: break-spaces. Override those for display pages if they don't suit your layout.

Quick start: Vue / Inertia

Install the editor from npm:

npm install wysio@alpha @wysio/vue@alpha
<script setup>
import { useForm } from '@inertiajs/vue3';
import { WysioEditor } from '@wysio/vue';
import 'wysio/style.css';

const props = defineProps({ post: Object });
const form = useForm({ body: props.post.body ?? '' });
</script>

<template>
  <form @submit.prevent="form.post(route('posts.store'))">
    <WysioEditor v-model="form.body" format="html" placeholder="Start writing…" />
    <p v-if="form.errors.body">{{ form.errors.body }}</p>
    <button :disabled="form.processing">Save</button>
  </form>
</template>

On the server, use the same validation rule and cast as in the Blade quick start. Don't use the Blade component or publish wysio-assets: Vite bundles the editor, and this package adds no JavaScript or CSS of its own.

If you configure custom palettes, share them with the editor so it offers the same colours the sanitiser allows. For example, in HandleInertiaRequests:

use Wysio\Laravel\Facades\Wysio;

public function share(Request $request): array
{
    return [
        ...parent::share($request),
        'wysio' => [
            'textColors' => Wysio::palette('text')->toEditorOption(),
            'highlightColors' => Wysio::palette('highlight')->toEditorOption(),
        ],
    ];
}
<WysioEditor v-model="form.body" :options="$page.props.wysio" />

Blade component

<x-wysio-editor
    name="body"
    :value="$post->body"
    format="html"
    toolbar="default"
    placeholder="Start writing…"
    locale="nl"
    label="Body"
    :options="['sticky' => true]"
    class="mt-4"
/>
Attribute Default
name required Form field name. Array names such as items[0][body] work.
value null Initial content: a string, Htmlable, or an array / Arrayable for format="json". Old input (old()) takes precedence.
format html What the form submits: html, json (ProseMirror JSON) or markdown.
toolbar defaults.toolbar A preset from config('wysio.toolbars'), an array of item names, or false.
placeholder defaults.placeholder Shown while the editor is empty.
locale app locale UI language (en, nl).
label Accessible name of the editing area.
options [] Any other editor option that JSON can hold, such as sticky or toolbarMode.

Other attributes (class, id, …) go on the wrapper div.

  • The component renders a <textarea> that the editor replaces. Its value stays escaped until the editor has parsed it through its schema, and without JavaScript the textarea still works.
  • When the field has validation errors, the wrapper gets the wysio-blade-invalid class and the textarea gets aria-invalid.
  • format="markdown" loads the larger editor bundle that includes Markdown support, only on pages that need it.

Assets

The component pushes a stylesheet and a deferred script to the styles and scripts stacks, once per page and only when an editor is rendered. A small inline script then creates the editors. It carries Laravel's CSP nonce when you use one (Vite::useCspNonce()).

Layouts without stacks can use @wysioAssets instead. Place it after the editors, for example at the end of the body, so it knows whether Markdown is needed. Or force the full bundle with @wysioAssets('full').

Sanitising

use Wysio\Laravel\Facades\Wysio;

$html = Wysio::sanitize($request->input('body'));

The allow-list (sanitizer in the config) mirrors the editor's schema:

  • Elements outside the allow-list are removed with their content. Harmless wrappers that other editors produce (div, font, b, section, …) are removed but keep their content, so older TinyMCE content survives.
  • class keeps only the editor's own classes: alignment, image alignment, and the colours of the configured palettes.
  • Links are limited to the configured schemes, and images to http, https and relative URLs (optionally only from given hosts). Base64 images need allow_data_images. An image without a valid src is removed.
  • Event handlers, style, id and other attributes are removed.

For one-off changes, use the fluent API. Each call returns a copy, so the configured sanitiser stays unchanged:

Wysio::sanitizer()
    ->allowElement('sup')
    ->allowClasses('p', ['lead'])
    ->allowImageHosts(['cdn.example.com'])
    ->sanitize($html);

Also available: removeElement(), allowLinkSchemes(), allowDataImages() and maxInputLength(). Elements that can run scripts or embed documents (script, style, iframe, svg, …) can't be allowed.

Validation

use Wysio\Laravel\Rules\WysioContent;

$request->validate([
    'body' => [new WysioContent(format: 'html', maxLength: 5000, required: true)],
    // or as a string rule, parameters in any order:
    'intro' => 'wysio:html,500',
    'data' => 'wysio:json,required',
]);
  • format: html (default), json (a ProseMirror document, as an array or a JSON string) or markdown.
  • required: there must be text or an image after sanitising. <p></p>, whitespace or a lone <script> don't count. Use this instead of Laravel's required, which accepts <p></p>.
  • maxLength: plain-text characters, not counting line breaks.
  • Input larger than sanitizer.max_input_length (default 1 MB) fails, instead of being cut off by the sanitiser.

Validation doesn't change the value: sanitise it before storing it, or use the cast.

Messages reuse Laravel's validation.required and validation.max.string. The package's own messages are in English and Dutch. To change them, publish them with php artisan vendor:publish --tag=wysio-lang.

Eloquent cast

use Wysio\Laravel\Casts\WysioHtml;

protected function casts(): array
{
    return ['body' => WysioHtml::class];
}

The cast sanitises when you set the attribute ($post->body = …, fill(), create()), so only sanitised HTML reaches the database. Reading returns the stored value unchanged.

Content stored before you added the cast, for example from TinyMCE, is only sanitised when it's saved again. Re-save those records once, or render them with {!! Wysio::sanitize($post->body) !!}.

Rendering

Wysio::renderJson($json);         // ProseMirror JSON (array or string) to sanitised HTML
Wysio::renderMarkdown($markdown); // CommonMark + GFM tables and strikethrough, sanitised
Wysio::toText($html);             // plain text, one line per block: excerpts, search indexes
  • renderJson() produces the same HTML as the editor's getHTML(), so you can store the editor's JSON (format="json") and render it on the server. Unknown nodes are dropped. An invalid document throws an InvalidArgumentException, so validate it with WysioContent first.
  • renderMarkdown() escapes raw HTML in the Markdown, as the editor does.

Colour palettes

The text colour and highlight pickers only offer the colours in the config:

'palettes' => [
    'text' => [
        'brand' => '#0a7',
        'ink' => ['value' => '#343a40', 'label' => 'Ink'],
    ],
    'highlight' => [
        'sun' => '#fff3bf',
    ],
],

This one list drives the editor (the Blade component passes custom palettes on), the sanitiser (only these wysio-color-* and wysio-highlight-* classes survive) and the CSS. The defaults are the editor's own 8 text and 8 highlight colours. Names may contain a-z, digits and -. Values must be hex colours.

The editor's stylesheet only has the default colours. Pages that display content with custom colours need their CSS:

Wysio::paletteCss(); // .wysio-color-brand { color: #00aa77; } …

Add the output to your stylesheet, or print it in a <style> block in your layout.

Configuration

config/wysio.php (publish with --tag=wysio-config):

Key Default
assets local (env WYSIO_ASSETS) Where the Blade component loads the editor from. local: public/vendor/wysio after publishing. cdn: jsDelivr, with Subresource Integrity.
asset_path vendor/wysio Public path of the published assets.
version null (env WYSIO_VERSION) Editor version for the CDN. null uses the version bundled with this package, which also enables SRI.
cdn_url jsDelivr URL template with {version} and {file}.
stacks styles, scripts Blade stacks the component pushes to.
defaults toolbar default, format html Default editor options for the component. A null locale follows the app locale.
toolbars default, minimal, full Toolbar presets. default is null, meaning the editor's built-in toolbar.
palettes the editor's colours Text and highlight palettes. See Colour palettes.
sanitizer.elements the editor schema Allowed elements and their attributes.
sanitizer.link_schemes http, https, mailto, tel Allowed URL schemes in links. Relative links are always allowed.
sanitizer.image_hosts [] (any) Hosts allowed for image sources. Relative URLs are always allowed.
sanitizer.allow_data_images false Allow base64 raster images (data:image/png;base64,…).
sanitizer.max_input_length 1000000 Largest input in bytes (-1: no limit). The validation rule rejects larger input.

Security

  • Never trust what the browser sends. The editor's own output is clean, but a request can contain anything. Sanitise every value from the editor on the server before you store or render it: use Wysio::sanitize(), the WysioHtml cast, renderJson() or renderMarkdown().
  • Print only sanitised HTML with {!! !!}. Values stored before you used the sanitiser are not sanitised: re-save them, or wrap them in Wysio::sanitize().
  • Strict CSP: formatting is stored as wysio-* classes, never as inline styles. The component's inline script carries Laravel's CSP nonce. In CDN mode, the script and stylesheet are loaded with Subresource Integrity.
  • Sanitiser updates: the sanitiser builds on symfony/html-sanitizer. Keep it updated with composer update; composer audit reports known vulnerabilities.

Please report security issues privately via GitHub (Security → Report a vulnerability on this repository), not in public issues.

Development

PHP and Composer run in Docker via bin/dev:

bin/dev composer install
bin/dev test              # Pest
bin/dev lint              # Pint (check); bin/dev format to fix
PHP=8.4 bin/dev test      # another PHP version

Updating the bundled editor

resources/dist/ holds the editor's browser build, copied from the wysio npm package. After an editor release:

bin/sync-assets.sh 0.2.0-alpha.2   # or a dist-tag: bin/sync-assets.sh alpha
bin/dev test

Then commit the result.

  • The script needs Node and npm on the host; npm verifies the download.
  • It writes resources/dist/manifest.json with the exact version and a sha384 hash per file. The package reads the bundled version from that manifest, and the hashes double as SRI values for the jsDelivr copies, which are byte-identical.
  • When the editor's schema changes, also regenerate tests/fixtures/core-json-html.json (the editor's getJSON() and getHTML() for an HTML corpus). The JSON renderer is tested against it.

Changelog

See CHANGELOG.md.

License

MIT © 2026 Rens Verhaeven. See LICENSE.