Skip to content

Commit b8bfe59

Browse files
committed
added codeblock, hrs and tables
1 parent 121f97d commit b8bfe59

File tree

6 files changed

+116
-6
lines changed

6 files changed

+116
-6
lines changed

.php-cs-fixer.dist.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
$finder = PhpCsFixer\Finder::create()
4-
->exclude('tests')
54
->in(__DIR__)
65
;
76

src/Parser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public function getDefaultNodeRenderers(): array
4545

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

48+
Types::codeBlock->name => static fn (Node $node) => '<pre><code>' . $node->renderContent() . '</code></pre>',
49+
50+
Types::horizontalRule->name => static fn () => '<hr />',
51+
52+
Types::table->name => static fn (Node $node) => "<table>{$node->renderContent()}</table>",
53+
54+
Types::tableRow->name => static fn (Node $node) => "<tr>{$node->renderContent()}</tr>",
55+
56+
Types::tableCell->name => static fn (Node $node) => "<td>{$node->renderContent()}</td>",
57+
4858
Types::text->name => static function (Node $node) {
4959
$text = $node->getText();
5060
foreach ($node->getMarks() as $mark) {

src/Types.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ enum Types
2121
case orderedList; // Represents an ordered list type.
2222
case listItem; // Represents a list item type.
2323
case text; // Represents a text type.
24+
case codeBlock;
25+
case horizontalRule;
26+
case tableRow;
27+
case tableCell;
28+
case table;
2429
}

tests/AdditionalsTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nadar\ProseMirror\Tests;
6+
7+
use Nadar\ProseMirror\Parser;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class AdditionalsTest extends TestCase
11+
{
12+
public function testCompile()
13+
{
14+
$path = __DIR__ . '/options.json';
15+
$buff = file_get_contents($path);
16+
$json = json_decode($buff, true);
17+
18+
$wysiwyg = new Parser();
19+
$result = $wysiwyg->toHtml($json);
20+
$html = '<pre><code>function hello() {
21+
console.log(&apos;Hello, World!&apos;);
22+
}</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>';
23+
$this->assertSame($html, $result);
24+
}
25+
}

tests/ParserTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Nadar\ProseMirror\Tests;
46

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

6062
// test without xss filter:
61-
63+
6264
$xss = <<<EOT
6365
{
6466
"type": "text",
@@ -90,14 +92,14 @@ public function testCustomNodeRenderer()
9092
EOT;
9193

9294
$wysiwyg = new Parser();
93-
$wysiwyg->replaceNode(Types::paragraph, function(Node $node) {
95+
$wysiwyg->replaceNode(Types::paragraph, function (Node $node) {
9496
return '<p>Custom Paragraph</p>';
9597
});
9698

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

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

101103
$this->assertSame('<div>BarFoo: Hello World</div>', $result);
102104
}
103-
}
105+
}

tests/options.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"type": "doc",
3+
"content": [
4+
{
5+
"type": "codeBlock",
6+
"content": [
7+
{
8+
"type": "text",
9+
"text": "function hello() {\n console.log('Hello, World!');\n}"
10+
}
11+
]
12+
},
13+
{
14+
"type": "horizontalRule"
15+
},
16+
{
17+
"type": "table",
18+
"content": [
19+
{
20+
"type": "tableRow",
21+
"content": [
22+
{
23+
"type": "tableCell",
24+
"content": [
25+
{
26+
"type": "text",
27+
"text": "Cell 1"
28+
}
29+
]
30+
},
31+
{
32+
"type": "tableCell",
33+
"content": [
34+
{
35+
"type": "text",
36+
"text": "Cell 2"
37+
}
38+
]
39+
}
40+
]
41+
},
42+
{
43+
"type": "tableRow",
44+
"content": [
45+
{
46+
"type": "tableCell",
47+
"content": [
48+
{
49+
"type": "text",
50+
"text": "Cell 3"
51+
}
52+
]
53+
},
54+
{
55+
"type": "tableCell",
56+
"content": [
57+
{
58+
"type": "text",
59+
"text": "Cell 4"
60+
}
61+
]
62+
}
63+
]
64+
}
65+
]
66+
}
67+
]
68+
}
69+

0 commit comments

Comments
 (0)