Skip to content

Commit 03beebb

Browse files
committed
Serve llms.txt and the Markdown source of each page, and scroll to sections of GitHub-style links
1 parent 2d243ba commit 03beebb

4 files changed

Lines changed: 269 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Documentation;
6+
use App\LlmsDocs;
7+
8+
class LlmsController extends Controller
9+
{
10+
public function __construct(protected LlmsDocs $llms)
11+
{
12+
}
13+
14+
/**
15+
* The llms.txt index of the default version.
16+
*/
17+
public function index()
18+
{
19+
return $this->text($this->llms->index(DEFAULT_VERSION), 'text/plain');
20+
}
21+
22+
/**
23+
* All pages of the default version in one file.
24+
*/
25+
public function full()
26+
{
27+
return $this->text($this->llms->full(DEFAULT_VERSION), 'text/plain');
28+
}
29+
30+
/**
31+
* The Markdown source of a page.
32+
*/
33+
public function markdown(string $version, string $page)
34+
{
35+
if (! array_key_exists($version, Documentation::getDocVersions())) {
36+
abort(404);
37+
}
38+
39+
return $this->text($this->llms->source($version, $page), 'text/markdown');
40+
}
41+
42+
protected function text(?string $body, string $type)
43+
{
44+
if ($body === null) {
45+
abort(404);
46+
}
47+
48+
return response($body, 200, ['Content-Type' => $type.'; charset=UTF-8']);
49+
}
50+
}

‎app/LlmsDocs.php‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
}

‎resources/views/partials/layout.blade.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,33 @@ class="w-full h-full font-sans antialiased text-gray-900 language-php"
8484
var version = '{{ isset($currentVersion) ? $currentVersion : DEFAULT_VERSION }}';
8585
</script>
8686

87+
<script>
88+
// Pages link to sections the way GitHub renders them (#section), but the headings here have a "content-" prefix.
89+
(function () {
90+
function scrollToSection() {
91+
var id;
92+
try {
93+
id = decodeURIComponent(window.location.hash.slice(1));
94+
} catch (e) {
95+
return;
96+
}
97+
98+
if (id === '' || document.getElementById(id)) {
99+
return;
100+
}
101+
102+
var target = document.getElementById('content-' + id);
103+
104+
if (target) {
105+
target.scrollIntoView();
106+
}
107+
}
108+
109+
window.addEventListener('load', scrollToSection);
110+
window.addEventListener('hashchange', scrollToSection);
111+
})();
112+
</script>
113+
87114
<div class="fixed">
88115
<input type="text">
89116
</div>

‎routes/web.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
use Illuminate\Support\Facades\Route;
44
use App\Http\Controllers\DocsController;
5+
use App\Http\Controllers\LlmsController;
56

67
if (! defined('DEFAULT_VERSION')) {
78
define('DEFAULT_VERSION', 'main');
89
}
910

1011
Route::get('/', [DocsController::class, 'showRootPage']);
1112

13+
Route::get('llms.txt', [LlmsController::class, 'index']);
14+
Route::get('llms-full.txt', [LlmsController::class, 'full']);
15+
Route::get('docs/{version}/{page}.md', [LlmsController::class, 'markdown'])->where('page', '[\w-]+');
16+
1217
Route::get('docs/{version}/index.json', [DocsController::class, 'index']);
1318
Route::get('docs/{version}/{page?}', [DocsController::class, 'show'])->name('docs.version');
1419

0 commit comments

Comments
 (0)