|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Markdown\CodeGroups; |
| 6 | + |
| 7 | +use App\Markdown\CodeBlocks\CodeBlockRenderer; |
| 8 | +use InvalidArgumentException; |
| 9 | +use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; |
| 10 | +use League\CommonMark\Node\Node; |
| 11 | +use League\CommonMark\Renderer\ChildNodeRendererInterface; |
| 12 | +use League\CommonMark\Renderer\NodeRendererInterface; |
| 13 | +use League\CommonMark\Util\HtmlElement; |
| 14 | +use Override; |
| 15 | + |
| 16 | +final class CodeGroupBlockRenderer implements NodeRendererInterface |
| 17 | +{ |
| 18 | + public function __construct( |
| 19 | + private CodeBlockRenderer $code_block_renderer, |
| 20 | + ) {} |
| 21 | + |
| 22 | + #[Override] |
| 23 | + public function render(Node $node, ChildNodeRendererInterface $child_renderer): mixed |
| 24 | + { |
| 25 | + if (! ($node instanceof CodeGroupBlock)) { |
| 26 | + throw new InvalidArgumentException('Incompatible node type: ' . $node::class); |
| 27 | + } |
| 28 | + |
| 29 | + $tabs = []; |
| 30 | + $panels = []; |
| 31 | + $index = 0; |
| 32 | + |
| 33 | + foreach ($node->children() as $child) { |
| 34 | + if (! ($child instanceof FencedCode)) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + $info_words = $child->getInfoWords(); |
| 39 | + $filename = $info_words[1] ?? "Tab {$index}"; |
| 40 | + |
| 41 | + $is_active = $index === 0; |
| 42 | + $tab_id = 'tab-' . md5($filename . $index); |
| 43 | + $panel_id = 'panel-' . md5($filename . $index); |
| 44 | + |
| 45 | + $tabs[] = new HtmlElement( |
| 46 | + tagName: 'button', |
| 47 | + attributes: [ |
| 48 | + 'class' => 'code-group-tab' . ($is_active ? ' active' : ''), |
| 49 | + 'role' => 'tab', |
| 50 | + 'aria-selected' => $is_active ? 'true' : 'false', |
| 51 | + 'aria-controls' => $panel_id, |
| 52 | + 'id' => $tab_id, |
| 53 | + 'data-panel' => $panel_id, |
| 54 | + ], |
| 55 | + contents: $filename, |
| 56 | + ); |
| 57 | + |
| 58 | + $rendered_code = $this->code_block_renderer->render($child, $child_renderer); |
| 59 | + |
| 60 | + $panels[] = new HtmlElement( |
| 61 | + tagName: 'div', |
| 62 | + attributes: array_filter([ |
| 63 | + 'class' => 'code-group-panel' . ($is_active ? ' active' : ''), |
| 64 | + 'role' => 'tabpanel', |
| 65 | + 'aria-labelledby' => $tab_id, |
| 66 | + 'id' => $panel_id, |
| 67 | + 'hidden' => $is_active ? null : 'hidden', |
| 68 | + ]), |
| 69 | + contents: $rendered_code, |
| 70 | + ); |
| 71 | + |
| 72 | + $index++; |
| 73 | + } |
| 74 | + |
| 75 | + if ($tabs === []) { |
| 76 | + return ''; |
| 77 | + } |
| 78 | + |
| 79 | + $tab_list = new HtmlElement( |
| 80 | + tagName: 'div', |
| 81 | + attributes: [ |
| 82 | + 'class' => 'code-group-tabs', |
| 83 | + 'role' => 'tablist', |
| 84 | + ], |
| 85 | + contents: $tabs, |
| 86 | + ); |
| 87 | + |
| 88 | + return new HtmlElement( |
| 89 | + tagName: 'div', |
| 90 | + attributes: ['class' => 'code-group'], |
| 91 | + contents: [$tab_list, ...$panels], |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
0 commit comments