Skip to content

Commit 2956740

Browse files
fix: image-parser
1 parent e526e91 commit 2956740

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

Diff for: src/Parser/EditorjsParser.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ListParser;
1313
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\EmbedParser;
1414
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ImageParser;
15+
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\TableParser;
1516
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\HeaderParser;
1617
use SyntaxPhoenix\EJSParserBundle\Parser\EditorjsParserExtension;
1718
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\WarningParser;
1819
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\DelimiterParser;
1920
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ParagraphParser;
20-
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\TableParser;
21+
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\SimpleImageParser;
2122

2223
class EditorjsParser
2324
{
@@ -49,7 +50,8 @@ public function __construct(object $data)
4950
'raw' => new RawParser(),
5051
'list' => new ListParser(),
5152
'warning' => new WarningParser(),
52-
'simpleImage' => new ImageParser(),
53+
'simpleImage' => new SimpleImageParser(),
54+
'image' => new ImageParser(),
5355
'table' => new TableParser()
5456
];
5557
}
@@ -84,5 +86,4 @@ private function render(): void
8486
}
8587
}
8688
}
87-
8889
}

Diff for: src/Parser/Extension/ImageParser.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ public function parseBlock(HTML5 $html5, DOMDocument $document, object $block, s
1414
{
1515
$figure = $document->createElement('figure');
1616

17-
$figure->setAttribute('class', "{$this->prefix}-image");
18-
19-
$img = $document->createElement('img');
20-
21-
$imgAttrs = [];
17+
$imgAttrs = [
18+
"{$prefix}-image"
19+
];
2220

2321
if ($block->data->withBorder) {
2422
$imgAttrs[] = "{$prefix}-image-border";
@@ -30,8 +28,11 @@ public function parseBlock(HTML5 $html5, DOMDocument $document, object $block, s
3028
$imgAttrs[] = "{$prefix}-image-stretched";
3129
}
3230

33-
$img->setAttribute('src', $block->data->url);
34-
$img->setAttribute('class', implode(' ', $imgAttrs));
31+
$figure->setAttribute('class', implode(' ', $imgAttrs));
32+
33+
$img = $document->createElement('img');
34+
35+
$img->setAttribute('src', $block->data->file->url);
3536

3637
$figCaption = $document->createElement('figcaption');
3738

Diff for: src/Parser/Extension/SimpleImageParser.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace SyntaxPhoenix\EJSParserBundle\Parser\Extension;
4+
5+
use DOMElement;
6+
use DOMDocument;
7+
use Masterminds\HTML5;
8+
use SyntaxPhoenix\EJSParserBundle\Parser\EditorjsParserExtension;
9+
10+
class SimpleImageParser implements EditorjsParserExtension
11+
{
12+
13+
public function parseBlock(HTML5 $html5, DOMDocument $document, object $block, string $prefix): DOMElement
14+
{
15+
$figure = $document->createElement('figure');
16+
17+
$figure->setAttribute('class', "{$prefix}-simpleimage");
18+
19+
$img = $document->createElement('img');
20+
21+
$imgAttrs = [];
22+
23+
if ($block->data->withBorder) {
24+
$imgAttrs[] = "{$prefix}-simpleimage-border";
25+
}
26+
if ($block->data->withBackground) {
27+
$imgAttrs[] = "{$prefix}-simpleimage-background";
28+
}
29+
if ($block->data->stretched) {
30+
$imgAttrs[] = "{$prefix}-simpleimage-stretched";
31+
}
32+
33+
$img->setAttribute('src', $block->data->url);
34+
$img->setAttribute('class', implode(' ', $imgAttrs));
35+
36+
$figCaption = $document->createElement('figcaption');
37+
38+
$figCaption->appendChild($html5->loadHTMLFragment($block->data->caption));
39+
40+
$figure->appendChild($img);
41+
42+
$figure->appendChild($figCaption);
43+
44+
return $figure;
45+
}
46+
}

0 commit comments

Comments
 (0)