Skip to content

Commit

Permalink
added codeblock, hrs and tables
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Dec 5, 2023
1 parent 121f97d commit b8bfe59
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
1 change: 0 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

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

Expand Down
10 changes: 10 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public function getDefaultNodeRenderers(): array

Types::listItem->name => static fn (Node $node) => $node->renderContent(),

Types::codeBlock->name => static fn (Node $node) => '<pre><code>' . $node->renderContent() . '</code></pre>',

Types::horizontalRule->name => static fn () => '<hr />',

Types::table->name => static fn (Node $node) => "<table>{$node->renderContent()}</table>",

Types::tableRow->name => static fn (Node $node) => "<tr>{$node->renderContent()}</tr>",

Types::tableCell->name => static fn (Node $node) => "<td>{$node->renderContent()}</td>",

Types::text->name => static function (Node $node) {
$text = $node->getText();
foreach ($node->getMarks() as $mark) {
Expand Down
5 changes: 5 additions & 0 deletions src/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ enum Types
case orderedList; // Represents an ordered list type.
case listItem; // Represents a list item type.
case text; // Represents a text type.
case codeBlock;
case horizontalRule;
case tableRow;
case tableCell;
case table;
}
25 changes: 25 additions & 0 deletions tests/AdditionalsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Nadar\ProseMirror\Tests;

use Nadar\ProseMirror\Parser;
use PHPUnit\Framework\TestCase;

class AdditionalsTest extends TestCase
{
public function testCompile()
{
$path = __DIR__ . '/options.json';
$buff = file_get_contents($path);
$json = json_decode($buff, true);

$wysiwyg = new Parser();
$result = $wysiwyg->toHtml($json);
$html = '<pre><code>function hello() {
console.log(&apos;Hello, World!&apos;);
}</code></pre><hr /><table><tr><td>Cell 1</td><td>Cell 2</td></tr><tr><td>Cell 3</td><td>Cell 4</td></tr></table>';
$this->assertSame($html, $result);
}
}
12 changes: 7 additions & 5 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Nadar\ProseMirror\Tests;

Expand Down Expand Up @@ -58,7 +60,7 @@ public function testXss()
$this->assertSame('<p>&lt;script&gt;alert(&apos;xss&apos;);&lt;/script&gt;</p>', $result);

// test without xss filter:

$xss = <<<EOT
{
"type": "text",
Expand Down Expand Up @@ -90,14 +92,14 @@ public function testCustomNodeRenderer()
EOT;

$wysiwyg = new Parser();
$wysiwyg->replaceNode(Types::paragraph, function(Node $node) {
$wysiwyg->replaceNode(Types::paragraph, function (Node $node) {
return '<p>Custom Paragraph</p>';
});

$wysiwyg->addNode('barfoo', fn(Node $node) => '<div>BarFoo: '.$node->renderContent().'</div>');
$wysiwyg->addNode('barfoo', fn (Node $node) => '<div>BarFoo: '.$node->renderContent().'</div>');

$result = $wysiwyg->toHtml(json_decode($json, true));

$this->assertSame('<div>BarFoo: Hello World</div>', $result);
}
}
}
69 changes: 69 additions & 0 deletions tests/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"type": "doc",
"content": [
{
"type": "codeBlock",
"content": [
{
"type": "text",
"text": "function hello() {\n console.log('Hello, World!');\n}"
}
]
},
{
"type": "horizontalRule"
},
{
"type": "table",
"content": [
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"content": [
{
"type": "text",
"text": "Cell 1"
}
]
},
{
"type": "tableCell",
"content": [
{
"type": "text",
"text": "Cell 2"
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"content": [
{
"type": "text",
"text": "Cell 3"
}
]
},
{
"type": "tableCell",
"content": [
{
"type": "text",
"text": "Cell 4"
}
]
}
]
}
]
}
]
}

0 comments on commit b8bfe59

Please sign in to comment.