|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Cache\Repository as Cache; |
| 6 | +use Illuminate\Filesystem\Filesystem; |
| 7 | +use Symfony\Component\Yaml\Yaml; |
| 8 | + |
| 9 | +/** |
| 10 | + * Builds the plain-text views of the documentation for AI assistants: |
| 11 | + * the llms.txt index, the llms-full.txt file and the Markdown source of a page. |
| 12 | + */ |
| 13 | +class LlmsDocs |
| 14 | +{ |
| 15 | + public const TITLE = 'Mage-OS developer documentation'; |
| 16 | + |
| 17 | + public const SUMMARY = 'Developer documentation for Mage-OS, a community distribution of Magento Open Source. ' |
| 18 | + .'Every page is available as Markdown: add .md to the page URL.'; |
| 19 | + |
| 20 | + /** |
| 21 | + * How long the generated files are cached, in seconds. |
| 22 | + */ |
| 23 | + protected const CACHE_SECONDS = 60; |
| 24 | + |
| 25 | + public function __construct(protected Filesystem $files, protected Cache $cache) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * The llms.txt index: every page of the navigation with its title and description. |
| 31 | + */ |
| 32 | + public function index(string $version): ?string |
| 33 | + { |
| 34 | + return $this->cache->remember('llms.'.$version.'.index', self::CACHE_SECONDS, function () use ($version) { |
| 35 | + $sections = $this->navigation($version); |
| 36 | + |
| 37 | + if ($sections === []) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + $out = '# '.self::TITLE."\n\n> ".self::SUMMARY."\n\n"; |
| 42 | + $out .= 'The full documentation in one file: '.url('/llms-full.txt')."\n"; |
| 43 | + |
| 44 | + foreach ($sections as $section => $slugs) { |
| 45 | + $lines = []; |
| 46 | + |
| 47 | + foreach ($slugs as $slug) { |
| 48 | + $page = $this->page($version, $slug); |
| 49 | + |
| 50 | + if ($page === null) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + $line = '- ['.$page['title'].']('.url('/docs/'.$version.'/'.$slug.'.md').')'; |
| 55 | + |
| 56 | + if ($page['description'] !== '') { |
| 57 | + $line .= ': '.$page['description']; |
| 58 | + } |
| 59 | + |
| 60 | + $lines[] = $line; |
| 61 | + } |
| 62 | + |
| 63 | + if ($lines !== []) { |
| 64 | + $out .= "\n## ".$section."\n\n".implode("\n", $lines)."\n"; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return $out; |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Every page of the navigation in one file. |
| 74 | + */ |
| 75 | + public function full(string $version): ?string |
| 76 | + { |
| 77 | + return $this->cache->remember('llms.'.$version.'.full', self::CACHE_SECONDS, function () use ($version) { |
| 78 | + $sections = $this->navigation($version); |
| 79 | + |
| 80 | + if ($sections === []) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + $out = '# '.self::TITLE."\n\n> ".self::SUMMARY."\n"; |
| 85 | + |
| 86 | + foreach ($sections as $section => $slugs) { |
| 87 | + foreach ($slugs as $slug) { |
| 88 | + $page = $this->page($version, $slug); |
| 89 | + |
| 90 | + if ($page === null) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + $out .= "\n---\n\nSource: ".url('/docs/'.$version.'/'.$slug)."\nSection: ".$section."\n\n" |
| 95 | + .$page['body']."\n"; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return $out; |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * The Markdown source of one page, without front matter and with absolute links. |
| 105 | + */ |
| 106 | + public function source(string $version, string $slug): ?string |
| 107 | + { |
| 108 | + $page = $this->page($version, $slug); |
| 109 | + |
| 110 | + return $page === null ? null : $page['body']."\n"; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * The sections of documentation.md as section title => page slugs, in navigation order. |
| 115 | + * |
| 116 | + * @return array<string, list<string>> |
| 117 | + */ |
| 118 | + protected function navigation(string $version): array |
| 119 | + { |
| 120 | + $path = base_path('resources/docs/'.$version.'/documentation.md'); |
| 121 | + |
| 122 | + if (! $this->files->exists($path)) { |
| 123 | + return []; |
| 124 | + } |
| 125 | + |
| 126 | + $sections = []; |
| 127 | + $current = null; |
| 128 | + |
| 129 | + foreach (preg_split('/\R/', $this->files->get($path)) as $line) { |
| 130 | + if (preg_match('/^\s*-\s+##\s+(.+?)\s*$/', $line, $match)) { |
| 131 | + $current = $match[1]; |
| 132 | + $sections[$current] ??= []; |
| 133 | + } elseif ($current !== null && preg_match('#\]\(/docs/\{\{version\}\}/([\w-]+)\)#', $line, $match)) { |
| 134 | + if (! in_array($match[1], $sections[$current], true)) { |
| 135 | + $sections[$current][] = $match[1]; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return $sections; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @return array{title: string, description: string, body: string}|null |
| 145 | + */ |
| 146 | + protected function page(string $version, string $slug): ?array |
| 147 | + { |
| 148 | + if (! preg_match('/^[\w-]+$/', $slug)) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + $path = base_path('resources/docs/'.$version.'/'.$slug.'.md'); |
| 153 | + |
| 154 | + if (! $this->files->exists($path)) { |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + $markdown = $this->files->get($path); |
| 159 | + $description = ''; |
| 160 | + |
| 161 | + if (preg_match('/\A---\R(.*?)\R---\R?/s', $markdown, $match)) { |
| 162 | + $markdown = substr($markdown, strlen($match[0])); |
| 163 | + |
| 164 | + try { |
| 165 | + $front = Yaml::parse($match[1]); |
| 166 | + $description = is_array($front) ? trim((string) ($front['description'] ?? '')) : ''; |
| 167 | + } catch (\Throwable $e) { |
| 168 | + // A page with broken front matter is still listed, only without a description. |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // The table of contents placeholder means nothing outside the website. |
| 173 | + $markdown = preg_replace('/^\[TOC\]\R+/m', '', $markdown); |
| 174 | + $markdown = trim(str_replace( |
| 175 | + ['](/docs/{{version}}/', '{{version}}'], |
| 176 | + ['](' . url('/docs/'.$version).'/', $version], |
| 177 | + $markdown |
| 178 | + )); |
| 179 | + $title = preg_match('/^#\s+(.+?)\s*$/m', $markdown, $match) ? $match[1] : $slug; |
| 180 | + |
| 181 | + return [ |
| 182 | + 'title' => $title, |
| 183 | + 'description' => preg_replace('/\s+/', ' ', $description), |
| 184 | + 'body' => $markdown, |
| 185 | + ]; |
| 186 | + } |
| 187 | +} |
0 commit comments