PHPackages                             ueberdosis/tiptap-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ueberdosis/tiptap-php

ActiveLibrary

ueberdosis/tiptap-php
=====================

A PHP package to work with Tiptap output

2.1.0(4mo ago)2669.2M↑10.9%43[15 issues](https://github.com/ueberdosis/tiptap-php/issues)[11 PRs](https://github.com/ueberdosis/tiptap-php/pulls)20MITPHPPHP ^8.0CI passing

Since Jan 27Pushed 4mo ago3 watchersCompare

[ Source](https://github.com/ueberdosis/tiptap-php)[ Packagist](https://packagist.org/packages/ueberdosis/tiptap-php)[ Docs](https://github.com/ueberdosis/tiptap-php)[ Fund](https://tiptap.dev/pricing)[ GitHub Sponsors](https://github.com/ueberdosis)[ RSS](/packages/ueberdosis-tiptap-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (6)Versions (8)Used By (20)

Tiptap for PHP
==============

[](#tiptap-for-php)

[![Latest Version on Packagist](https://camo.githubusercontent.com/71d9b374f155a04d9c20c1e3e703eff364f8a46ca706b59803b79bd629862c4f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7565626572646f7369732f7469707461702d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ueberdosis/tiptap-php)[![GitHub Tests Action Status](https://github.com/ueberdosis/tiptap-php/actions/workflows/run-tests.yml/badge.svg)](https://github.com/ueberdosis/tiptap-php/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/d30f3d83c8239c3e47e7997628a3f480a6009debd3ef0bc7af02ca6c31aea6f2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7565626572646f7369732f7469707461702d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ueberdosis/tiptap-php)[![License](https://camo.githubusercontent.com/8ac105a6750b78fe15f37dd3c5278bc8bc3a4cad0b9d189de8bae9ddab8da442/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7565626572646f7369732f7469707461702d7068703f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ueberdosis/tiptap-php)[![Chat](https://camo.githubusercontent.com/e2b1802197c23b0c23cdefd3813261883b6a0e50bf0f12f7bd29c1351c627474/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636861742d6f6e253230646973636f72642d3732383964612e7376673f73616e6974697a653d74727565)](https://discord.gg/WtJ49jGshW)[![Sponsor](https://camo.githubusercontent.com/ee73cad5907779cdf16e865239ea623ca0bd44ccd5ad44fa7bfe7a5ac3ced298/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d53706f6e736f72266d6573736167653d254532253944254134266c6f676f3d476974487562)](https://github.com/sponsors/ueberdosis)

A PHP package to work with [Tiptap](https://tiptap.dev/) content. You can transform Tiptap-compatible JSON to HTML, and the other way around, sanitize your content, or just modify it.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require ueberdosis/tiptap-php
```

Usage
-----

[](#usage)

The PHP package mimics large parts of the JavaScript package. If you know your way around Tiptap, the PHP syntax will feel familiar to you.

### Convert Tiptap HTML to JSON

[](#convert-tiptap-html-to-json)

Let’s start by converting a HTML snippet to a PHP array with a Tiptap-compatible structure:

```
(new \Tiptap\Editor)
    ->setContent('Example Text')
    ->getDocument();

// Returns:
// ['type' => 'doc', 'content' => …]
```

You can get a JSON string in PHP, too.

```
(new \Tiptap\Editor)
    ->setContent('Example Text')
    ->getJSON();

// Returns:
// {"type": "doc", "content": …}
```

### Convert Tiptap JSON to HTML

[](#convert-tiptap-json-to-html)

The other way works aswell. Just pass a JSON string or an PHP array to generate the HTML.

```
(new \Tiptap\Editor)
    ->setContent([
        'type' => 'doc',
        'content' => [
            [
                'type' => 'paragraph',
                'content' => [
                    [
                        'type' => 'text',
                        'text' => 'Example Text',
                    ],
                ]
            ]
        ],
    ])
    ->getHTML();

// Returns:
// Example Text
```

This doesn’t fully adhere to the ProseMirror schema. Some things are supported too, for example aren’t marks allowed in a `CodeBlock`.

If you need better schema support, create an issue with the feature you’re missing.

### Syntax highlighting for code blocks with [highlight.php](https://github.com/scrivo/highlight.php)

[](#syntax-highlighting-for-code-blocks-with-highlightphp)

The default `CodeBlock` extension doesn’t add syntax highlighting to your code blocks. However, if you want to add syntax highlighting to your code blocks, there’s a special `CodeBlockHighlight` extension.

Swapping our the default one works like that:

```
(new \Tiptap\Editor([
    'extensions' => [
        new \Tiptap\Extensions\StarterKit([
            'codeBlock' => false,
        ]),
        new \Tiptap\Nodes\CodeBlockHighlight(),
    ],
]))
->setContent('&lt;?php phpinfo()')
->getHTML();

// Returns:
// &lt;?php phpinfo()
```

This is still unstyled. You need to [load a CSS file](https://highlightjs.org/download/) to add colors to the output, for example like that:

```

```

Boom, syntax highlighting! By the way, this is powered by the amazing [scrivo/highlight.php](https://github.com/scrivo/highlight.php).

### Syntax highlighting for code blocks with [Shiki](https://github.com/shikijs/shiki) (Requires Node.js)

[](#syntax-highlighting-for-code-blocks-with-shiki-requires-nodejs)

There is an alternate syntax highlighter that utilizes [Shiki](https://github.com/shikijs/shiki). Shiki is a beautiful syntax highlighter powered by the same language engine that many code editors use. The major differences from the `CodeBlockHighlight` extensions are:

1. you must install the `shiki` npm package.
2. Shiki code highlighting works by injecting inline styles so pulling in a external css file is not required.
3. you can use most VS Code themes to highlight your code.

To use the Shiki extension, first install the npm package

```
npm install shiki
```

Then follow the example below:

```
(new \Tiptap\Editor([
    'extensions' => [
        new \Tiptap\Extensions\StarterKit([
            'codeBlock' => false,
        ]),
        new \Tiptap\Nodes\CodeBlockShiki(),
    ],
]))
->setContent('&lt;?php phpinfo()')
->getHTML();
```

To configure the theme or default language for code blocks pass additonal configuration into the constructor as show below:

```
(new \Tiptap\Editor([
    'extensions' => [
        new \Tiptap\Extensions\StarterKit([
            'codeBlock' => false,
        ]),
        new \Tiptap\Nodes\CodeBlockShiki([
            'theme' => 'github-dark', // default: nord, see https://github.com/shikijs/shiki/blob/main/docs/themes.md
            'defaultLanguage' => 'php', // default: html, see https://github.com/shikijs/shiki/blob/main/docs/languages.md
            'guessLanguage' => true, // default: true, if the language isn’t passed, it tries to guess the language with highlight.php
        ]),
    ],
]))
->setContent('&lt;?php phpinfo()')
->getHTML();
```

Under the hood the Shiki extension utilizes [Shiki PHP by Spatie](https://github.com/spatie/shiki-php), so please see the documentation for additional details and considerations.

### Convert content to plain text

[](#convert-content-to-plain-text)

Content can also be transformed to plain text, for example to put it into a search index.

```
(new \Tiptap\Editor)
    ->setContent('HeadingParagraph')
    ->getText();

// Returns:
// "Heading
//
// Paragraph"
```

What’s coming between blocks can be configured, too.

```
(new \Tiptap\Editor)
    ->setContent('HeadingParagraph')
    ->getText([
        'blockSeparator' => "\n",
    ]);

// Returns:
// "Heading
// Paragraph"
```

### Sanitize content

[](#sanitize-content)

A great use case for the PHP package is to clean (or “sanitize”) the content. You can do that with the `sanitize()` method. Works with JSON strings, PHP arrays and HTML.

It’ll return the same format you’re using as the input format.

```
(new \Tiptap\Editor)
    ->sanitize('Example Textalert("HACKED!")');

// Returns:
// 'Example Text'
```

### Modifying the content

[](#modifying-the-content)

With the `descendants()` method you can loop through all nodes recursively as you are used to from the JavaScript package. But in PHP, you can even modify the node to update attributes and all that.

> Warning: You need to add `&` to the parameter. Thats keeping a reference to the original item and allows to modify the original one, instead of just a copy.

```
$editor->descendants(function (&$node) {
    if ($node->type !== 'heading') {
        return;
    }

    $node->attrs->level = 1;
});
```

### Configuration

[](#configuration)

Pass the configuration to the constructor of the editor. There’s not much to configure, but at least you can pass the initial content and load specific extensions.

```
new \Tiptap\Editor([
    'content' => 'Example Text',
    'extensions' => [
        new \Tiptap\Extensions\StarterKit,
    ],
])
```

The `StarterKit` is loaded by default. If you just want to use that, there’s no need to set it.

### Extensions

[](#extensions)

By default, the [`StarterKit`](https://tiptap.dev/api/extensions/starter-kit) is loaded, but you can pass a custom array of extensions aswell.

```
new \Tiptap\Editor([
    'extensions' => [
        new \Tiptap\Extensions\StarterKit,
        new \Tiptap\Marks\Link,
    ],
])
```

### Configure extensions

[](#configure-extensions)

Some extensions can be configured. Just pass an array to the constructor, that’s it. We’re aiming to support the same configuration as the JavaScript package.

```
new \Tiptap\Editor([
    'extensions' => [
        // …
        new \Tiptap\Nodes\Heading([
            'levels' => [1, 2, 3],
        ]),
    ],
])
```

You can pass custom HTML attributes through the configuration, too.

```
new \Tiptap\Editor([
    'extensions' => [
        // …
        new \Tiptap\Nodes\Heading([
            'HTMLAttributes' => [
                'class' => 'my-custom-class',
            ],
        ]),
    ],
])
```

For the `StarterKit`, it’s slightly different, but works as you are used to from the JavaScript package.

```
new \Tiptap\Editor([
    'extensions' => [
        new Tiptap\Extensions\StarterKit([
            'codeBlock' => false,
            'heading' => [
                'HTMLAttributes' => [
                    'class' => 'my-custom-class',
                ],
            ]
        ]),
    ],
])
```

### Extend existing extensions

[](#extend-existing-extensions)

If you need to change minor details of the supported extensions, you can just extend an extension.

```
