Upgrade markdown parser for tables and footnotes
This commit is contained in:
parent
978c075a3e
commit
33bb4e77e5
@ -6,6 +6,9 @@ use App\Repository\SnipRepository;
|
|||||||
use App\Service\SnipParser\AbstractParser;
|
use App\Service\SnipParser\AbstractParser;
|
||||||
use League\CommonMark\Event\DocumentParsedEvent;
|
use League\CommonMark\Event\DocumentParsedEvent;
|
||||||
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
|
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
|
||||||
|
use League\CommonMark\Extension\DefaultAttributes\DefaultAttributesExtension;
|
||||||
|
use League\CommonMark\Extension\Footnote\FootnoteExtension;
|
||||||
|
use League\CommonMark\Extension\Table\Table;
|
||||||
use League\CommonMark\GithubFlavoredMarkdownConverter;
|
use League\CommonMark\GithubFlavoredMarkdownConverter;
|
||||||
use League\CommonMark\Node\Inline\Text;
|
use League\CommonMark\Node\Inline\Text;
|
||||||
use League\CommonMark\Node\Query;
|
use League\CommonMark\Node\Query;
|
||||||
@ -23,12 +26,24 @@ class MarkdownParser extends AbstractParser
|
|||||||
|
|
||||||
public function safeParseView(string $content): string
|
public function safeParseView(string $content): string
|
||||||
{
|
{
|
||||||
$converter = new GithubFlavoredMarkdownConverter();
|
$config = [
|
||||||
|
'default_attributes' => [
|
||||||
|
Table::class => [
|
||||||
|
'class' => 'table table-hover',
|
||||||
|
],
|
||||||
|
Link::class => [
|
||||||
|
'class' => 'btn btn-sm btn-secondary',
|
||||||
|
'target' => '_blank',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$converter = new GithubFlavoredMarkdownConverter($config);
|
||||||
$converter
|
$converter
|
||||||
->getEnvironment()
|
->getEnvironment()
|
||||||
->addExtension(new HighlightExtension(
|
->addExtension(new HighlightExtension(new Highlighter()->withGutter()))
|
||||||
new Highlighter()->withGutter()
|
->addExtension(new FootnoteExtension())
|
||||||
))
|
->addExtension(new DefaultAttributesExtension())
|
||||||
->addEventListener(DocumentParsedEvent::class, $this->documentParsed(...))
|
->addEventListener(DocumentParsedEvent::class, $this->documentParsed(...))
|
||||||
;
|
;
|
||||||
return $converter->convert($content);
|
return $converter->convert($content);
|
||||||
@ -43,9 +58,14 @@ class MarkdownParser extends AbstractParser
|
|||||||
->findAll($document)
|
->findAll($document)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
/** @var Link $linkNode */
|
||||||
foreach ($linkNodes as $linkNode) {
|
foreach ($linkNodes as $linkNode) {
|
||||||
$url = $linkNode->getUrl();
|
$url = $linkNode->getUrl();
|
||||||
|
|
||||||
|
if (!is_int($url)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$snip = $this->snipRepo->find($url);
|
$snip = $this->snipRepo->find($url);
|
||||||
if ($snip === null) {
|
if ($snip === null) {
|
||||||
continue;
|
continue;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user