Skip to content

Commit

Permalink
rename node handler, csfixer
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Nov 26, 2023
1 parent 87e6253 commit 919316a
Show file tree
Hide file tree
Showing 11 changed files with 2,077 additions and 110 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor/
.php-cs-fixer.cache
16 changes: 16 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('tests')
->in(__DIR__)
;

$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
])
->setFinder($finder)
;
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ It functions seamlessly with both TipTap and ProseMirror, given that TipTap is b

## Installation & Usage

The library can be installed via Composer:

```bash
composer require ...
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
$parser = new Nadar\ProseMirror\Parser();
$html = $parser->toHtml($json);
$html = (new Nadar\ProseMirror\Parser())->toHtml($json);
```

## Extending & Customizing
Expand All @@ -28,8 +31,12 @@ 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:

```php
$parser = new \Nadar\ProseMirrorParser([
'image' => fn(\Nadar\ProseMirror\Node $node) => '<img src="' . $node->getAttr('src') . '" alt="' . $node->getAttr('alt') . '" class="this-is-my-class" />',
]);
$parser = new \Nadar\ProseMirrorParser();
$parser->overwriteNode(\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);
```
```

Or if you have a custom node which a custom name you can add it to the parser:

```php
$parser = new \Nadar\ProseMirrorParser();
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
}
],
"require-dev": {
"phpunit/phpunit": "^10"
"phpunit/phpunit": "^10",
"friendsofphp/php-cs-fixer": "^3.2",
"phpstan/phpstan": "^1.7",
"rector/rector": "^0.14.2"
},
"scripts": {
"phpstan": "vendor/bin/phpstan -v",
"phpcsfixer": "vendor/bin/php-cs-fixer fix",
"rector": "vendor/bin/rector"
}
}
Loading

0 comments on commit 919316a

Please sign in to comment.