Skip to content

Commit 68b4516

Browse files
committed
[Site] Document Toolkit recipes props and blocks
1 parent c7b803c commit 68b4516

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

ux.symfony.com/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"symfony/runtime": "^7.2",
3030
"symfony/serializer": "7.2.*",
3131
"symfony/stimulus-bundle": "2.x-dev",
32+
"symfony/string": "7.2.*",
3233
"symfony/translation": "7.2.*",
3334
"symfony/twig-bundle": "7.2.*",
3435
"symfony/uid": "7.2.*",

ux.symfony.com/composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
use App\Service\Toolkit\ToolkitService;
1616
use League\CommonMark\CommonMarkConverter;
1717
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
18+
use League\CommonMark\Extension\DefaultAttributes\DefaultAttributesExtension;
1819
use League\CommonMark\Extension\ExternalLink\ExternalLinkExtension;
1920
use League\CommonMark\Extension\FrontMatter\FrontMatterExtension;
2021
use League\CommonMark\Extension\Mention\MentionExtension;
22+
use League\CommonMark\Extension\Table\Table;
23+
use League\CommonMark\Extension\Table\TableExtension;
2124
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
2225

2326
/**
@@ -34,6 +37,11 @@ public function __construct(
3437
public function __invoke(): CommonMarkConverter
3538
{
3639
$converter = new CommonMarkConverter([
40+
'default_attributes' => [
41+
Table::class => [
42+
'class' => 'table',
43+
]
44+
],
3745
'mentions' => [
3846
'github_handle' => [
3947
'prefix' => '@',
@@ -52,9 +60,11 @@ public function __invoke(): CommonMarkConverter
5260
]);
5361

5462
$converter->getEnvironment()
63+
->addExtension(new DefaultAttributesExtension())
5564
->addExtension(new ExternalLinkExtension())
5665
->addExtension(new MentionExtension())
5766
->addExtension(new FrontMatterExtension())
67+
->addExtension(new TableExtension())
5868
->addRenderer(FencedCode::class, new CodeBlockRenderer($this->toolkitService))
5969
;
6070

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,20 @@
2222
use Symfony\UX\Toolkit\Recipe\Recipe;
2323
use Symfony\UX\Toolkit\Recipe\RecipeType;
2424
use Symfony\UX\Toolkit\Registry\RegistryFactory;
25+
use function Symfony\Component\String\s;
2526

2627
class ToolkitService
2728
{
29+
/**
30+
* @see https://regex101.com/r/3JXNX7/1
31+
*/
32+
private const RE_API_PROPS = '/{#\s+@prop\s+(?P<name>\w+)\s+(?P<type>[^\s]+)\s+(?P<description>.+?)\s+#}/s';
33+
34+
/**
35+
* @see https://regex101.com/r/jYjXpq/1
36+
*/
37+
private const RE_API_BLOCKS = '/{#\s+@block\s+(?P<name>\w+)\s+(?P<description>.+?)\s+#}/s';
38+
2839
public function __construct(
2940
#[Autowire(service: 'ux_toolkit.registry.registry_factory')]
3041
private readonly RegistryFactory $registryFactory,
@@ -142,6 +153,59 @@ public function renderInstallationSteps(ToolkitKitId $kitId, Recipe $recipe): st
142153
]);
143154
}
144155

156+
public function renderApiReference(Recipe $recipe): string
157+
{
158+
$return = '';
159+
160+
foreach ($recipe->getFiles() as $file) {
161+
$filePath = Path::join($recipe->absolutePath, $file->sourceRelativePathName);
162+
if (!file_exists($filePath)) {
163+
continue;
164+
}
165+
166+
$fileContent = s(file_get_contents($filePath));
167+
168+
// Twig files...
169+
if (str_ends_with($file->sourceRelativePathName, '.html.twig')) {
170+
$props = $fileContent->match(self::RE_API_PROPS, PREG_SET_ORDER);
171+
$blocks = $fileContent->match(self::RE_API_BLOCKS, PREG_SET_ORDER);
172+
173+
if ($props === [] && $blocks === []) {
174+
continue;
175+
}
176+
177+
$componentName = s($file->sourceRelativePathName)
178+
->replace('templates/components/', '')
179+
->replace('.html.twig', '')
180+
->replace('/', ':')
181+
->toString();
182+
$return .= '### '.htmlspecialchars($componentName).\PHP_EOL.\PHP_EOL;
183+
if ([] !== $props) {
184+
$return .= "**Properties**\n\n";
185+
$return .= "| Name | Type | Description |\n";
186+
$return .= "| ---- | ---- | ----------- |\n";
187+
foreach ($props as $prop) {
188+
$return .= sprintf("| `%s` | `%s` | %s |\n", $prop['name'], $prop['type'], trim(preg_replace('/\s+/', ' ', $prop['description'])));
189+
}
190+
$return .= \PHP_EOL;
191+
}
192+
if ([] !== $blocks) {
193+
$return .= "**Blocks**\n\n";
194+
$return .= "| Name | Description |\n";
195+
$return .= "| ---- | ----------- |\n";
196+
foreach ($blocks as $block) {
197+
$return .= sprintf("| `%s` | %s |\n", $block['name'], trim(preg_replace('/\s+/', ' ', $block['description'])));
198+
}
199+
$return .= \PHP_EOL;
200+
}
201+
}
202+
}
203+
204+
dump($return);
205+
206+
return $return;
207+
}
208+
145209
/**
146210
* @param non-empty-array<string, string> $tabs
147211
*/

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public function getContent(): string
5353
5454
## Examples
5555
56+
%s
57+
58+
## API Reference
59+
5660
%s
5761
MARKDOWN,
5862
$this->component->manifest->name,
@@ -64,7 +68,8 @@ public function getContent(): string
6468
$acc .= '### '.$exampleTitle.\PHP_EOL.$examples[$exampleTitle].\PHP_EOL;
6569

6670
return $acc;
67-
}, '')
71+
}, ''),
72+
$this->toolkitService->renderApiReference($this->component),
6873
));
6974
}
7075

0 commit comments

Comments
 (0)