Skip to content

Commit b83582a

Browse files
committed
Removed duplicate old link parser function. Added alt for images. Added default target _blank for LinkTool. Added site_name for LinkTool
1 parent 6a686fd commit b83582a

File tree

1 file changed

+11
-52
lines changed

1 file changed

+11
-52
lines changed

src/Parser.php

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -186,58 +186,6 @@ private function parseParagraph($block)
186186
$this->dom->appendChild($node);
187187
}
188188

189-
private function parseLink($block)
190-
{
191-
$link = $this->dom->createElement('a');
192-
193-
$link->setAttribute('href', $block->data->link);
194-
$link->setAttribute('target', '_blank');
195-
$link->setAttribute('class', "{$this->prefix}-link");
196-
197-
$innerContainer = $this->dom->createElement('div');
198-
$innerContainer->setAttribute('class', "{$this->prefix}-link-container");
199-
200-
$hasTitle = isset($block->data->meta->title);
201-
$hasDescription = isset($block->data->meta->description);
202-
$hasImage = isset($block->data->meta->image);
203-
204-
if ($hasTitle) {
205-
$titleNode = $this->dom->createElement('div');
206-
$titleNode->setAttribute('class', "{$this->prefix}-link-title");
207-
$titleText = new DOMText($block->data->meta->title);
208-
$titleNode->appendChild($titleText);
209-
$innerContainer->appendChild($titleNode);
210-
}
211-
212-
if ($hasDescription) {
213-
$descriptionNode = $this->dom->createElement('div');
214-
$descriptionNode->setAttribute('class', "{$this->prefix}-link-description");
215-
$descriptionText = new DOMText($block->data->meta->description);
216-
$descriptionNode->appendChild($descriptionText);
217-
$innerContainer->appendChild($descriptionNode);
218-
}
219-
220-
$linkContainer = $this->dom->createElement('div');
221-
$linkContainer->setAttribute('class', "{$this->prefix}-link-url");
222-
$linkText = new DOMText($block->data->link);
223-
$linkContainer->appendChild($linkText);
224-
$innerContainer->appendChild($linkContainer);
225-
226-
$link->appendChild($innerContainer);
227-
228-
if ($hasImage) {
229-
$imageContainer = $this->dom->createElement('div');
230-
$imageContainer->setAttribute('class', "{$this->prefix}-link-img-container");
231-
$image = $this->dom->createElement('img');
232-
$image->setAttribute('src', $block->data->meta->image->url);
233-
$imageContainer->appendChild($image);
234-
$link->appendChild($imageContainer);
235-
$innerContainer->setAttribute('class', "{$this->prefix}-link-container-with-img");
236-
}
237-
238-
$this->dom->appendChild($link);
239-
}
240-
241189
private function parseEmbed($block)
242190
{
243191
$figure = $this->dom->createElement('figure');
@@ -392,6 +340,7 @@ private function parseImage($block)
392340
$img = $this->dom->createElement('img');
393341

394342
$img->setAttribute('src', $block->data->url);
343+
$img->setAttribute('alt', '');
395344

396345
$figure->appendChild($img);
397346

@@ -423,6 +372,7 @@ private function parseStandardImage($block)
423372
$img = $this->dom->createElement('img');
424373

425374
$img->setAttribute('src', $block->data->url);
375+
$img->setAttribute('alt', '');
426376

427377
$figure->appendChild($img);
428378

@@ -506,11 +456,15 @@ private function parseLinkTool($block)
506456
$figure = $this->dom->createElement('figure');
507457
$figure->setAttribute('class', $this->addClass($block->type));
508458

459+
$site_name = !empty($block->data->meta->site_name) ? $block->data->meta->site_name : parse_url($block->data->link, PHP_URL_HOST);
460+
509461
$link = $this->dom->createElement('a');
510462
$link->setAttribute('href', $block->data->link);
463+
$link->setAttribute('target', '_blank');
511464

512465
$img = $this->dom->createElement('img');
513466
$img->setAttribute('src', $block->data->meta->image->url);
467+
$img->setAttribute('alt', '');
514468

515469
$link->appendChild($img);
516470

@@ -524,6 +478,11 @@ private function parseLinkTool($block)
524478
$link_description->appendChild($this->html5->loadHTMLFragment($block->data->meta->description));
525479
$link->appendChild($link_description);
526480

481+
$link_name = $this->dom->createElement('p');
482+
$link_name->setAttribute('class', "{$this->prefix}_sitename");
483+
$link_name->appendChild($this->html5->loadHTMLFragment($site_name));
484+
$link->appendChild($link_name);
485+
527486
$figure->appendChild($link);
528487

529488
$this->dom->appendChild($figure);

0 commit comments

Comments
 (0)