nadar/quill-delta-parser
Parse Quill editor Delta JSON (ops) into safe, sanitized HTML in PHP. Simple Lexer API to render output from arrays or JSON strings, with a consistent parsing mechanism and hooks to extend/customize elements and attributes.
Installation
composer require nadar/quill-delta-parser:^3.7.0
Add to composer.json if using a monorepo or custom package management.
Basic Usage (Updated for Font Size)
use Nadar\QuillDeltaParser\Parser;
$delta = [
['insert', 'Hello '],
['insert', 'World', {'bold': true, 'font': {'size': '24px'}}],
];
$parser = new Parser();
$html = $parser->parse($delta);
// Output: <p>Hello <span style="font-size: 24px;"><strong>World</strong></span></p>
First Use Case Parse raw Quill Delta JSON (from API or frontend) into sanitized HTML for:
$model->content = $html){!! $html !!})Frontend ↔ Backend Sync
// Frontend (Quill.js with font-size support)
const delta = quill.getContents();
// Send to Laravel via API
// Backend (Laravel Controller)
$delta = json_decode(request('delta'), true);
$html = (new Parser())->parse($delta);
Database Storage
// Migration
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->text('content_html'); // Store parsed HTML
$table->json('content_delta')->nullable(); // Optional: Store raw delta
});
Blade Rendering
<div class="quill-editor">
{!! $post->content_html !!}
</div>
Font Size Customization
$parser = new Parser();
$parser->setFontSizeMap([
'small' => '12px',
'medium' => '16px',
'large' => '24px',
]);
Custom Elements with Font Support
$parser = new Parser();
$parser->addCustomElement('mention', function ($attrs, $text) {
$fontSize = $attrs['font']['size'] ?? '16px';
return '<span class="mention" style="font-size: ' . $fontSize . '" data-id="' . $attrs['id'] . '">@' . $text . '</span>';
});
Delta Validation
use Nadar\QuillDeltaParser\Validator;
$validator = new Validator();
if ($validator->validate($delta)) {
$html = $parser->parse($delta);
}
Batch Processing
$deltas = collect($request->deltas);
$htmls = $deltas->map(fn($delta) => $parser->parse($delta));
XSS Risks
Validator or htmlspecialchars).!! in Blade if user-generated content isn’t trusted.Nested Deltas
Font Size Attribute Handling
font.size attribute now maps to inline style="font-size: ...".setFontSizeMap for custom size handling.Log Raw Deltas
\Log::debug('Raw Delta:', $delta);
['insert', 'text', {'bold': true, 'font': {'size': '24px'}}]).Inspect HTML Output
$html = $parser->parse($delta);
\Log::debug('Generated HTML:', $html);
Custom Element Quirks
font.size vs. font-size).Override Default Tags
$parser->setTagMap([
'bold' => 'b', // Default: 'strong'
'italic' => 'i',
]);
Font Size Customization
$parser->setFontSizeMap([
'small' => '12px',
'medium' => '16px',
'large' => '24px',
]);
Pre/Post-Processing
$parser->setPreProcess(function ($delta) {
// Modify delta before parsing (e.g., sanitize)
return $delta;
});
$parser->setPostProcess(function ($html) {
// Modify HTML after parsing (e.g., wrap in div)
return '<div class="content">' . $html . '</div>';
});
Performance
Parser instance:
$parser = new Parser(); // Initialize once
foreach ($deltas as $delta) {
$html = $parser->parse($delta);
}
How can I help you explore Laravel packages today?