misuzu/src/Parsers/MarkdownParser.php

48 lines
1.4 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu\Parsers;
use Parsedown;
class MarkdownParser extends Parsedown implements ParserInterface {
public function parseText(string $text): string {
return $this->text($text);
}
public function parseLine(string $line): string {
return $this->line($line);
}
protected function inlineImage($excerpt) {
2023-01-26 22:51:33 +00:00
if(!isset($excerpt['text'][1]) || $excerpt['text'][1] !== '[')
return;
2022-09-13 13:14:49 +00:00
2023-01-26 22:51:33 +00:00
$excerpt['text'] = substr($excerpt['text'], 1);
$link = $this->inlineLink($excerpt);
if($link === null)
return;
$inline = [
'extent' => $link['extent'] + 1,
'element' => [
'name' => 'span',
'attributes' => [
'class' => 'js-msz-embed-media',
'data-msz-embed-url' => $link['element']['attributes']['href'],
'data-msz-embed-alt' => $link['element']['text'],
],
'rawHtml' => sprintf(
'<noscript><a href="%1$s" target="_blank" class="link" rel="noopener noreferrer">%1$s</a></noscript>',
htmlspecialchars($link['element']['attributes']['href'])
),
2023-01-26 22:51:33 +00:00
],
];
$inline['element']['attributes'] += $link['element']['attributes'];
unset($inline['element']['attributes']['href']);
return $inline;
2022-09-13 13:14:49 +00:00
}
}