Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Dec 5, 2023
1 parent 75c4a8d commit eb7e5df
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ The **prosemirror-json-parser** is a versatile library designed to parse ProseMi

It functions seamlessly with both TipTap and ProseMirror, given that TipTap is built upon ProseMirror.

[![PHPUnit Tests](https://github.com/nadar/prosemirror-json-parser/actions/workflows/phpunit.yml/badge.svg)](https://github.com/nadar/prosemirror-json-parser/actions/workflows/phpunit.yml)
[![Latest Stable Version](https://poser.pugx.org/nadar/prosemirror-json-parser/v/stable)](https://packagist.org/packages/nadar/prosemirror-json-parser)
[![Total Downloads](https://poser.pugx.org/nadar/prosemirror-json-parser/downloads)](https://packagist.org/packages/nadar/prosemirror-json-parser)
[![License](https://poser.pugx.org/nadar/prosemirror-json-parser/license)](https://packagist.org/packages/nadar/prosemirror-json-parser)


**Key Features:**

+ Dependency-free: No additional libraries required for this parser.
Expand All @@ -21,7 +27,8 @@ composer require nadar/prosemirror-json-parser
Then add parser to your project and convert the input json code (which must be available as array, so you can use `json_decode($json, true)` to convert the json string into an array) into html:

```php
$html = (new Nadar\ProseMirror\Parser())->toHtml($json);
$html = (new Nadar\ProseMirror\Parser())
->toHtml($json);
```

## Extending & Customizing
Expand All @@ -31,19 +38,19 @@ Each node corresponds to a callable function within the parser, with the node's
For instance, suppose you want to adjust the rendering of the image node. In that case, you can achieve this by incorporating your own function into the parser using the `replaceNode()` method:

```php
$parser = new \Nadar\ProseMirror\Parser();
$parser->replaceNode(\Nadar\ProseMirror\Types::image, fn(\Nadar\ProseMirror\Node $node) => '<img src="' . $node->getAttr('src') . '" alt="' . $node->getAttr('alt') . '" class="this-is-my-class" />');
$html = $parser->toHtml($json);
$html = (new \Nadar\ProseMirror\Parser());
->replaceNode(\Nadar\ProseMirror\Types::image, fn(\Nadar\ProseMirror\Node $node) => '<img src="' . $node->getAttr('src') . '" class="this-is-my-class" />')
->toHtml($json);
```

> Check the Types class for all available node types.
Or if you have a custom node with a custom name you can add it to the parser using `addNode` method:

```php
$parser = new \Nadar\ProseMirror\Parser();
$parser->addNode('myCustomNode', fn(\Nadar\ProseMirror\Node $node) => '<div class="my-custom-node">...</div>');
$html = $parser->toHtml($json);
$html = (new \Nadar\ProseMirror\Parser())
->addNode('myCustomNode', fn(\Nadar\ProseMirror\Node $node) => '<div class="my-custom-node">...</div>')
->toHtml($json);
```

> The `addNode()` and `replaceNode()` methods are internal almost the same, except of that replaceNode should only be used for existing defualt nodes, which are listed in the Types enum class.

0 comments on commit eb7e5df

Please sign in to comment.