-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathHeadingRenderer.php
More file actions
38 lines (29 loc) · 1.23 KB
/
HeadingRenderer.php
File metadata and controls
38 lines (29 loc) · 1.23 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
<?php
namespace App\Support\CommonMark;
use Illuminate\Support\Str;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Util\HtmlElement;
class HeadingRenderer implements NodeRendererInterface
{
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
$tag = 'h'.$node->getLevel();
$attrs = $node->data->get('attributes', []);
$element = new HtmlElement($tag, $attrs, $childRenderer->renderNodes($node->children()));
$id = Str::slug($element->getContents());
$element->setAttribute('id', $id);
if ($node->getLevel() === 1 || $node->getLevel() === 2 || $node->getLevel() === 3) {
$element->setContents(
$element->getContents().
new HtmlElement(
'a',
['href' => "#{$id}", 'class' => 'heading-anchor ml-2 no-underline font-medium', 'style' => 'border-bottom: 0 !important;'],
new HtmlElement('span', ['class' => 'text-gray-600 dark:text-gray-400 hover:text-[#00aaa6]'], '#'),
)
);
}
return $element;
}
}