Skip to content

Commit

Permalink
fix: image-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
YellowPhoenix18 committed Apr 26, 2021
1 parent e526e91 commit 2956740
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/Parser/EditorjsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ListParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\EmbedParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ImageParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\TableParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\HeaderParser;
use SyntaxPhoenix\EJSParserBundle\Parser\EditorjsParserExtension;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\WarningParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\DelimiterParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ParagraphParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\TableParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\SimpleImageParser;

class EditorjsParser
{
Expand Down Expand Up @@ -49,7 +50,8 @@ public function __construct(object $data)
'raw' => new RawParser(),
'list' => new ListParser(),
'warning' => new WarningParser(),
'simpleImage' => new ImageParser(),
'simpleImage' => new SimpleImageParser(),
'image' => new ImageParser(),
'table' => new TableParser()
];
}
Expand Down Expand Up @@ -84,5 +86,4 @@ private function render(): void
}
}
}

}
15 changes: 8 additions & 7 deletions src/Parser/Extension/ImageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ public function parseBlock(HTML5 $html5, DOMDocument $document, object $block, s
{
$figure = $document->createElement('figure');

$figure->setAttribute('class', "{$this->prefix}-image");

$img = $document->createElement('img');

$imgAttrs = [];
$imgAttrs = [
"{$prefix}-image"
];

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

$img->setAttribute('src', $block->data->url);
$img->setAttribute('class', implode(' ', $imgAttrs));
$figure->setAttribute('class', implode(' ', $imgAttrs));

$img = $document->createElement('img');

$img->setAttribute('src', $block->data->file->url);

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

Expand Down
46 changes: 46 additions & 0 deletions src/Parser/Extension/SimpleImageParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace SyntaxPhoenix\EJSParserBundle\Parser\Extension;

use DOMElement;
use DOMDocument;
use Masterminds\HTML5;
use SyntaxPhoenix\EJSParserBundle\Parser\EditorjsParserExtension;

class SimpleImageParser implements EditorjsParserExtension
{

public function parseBlock(HTML5 $html5, DOMDocument $document, object $block, string $prefix): DOMElement
{
$figure = $document->createElement('figure');

$figure->setAttribute('class', "{$prefix}-simpleimage");

$img = $document->createElement('img');

$imgAttrs = [];

if ($block->data->withBorder) {
$imgAttrs[] = "{$prefix}-simpleimage-border";
}
if ($block->data->withBackground) {
$imgAttrs[] = "{$prefix}-simpleimage-background";
}
if ($block->data->stretched) {
$imgAttrs[] = "{$prefix}-simpleimage-stretched";
}

$img->setAttribute('src', $block->data->url);
$img->setAttribute('class', implode(' ', $imgAttrs));

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

$figCaption->appendChild($html5->loadHTMLFragment($block->data->caption));

$figure->appendChild($img);

$figure->appendChild($figCaption);

return $figure;
}
}

0 comments on commit 2956740

Please sign in to comment.