Skip to content

Commit d99afef

Browse files
committed
One session-user reader and one docs page loader behind the search index
`SessionUser::fromSession()` is now the only place the AUTH_USER / AUTH_COLLECTION pair is read. FileAccessManager, UploadAccessPolicy, AccessManager, PersistentLoginService and cms.auth.isSuperAdmin() each carried their own copy of the has/has/get/get dance and the "non-empty id → isSuperAdmin" check. The collection may be empty (the test harness and older sessions store '' for the default) and AccessManager still substitutes the configured one. bin/build-docs-index.php builds the search index through the same DocsPageLoader and DocsMarkdownRenderer the admin viewer uses instead of walking the tree, flattening menu.php and regex-stripping markdown on its own; AdminTwigAdapter::docsMenu() delegates to the loader too, so the menu has one reader instead of three. The rebuilt index no longer starts every excerpt with the raw front matter, and pages without an H1 take their front-matter title instead of the filename.
1 parent 8505206 commit d99afef

18 files changed

Lines changed: 694 additions & 573 deletions

‎bin/build-docs-index.php‎

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,38 @@
1616
require_once __DIR__ . '/lib/docs-reference-parsers.php';
1717
require_once __DIR__ . '/lib/docs-reference-reflectors.php';
1818

19+
use TotalCMS\Domain\Docs\Service\DocsMarkdownRenderer;
20+
use TotalCMS\Domain\Docs\Service\DocsPageLoader;
21+
1922
$docsDir = __DIR__ . '/../resources/docs';
2023
$outputFile = $docsDir . '/search-index.json';
2124
$referenceIndexFile = $docsDir . '/reference-index.json';
2225

23-
// Build path → group lookup from the shared menu config so search results carry
24-
// the section label that matches the in-admin sidebar grouping.
25-
$menu = require $docsDir . '/menu.php';
26-
$groupLookup = [];
27-
foreach ($menu as $topGroup) {
28-
$title = $topGroup['title'] ?? '';
29-
foreach ($topGroup['sub'] ?? [] as $leaf) {
30-
if (isset($leaf['path'])) {
31-
$groupLookup[$leaf['path']] = $title;
32-
}
33-
}
34-
foreach ($topGroup['groups'] ?? [] as $subGroup) {
35-
foreach ($subGroup['sub'] ?? [] as $leaf) {
36-
if (isset($leaf['path'])) {
37-
$groupLookup[$leaf['path']] = $title;
38-
}
39-
}
40-
}
41-
}
26+
// The same loader and renderer the in-admin viewer uses, so the index
27+
// carries the sidebar's group labels and indexes what the reader sees.
28+
$loader = new DocsPageLoader($docsDir);
29+
$renderer = new DocsMarkdownRenderer();
4230

43-
// Get all markdown files (including nested subdirectories)
44-
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($docsDir));
45-
$files = [];
46-
foreach ($iterator as $file) {
47-
if ($file->isFile() && $file->getExtension() === 'md') {
48-
$files[] = $file->getPathname();
49-
}
50-
}
31+
$groupLookup = array_column($loader->pages(), 'group', 'path');
5132

5233
$index = [];
5334

54-
foreach ($files as $file) {
55-
$relativePath = str_replace($docsDir . '/', '', $file);
56-
$path = str_replace('.md', '', $relativePath);
57-
58-
// Skip the index file itself
59-
if ($path === 'index') {
60-
continue;
61-
}
62-
63-
$content = file_get_contents($file);
35+
foreach ($loader->markdownPages() as $path) {
36+
$content = file_get_contents("{$docsDir}/{$path}.md");
6437
if ($content === false) {
6538
continue;
6639
}
6740

68-
// Extract title from first H1
69-
$title = basename($file, '.md');
70-
if (preg_match('/^#\s+(.+)$/m', $content, $matches)) {
71-
$title = trim($matches[1]);
72-
}
41+
$page = $renderer->render($content);
42+
$title = $page['title'] !== '' ? $page['title'] : basename($path);
7343

74-
// Extract all headings for section indexing
75-
$sections = [];
76-
if (preg_match_all('/^##\s+(.+)$/m', $content, $matches)) {
77-
$sections = $matches[1];
78-
}
44+
// H2s are the sections a search hit can point into
45+
$sections = array_values(array_map(
46+
static fn (array $heading): string => $heading['text'],
47+
array_filter($page['toc'], static fn (array $heading): bool => $heading['level'] === 2),
48+
));
7949

80-
// Clean content for indexing:
81-
// - Remove code block fences but keep the code content
82-
$searchContent = (string)preg_replace('/```\w*\n?/', '', $content);
83-
// - Replace <pre> tags with their content (preserve code for searching)
84-
$searchContent = (string)preg_replace('/<pre[^>]*>(.*?)<\/pre>/s', ' $1 ', $searchContent);
85-
// - Remove HTML tags
86-
$searchContent = strip_tags($searchContent);
87-
// - Remove markdown formatting but preserve dots (for cms.login, etc.)
88-
$searchContent = (string)preg_replace('/[#*_`\[\]()]/', ' ', $searchContent);
89-
// - Remove URLs
90-
$searchContent = (string)preg_replace('/https?:\/\/[^\s]+/', '', $searchContent);
91-
// - Remove Twig delimiters but keep the content
92-
$searchContent = (string)preg_replace('/\{\{|\}\}|\{%|%\}/', ' ', $searchContent);
93-
// - Normalize whitespace
94-
$searchContent = (string)preg_replace('/\s+/', ' ', $searchContent);
95-
$searchContent = trim($searchContent);
50+
$searchContent = DocsMarkdownRenderer::searchText($page['content']);
9651

9752
// Create excerpt (first 200 chars after title)
9853
$excerpt = substr($searchContent, 0, 200);

‎resources/bundle‎

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

‎resources/docs/reference-index.json‎

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

0 commit comments

Comments
 (0)