litlife/json-to-bbcode

Json-to-bbcode is a library written in PHP for converting ProseMirror json to BBCode

1.2.0 2021-10-04 17:05 UTC

This package is auto-updated.

Last update: 2024-06-04 23:11:45 UTC


README

Json-to-bbcode is a library written in PHP for converting ProseMirror json to BBCode

Installation

Use the package manager composer to install json-to-bbcode.

composer require litlife/json-to-bbcode

Usage

In this example, you can see how to convert json to a string of bb code

use Litlife\JsonToBBCode\Renderer;

$jsonString = <<<EOT
{
  "type": "doc",
  "content": [
    {
      "type": "paragraph",
      "content": [
        {
          "type": "text",
          "text": "Example text using strong",
          "marks": [
            {
              "type": "bold"
            }
          ]
        }
      ]
    }
  ]
}
EOT;

$jsonArray = json_decode($jsonString, true);

$bbCodeString = (new Renderer())->render($jsonArray);

echo ($bbCodeString);

Output:

[b]Example text using strong[/b]

Adding a mark

use Litlife\JsonToBBCode\Marks\Italic;

$bbCodeString = (new Renderer())->addMark(Italic::class)->render($jsonArray);

Adding a node

use Litlife\JsonToBBCode\Nodes\Blockquote;

$bbCodeString = (new Renderer())->addNode(Blockquote::class)->render($jsonArray);

Testing

composer test

License

MIT