Use CommonMark plugin for @-mentions

This commit is contained in:
Jonny Barnes 2022-07-08 16:37:38 +01:00
parent 08b3691aeb
commit 3ff4149304
Signed by: jonny
SSH key fingerprint: SHA256:CTuSlns5U7qlD9jqHvtnVmfYV3Zwl2Z7WnJ4/dqOaL8
6 changed files with 89 additions and 17 deletions

View file

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\CommonMark\Renderers;
use App\Models\Contact;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
class ContactMentionRenderer implements NodeRendererInterface
{
public function render(Node $node, ChildNodeRendererInterface $childRenderer): string
{
$contact = Contact::where('nick', $node->getIdentifier())->first();
if ($contact === null) {
return '<a href="https://twitter.com/' . $node->getIdentifier() . '">@' . $node->getIdentifier() . '</a>';
}
return trim(view('templates.mini-hcard', ['contact' => $contact])->render());
}
}