Skip to content

Commit 948abe5

Browse files
committed
[Site] Refactor and use the CodeBlockRenderer::highlightCode() method to render the highlighted code inside a Terminal component
1 parent 2379c89 commit 948abe5

File tree

4 files changed

+19
-38
lines changed

4 files changed

+19
-38
lines changed

ux.symfony.com/src/Service/CommonMark/ConverterFactory.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
use League\CommonMark\Extension\FrontMatter\FrontMatterExtension;
2020
use League\CommonMark\Extension\Mention\MentionExtension;
2121
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
22-
use Symfony\Component\HttpFoundation\UriSigner;
23-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2422

2523
/**
2624
* @author Kevin Bond <kevinbond@gmail.com>

ux.symfony.com/src/Service/CommonMark/Extension/CodeBlockRenderer/CodeBlockRenderer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
final readonly class CodeBlockRenderer implements NodeRendererInterface
2424
{
2525
public function __construct(
26-
private ToolkitService $toolkitService,
26+
private ToolkitService $toolkitService,
2727
) {
2828
}
2929

@@ -40,7 +40,7 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): \
4040
$kitId = ToolkitKitId::tryFrom($options['kit'] ?? null);
4141
$preview = $options['preview'] ?? false;
4242

43-
$output = $this->highlightCode($code = $node->getLiteral(), $language);
43+
$output = $this->highlightCode($language, $code = $node->getLiteral());
4444

4545
if ($kitId && $preview) {
4646
$output = $this->toolkitService->renderComponentPreviewCodeTabs($kitId, $code, $output, $options['height'] ?? '150px');
@@ -49,7 +49,7 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): \
4949
return $output;
5050
}
5151

52-
private function highlightCode(string $code, string $language): string
52+
public static function highlightCode(string $language, string $code, string $style = 'margin-bottom: 1rem'): string
5353
{
5454
$highlighter = new Highlighter();
5555

@@ -60,7 +60,7 @@ private function highlightCode(string $code, string $language): string
6060
: '<pre data-lang="'.$language.'" class="notranslate">'.$parsed.'</pre>';
6161

6262
return <<<HTML
63-
<div class="Terminal terminal-code" style="margin-bottom: 1rem;">
63+
<div class="Terminal terminal-code" style="$style">
6464
<div class="Terminal_body">
6565
<div class="Terminal_content" style="max-height: 450px;">{$output}</div>
6666
</div>

ux.symfony.com/src/Service/Toolkit/ToolkitService.php

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace App\Service\Toolkit;
1313

1414
use App\Enum\ToolkitKitId;
15-
use App\Util\SourceCleaner;
15+
use App\Service\CommonMark\Extension\CodeBlockRenderer\CodeBlockRenderer;
1616
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1717
use Symfony\Component\Filesystem\Path;
1818
use Symfony\Component\HttpFoundation\UriSigner;
@@ -21,15 +21,14 @@
2121
use Symfony\UX\Toolkit\Installer\PoolResolver;
2222
use Symfony\UX\Toolkit\Kit\Kit;
2323
use Symfony\UX\Toolkit\Registry\RegistryFactory;
24-
use function Symfony\Component\String\s;
2524

2625
class ToolkitService
2726
{
2827
public function __construct(
2928
#[Autowire(service: 'ux_toolkit.registry.registry_factory')]
3029
private readonly RegistryFactory $registryFactory,
3130
private readonly UriSigner $uriSigner,
32-
private readonly UrlGeneratorInterface $urlGenerator
31+
private readonly UrlGeneratorInterface $urlGenerator,
3332
) {
3433
}
3534

@@ -76,41 +75,41 @@ public function renderComponentPreviewCodeTabs(ToolkitKitId $kitId, string $code
7675
</div>
7776
<iframe class="Toolkit_Preview loading" src="{$previewUrl}" style="height: {$height};" loading="lazy" onload="this.previousElementSibling.style.display = 'none'; this.classList.remove('loading')"></iframe>
7877
HTML,
79-
'Code' => $highlightedCode
78+
'Code' => $highlightedCode,
8079
]);
8180
}
8281

8382
public function renderInstallationSteps(ToolkitKitId $kitId, Component $component): string
8483
{
8584
$kit = $this->getKit($kitId);
86-
$pool = (new PoolResolver)->resolveForComponent($kit, $component);
85+
$pool = (new PoolResolver())->resolveForComponent($kit, $component);
8786

8887
$manual = '<p>The UX Toolkit is not mandatory to install a component. You can install it manually by following the next steps:</p>';
8988
$manual .= '<ol style="display: grid; gap: 1rem;">';
90-
$manual .= '<li><strong>Copy the files into your Symfony app:</strong>';
89+
$manual .= '<li><strong>Copy the following file(s) into your Symfony app:</strong>';
9190
foreach ($pool->getFiles() as $file) {
92-
$manual .= sprintf(
91+
$manual .= \sprintf(
9392
"<details><summary><code>%s</code></summary>\n%s\n</details>",
9493
$file->relativePathNameToKit,
95-
sprintf("\n```%s\n%s\n```", pathinfo($file->relativePathNameToKit, PATHINFO_EXTENSION), trim(file_get_contents(Path::join($kit->path, $file->relativePathNameToKit))))
94+
\sprintf("\n```%s\n%s\n```", pathinfo($file->relativePathNameToKit, \PATHINFO_EXTENSION), trim(file_get_contents(Path::join($kit->path, $file->relativePathNameToKit))))
9695
);
9796
}
9897
$manual .= '</li>';
9998

10099
if ($phpPackageDependencies = $pool->getPhpPackageDependencies()) {
101100
$manual .= '<li><strong>If necessary, install the following Composer dependencies:</strong>';
102-
$manual .= self::generateTerminal('shell', SourceCleaner::processTerminalLines('composer require ' . implode(' ', $phpPackageDependencies)));
101+
$manual .= CodeBlockRenderer::highlightCode('shell', '$ composer require '.implode(' ', $phpPackageDependencies), 'margin-bottom: 0');
103102
$manual .= '</li>';
104103
}
105104

106105
$manual .= '<li><strong>And the most important, enjoy!</strong></li>';
107106
$manual .= '</ol>';
108107

109108
return $this->generateTabs([
110-
'Automatic' => sprintf(
109+
'Automatic' => \sprintf(
111110
'<p>Ensure the Symfony UX Toolkit is installed in your Symfony app:</p>%s<p>Then, run the following command to install the component and its dependencies:</p>%s',
112-
self::generateTerminal('shell', SourceCleaner::processTerminalLines('composer require --dev symfony/ux-toolkit'), 'margin-bottom: 1rem'),
113-
self::generateTerminal('shell', SourceCleaner::processTerminalLines("bin/console ux:toolkit:install-component {$component->name} --kit {$kitId->value}"), 'margin-bottom: 1rem'),
111+
CodeBlockRenderer::highlightCode('shell', '$ composer require --dev symfony/ux-toolkit'),
112+
CodeBlockRenderer::highlightCode('shell', "$ bin/console ux:toolkit:install-component {$component->name} --kit {$kitId->value}"),
114113
),
115114
'Manual' => $manual,
116115
]);
@@ -130,8 +129,8 @@ private static function generateTabs(array $tabs): string
130129
$activeTabId ??= $tabId;
131130
$isActive = $activeTabId === $tabId;
132131

133-
$tabsControls .= sprintf('<button class="Toolkit_TabControl" data-action="tabs#show" data-tabs-target="control" data-tabs-tab-param="%s" role="tab" aria-selected="%s">%s</button>', $tabId, $isActive ? 'true' : 'false', trim($tabText));
134-
$tabsPanels .= sprintf('<div class="Toolkit_TabPanel %s" data-tabs-target="tab" data-tab="%s" role="tabpanel">%s</div>', $isActive ? 'active' : '', $tabId, $tabContent);
132+
$tabsControls .= \sprintf('<button class="Toolkit_TabControl" data-action="tabs#show" data-tabs-target="control" data-tabs-tab-param="%s" role="tab" aria-selected="%s">%s</button>', $tabId, $isActive ? 'true' : 'false', trim($tabText));
133+
$tabsPanels .= \sprintf('<div class="Toolkit_TabPanel %s" data-tabs-target="tab" data-tab="%s" role="tabpanel">%s</div>', $isActive ? 'active' : '', $tabId, $tabContent);
135134
}
136135

137136
return <<<HTML
@@ -141,17 +140,4 @@ private static function generateTabs(array $tabs): string
141140
</div>
142141
HTML;
143142
}
144-
145-
private static function generateTerminal(string $language, string $content, string $style = ''): string
146-
{
147-
return <<<HTML
148-
<div class="Terminal terminal-code" style="$style">
149-
<div class="Terminal_body">
150-
<div class="Terminal_content">
151-
<pre><code class="language-{$language}">{$content}</code></pre>
152-
</div>
153-
</div>
154-
</div>
155-
HTML;
156-
}
157143
}

ux.symfony.com/src/Twig/Components/Toolkit/ComponentDoc.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@
1313

1414
use App\Enum\ToolkitKitId;
1515
use App\Service\Toolkit\ToolkitService;
16-
use App\Util\SourceCleaner;
17-
use Symfony\Component\HttpFoundation\UriSigner;
18-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1916
use Symfony\Component\String\AbstractString;
2017
use Symfony\UX\Toolkit\Asset\Component;
2118
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
22-
use Tempest\Highlight\Highlighter;
2319

2420
use function Symfony\Component\String\s;
2521

@@ -29,7 +25,8 @@ class ComponentDoc
2925
public ToolkitKitId $kitId;
3026
public Component $component;
3127

32-
public function __construct(private readonly ToolkitService $toolkitService) {
28+
public function __construct(private readonly ToolkitService $toolkitService)
29+
{
3330
}
3431

3532
public function getContent(): string

0 commit comments

Comments
 (0)