How to Parse Quill Delta to HTML Using PHP

Dec 14, 2023 | Programming

Quill is a modern WYSIWYG editor designed to maintain flexibility and customizability. If you’re looking to convert Quill’s delta format into HTML, you’ll find a handy PHP library specifically for that purpose. This guide will walk you through the installation, usage, and troubleshooting aspects to make your parsing endeavors smooth and efficient.

What is Quill?

Quill is a free, open-source WYSIWYG editor that allows users to create rich text in a customizable manner. It is built with a modular architecture, providing an expressive API that caters to various needs. By understanding how Quill works, you’re set to unlock powerful functionalities in your applications.

Installation

The Quill Delta to HTML parser is available exclusively through Composer. To install it, run the following command in your terminal:

sh
composer require nadar/quill-delta-parser

Usage

Once the library is installed, you will need to include the Composer autoload file in your application:

php
require __DIR__ . '/vendor/autoload.php';

Next, you can create a lexer object with your Quill JSON delta code, which can be either a PHP array or a JSON string. Here’s how:

php
$lexer = new \nadar\quill\Lexer($json);
echo $lexer->render();

In this scenario, $json represents the ops JSON array from Quill. For example:

json
{
    ops: [
        { insert: "Hello", attributes: { header: 1 } },
        { insert: "\n" },
        { insert: "This is the PHP Quill ", attributes: { bold: true } },
        { insert: "parser" },
        { insert: "!\n" }
    ]
}

Understanding the Rendering Process: An Analogy

Think of the Quill Delta as an unassembled piece of furniture, like a chair. Each part – the seat, the legs, and the back – corresponds to elements in the delta structure (insert, attributes). The parser is like the instruction manual that guides you to assemble all these parts into a cohesive chair (HTML). It tells you which parts go where and verifies that everything is tight and secure (sanitizes the output). As such, the final product—your chair—is stable and functional, just like the clean and secure HTML output generated from your Quill delta.

Extending the Parser

If you want to extend the parser by adding your own listener, decide whether it should handle inline or block elements. For instance, using a mention plugin to process mentions from the delta:

php
class Mention extends InlineListener {
    public function process(Line $line) {
        $mention = $line->insertJsonKey('mention');
        if ($mention) {
            $this->updateInput($line, '' . $mention['value'] . '');
        }
    }
}
$lexer = new \nadar\quill\Lexer($json);
$lexer->registerListener(new Mention());
echo $lexer->render();

Overriding Built-in Listeners

If you find that the HTML output from certain listeners (like images or videos) doesn’t meet your use case, you can override them:

php
$image = new Image();
$image->wrapper = '';
$lexer = new \nadar\quill\Lexer($json);
$lexer->registerListener($image);
echo $lexer->render();

Debugging

Debugging is essential for understanding how data is parsed. The library includes a debugger class that helps in this regard:

php
$lexer = new \nadar\quill\Lexer($json);
$lexer->render();
$debug = new Debug($lexer);
echo $debug->debugPrint();

You can also utilize a built-in Docker Compose file to directly interact with the Quill editor, allowing you to write content and display the rendered output. Simply execute:

sh
docker-compose up

Then visit http://localhost:5555 in your browser.

Troubleshooting

If you encounter issues while parsing your Quill Deltas, consider the following troubleshooting steps:

  • Ensure that your JSON is correctly formatted. Improperly structured JSON will lead to errors.
  • Verify that all required listeners are registered before rendering. Missing listeners can cause parts of your delta to render incorrectly or not at all.
  • Check that dependencies are properly installed via Composer, especially if you are running this in a new environment.
  • Utilize the debugging tools provided in the library to gain insights into how your data is being processed.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

The Quill Delta to HTML parser offers a powerful and flexible way to handle text rendering in web applications. Combining the modularity of Quill with PHP’s capabilities provides a robust framework for developers. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox