-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathAlertBlockRenderer.php
More file actions
70 lines (60 loc) · 2.33 KB
/
AlertBlockRenderer.php
File metadata and controls
70 lines (60 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace App\Markdown\Alerts;
use InvalidArgumentException;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\HtmlRenderer;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Util\HtmlElement;
use Override;
use Tempest\View\ViewRenderer;
use function Tempest\Container\get;
use function Tempest\View\view;
final class AlertBlockRenderer implements NodeRendererInterface
{
#[Override]
public function render(Node $node, ChildNodeRendererInterface $childRenderer): mixed
{
if (! $node instanceof AlertBlock) {
throw new InvalidArgumentException('Incompatible node type: ' . get_class($node));
}
if (! $childRenderer instanceof HtmlRenderer) {
throw new InvalidArgumentException('Incompatible renderer type: ' . get_class($childRenderer));
}
$iconName = match ($node->icon) {
'false' => null,
null => match ($node->alertType) {
'warning' => 'tabler:exclamation-circle',
'info' => 'tabler:info-circle',
'success' => 'tabler:check-circle',
'error' => 'tabler:exclamation-circle',
default => null,
},
default => $node->icon,
};
$icon = $iconName
? get(ViewRenderer::class)->render(view('<x-icon :name="$name" class="alert-icon" />', name: $iconName))
: null;
$content = new HtmlElement(
tagName: 'div',
attributes: ['class' => 'alert-wrapper'],
contents: [
$icon ? new HtmlElement('div', attributes: ['class' => 'alert-icon-wrapper'], contents: $icon) : null,
new HtmlElement(
tagName: 'div',
attributes: ['class' => 'alert-content'],
contents: [
$node->title ? new HtmlElement('span', attributes: ['class' => 'alert-title'], contents: $node->title) : null,
$childRenderer->renderNodes($node->children()),
],
),
],
);
return new HtmlElement(
'div',
['class' => "alert alert-{$node->alertType}"],
$content,
);
}
}