New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

freshleafmedia/tiptap-parser

1,043 2
Install
composer require freshleafmedia/tiptap-parser:dev-master
License:MIT
Last Updated:Jun 19, 2024
Links: GitHub  ·  Packagist
Maintainer: freshleafmedia

Tiptap JSON Parser

Latest Version on Packagist Total Downloads License


This package simply converts the JSON output from the Tiptap editor to HTML. It differs from other packages by allowing you to easily customise and extend the HTML output.


Installation

composer require freshleafmedia/tiptap-parser

Basic Usage

use FreshleafMedia\TiptapParser\TiptapContent;

$tiptapArray = [
    'type' => 'paragraph',
    'content' => [
        [
            'type' => 'text',
            'text' => 'Hello world',
        ],
    ],
];

TiptapContent::make($tiptapArray)->toHtml(); // <p>Hello world</p>

Customising A Node

use FreshleafMedia\TiptapParser\Nodes\Paragraph;

readonly class CustomParagraph extends Paragraph
{
    public function render(): string
    {
        return <<<HTML
            <p class="paragraph">
                {$this->renderInnerHtml()}
            </p>
            HTML;
    }
}

$html = Parser::fromArray($tiptapArray)
    ->registerNode('paragraph', CustomParagraph::class)
    ->toHtml(); // <p class="paragraph">Hello world</p>

Accessing Custom Attributes

Nodes are instantiated via the fromArray method, the method is passed all the data from the original array.

For example given this array:

[
    'type' => 'paragraph',
    'attrs' => [
        'lang' => 'en',
    ]
]

We can easily add the lang attribute to the p element like this:

use FreshleafMedia\TiptapParser\Nodes\Paragraph;

readonly class LocalisedParagraph extends Paragraph
{
    public function __construct(
        public string $language,
        public array $children = [],
    )
    {
    }

    public function render(): string
    {
        return <<<HTML
            <p lang="{$this->language}">
                {$this->renderInnerHtml()}
            </p>
            HTML;
    }

    public static function fromArray(array $array): self
    {
        return new self(
            $array['attrs']['lang'] ?? 'en',
            $array['children'] ?? [],
        );
    }
}

Plain Text

Plain text can be extracted available via the toText method. This is useful for things like populating a search index.

use FreshleafMedia\TiptapParser\TiptapContent;

$tiptapArray = [
    'type' => 'paragraph',
    'content' => [
        [
            'type' => 'text',
            'text' => 'Hello world',
            'marks' => [
                ['type' => 'bold'],
            ],
        ],
    ],
];

TiptapContent::make($tiptapArray)->toHtml(); // <p><strong>Hello world</strong></p>
TiptapContent::make($tiptapArray)->toText(); // Hello world

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

655,942,320 9,702
laravel/framework

The Laravel Framework.

610,364,751 34,988
laravel/tinker

Powerful REPL for the Laravel framework.

518,004,183 7,444
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

435,097,283 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

412,228,818 4,664