|
13 | 13 |
|
14 | 14 | use App\Enum\ToolkitKitId;
|
15 | 15 | use App\Service\Toolkit\ToolkitService;
|
| 16 | +use Symfony\Component\Filesystem\Filesystem; |
| 17 | +use Symfony\Component\Filesystem\Path; |
16 | 18 | use Symfony\Component\String\AbstractString;
|
17 |
| -use Symfony\UX\Toolkit\Asset\Component; |
| 19 | +use Symfony\UX\Toolkit\Recipe\Recipe; |
18 | 20 | use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
|
19 | 21 |
|
20 | 22 | use function Symfony\Component\String\s;
|
|
23 | 25 | class ComponentDoc
|
24 | 26 | {
|
25 | 27 | public ToolkitKitId $kitId;
|
26 |
| - public Component $component; |
| 28 | + public Recipe $component; |
27 | 29 |
|
28 |
| - public function __construct(private readonly ToolkitService $toolkitService) |
29 |
| - { |
| 30 | + public function __construct( |
| 31 | + private readonly Filesystem $filesystem, |
| 32 | + private readonly ToolkitService $toolkitService |
| 33 | + ) { |
30 | 34 | }
|
31 | 35 |
|
32 | 36 | public function getContent(): string
|
33 | 37 | {
|
34 |
| - return $this->formatContent($this->component->doc->markdownContent); |
35 |
| - } |
| 38 | + $examples = $this->getExamples(); |
36 | 39 |
|
37 |
| - private function formatContent(string $markdownContent): string |
38 |
| - { |
39 |
| - $markdownContent = s($markdownContent); |
| 40 | + return $this->adaptPreviewableCodeBlocks(sprintf(<<<MARKDOWN |
| 41 | + # %s |
40 | 42 |
|
41 |
| - $markdownContent = $this->insertInstallation($markdownContent); |
42 |
| - $markdownContent = $this->insertUsage($markdownContent); |
43 |
| - $markdownContent = $this->adaptPreviewableCodeBlocks($markdownContent); |
| 43 | + %s |
44 | 44 |
|
45 |
| - return $markdownContent; |
46 |
| - } |
| 45 | + %s |
47 | 46 |
|
48 |
| - private function insertInstallation(AbstractString $markdownContent): AbstractString |
49 |
| - { |
50 |
| - return $markdownContent->replace( |
51 |
| - '<!-- Placeholder: Installation -->', |
52 |
| - $this->toolkitService->renderInstallationSteps($this->kitId, $this->component) |
53 |
| - ); |
| 47 | + ## Installation |
| 48 | +
|
| 49 | + %s |
| 50 | +
|
| 51 | + ## Usage |
| 52 | +
|
| 53 | + %s |
| 54 | +
|
| 55 | + ## Examples |
| 56 | +
|
| 57 | + %s |
| 58 | + MARKDOWN, |
| 59 | + $this->component->manifest->name, |
| 60 | + $this->component->manifest->description, |
| 61 | + current($examples), |
| 62 | + $this->toolkitService->renderInstallationSteps($this->kitId, $this->component), |
| 63 | + dump(preg_replace('/^```twig.*\n/', '```twig'.PHP_EOL, current($examples))), |
| 64 | + array_reduce(array_keys($examples), function (string $acc, string $exampleTitle) use ($examples) { |
| 65 | + $acc .= '### '.$exampleTitle.PHP_EOL.$examples[$exampleTitle].PHP_EOL; |
| 66 | + |
| 67 | + return $acc; |
| 68 | + }, '') |
| 69 | + )); |
54 | 70 | }
|
55 | 71 |
|
56 |
| - private function insertUsage(AbstractString $markdownContent): AbstractString |
| 72 | + /** |
| 73 | + * @return array<string, string> |
| 74 | + */ |
| 75 | + private function getExamples(): array |
57 | 76 | {
|
58 |
| - $firstTwigPreviewBlock = $markdownContent->match('/```twig.*?\n(.+?)```/s'); |
59 |
| - $firstTwigPreviewBlock = $firstTwigPreviewBlock ? trim($firstTwigPreviewBlock[1]) : ''; |
| 77 | + $examplesMdPath = Path::join($this->component->absolutePath, 'EXAMPLES.md'); |
| 78 | + |
| 79 | + $markdown = s($this->filesystem->readFile($examplesMdPath)); |
| 80 | + |
| 81 | + // Remove "# Examples" header |
| 82 | + $markdown = $markdown->replace('# Examples', ''); |
| 83 | + |
| 84 | + // Split the markdown for each title and content |
| 85 | + $examples = []; |
| 86 | + foreach (explode(PHP_EOL, $markdown) as $line) { |
| 87 | + if (str_starts_with($line, '## ')) { |
| 88 | + // This is a new example title |
| 89 | + $title = trim(substr($line, 2)); |
| 90 | + $examples[$title] = ''; |
| 91 | + } elseif (isset($title)) { |
| 92 | + // This line belongs to the last example |
| 93 | + $examples[$title] .= $line.PHP_EOL; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if ([] === $examples) { |
| 98 | + throw new \LogicException(sprintf('No examples found in "%s".', $examplesMdPath)); |
| 99 | + } |
| 100 | + |
| 101 | + foreach ($examples as $title => &$example) { |
| 102 | + $example = trim($example); |
| 103 | + } |
60 | 104 |
|
61 |
| - return $markdownContent->replace( |
62 |
| - '<!-- Placeholder: Usage -->', |
63 |
| - '```twig'."\n".$firstTwigPreviewBlock."\n".'```' |
64 |
| - ); |
| 105 | + return $examples; |
65 | 106 | }
|
66 | 107 |
|
67 | 108 | /**
|
68 | 109 | * Iterate over code blocks, and add the option "kit" if the option "preview" exists.
|
69 | 110 | */
|
70 |
| - private function adaptPreviewableCodeBlocks(AbstractString $markdownContent): AbstractString |
| 111 | + private function adaptPreviewableCodeBlocks(string $markdownContent): string |
71 | 112 | {
|
72 |
| - return $markdownContent->replaceMatches('/```(?P<lang>[a-z]+) +(?P<options>\{.+?\})\n/', function (array $matches) { |
| 113 | + return s($markdownContent)->replaceMatches('/```(?P<lang>[a-z]+) +(?P<options>\{.+?\})\n/', function (array $matches) { |
73 | 114 | $lang = $matches['lang'];
|
74 | 115 | $options = json_decode($matches['options'], true, flags: \JSON_THROW_ON_ERROR);
|
75 | 116 |
|
76 | 117 | if ($options['preview'] ?? false) {
|
77 | 118 | $options['kit'] = $this->kitId->value;
|
78 | 119 | }
|
79 | 120 |
|
80 |
| - return \sprintf('```%s %s'."\n", $lang, json_encode($options, \JSON_THROW_ON_ERROR)); |
81 |
| - }); |
| 121 | + return \sprintf('```%s %s'.PHP_EOL, $lang, json_encode($options, \JSON_THROW_ON_ERROR)); |
| 122 | + })->toString(); |
82 | 123 | }
|
83 | 124 | }
|
0 commit comments