Add slug resolution for content paths

Implement function to resolve slugs to actual folder paths Update path
parsing to handle slug resolution Add language prefix support to
navigation URLs
This commit is contained in:
Ruben 2025-11-25 23:16:34 +01:00
parent 441c8bca68
commit d10ff75aa4

View file

@ -32,6 +32,28 @@ function findAllContentFiles(string $dir): array {
return array_column($contentFiles, 'path');
}
function resolveSlugToFolder(string $parentDir, string $slug): ?string {
if (!is_dir($parentDir)) return null;
$items = scandir($parentDir) ?: [];
foreach ($items as $item) {
if ($item === '.' || $item === '..' || !is_dir("$parentDir/$item")) continue;
// Check if folder name matches slug
if ($item === $slug) {
return $item;
}
// Check metadata for custom slug
$metadata = loadMetadata("$parentDir/$item");
if ($metadata && isset($metadata['slug']) && $metadata['slug'] === $slug) {
return $item;
}
}
return null;
}
function parseRequestPath(Context $ctx): array {
$requestPath = $ctx->requestPath;
@ -39,7 +61,20 @@ function parseRequestPath(Context $ctx): array {
return ['type' => 'frontpage', 'path' => $ctx->contentDir];
}
$contentPath = $ctx->contentDir . '/' . $requestPath;
// Try resolving slug to actual folder path
$pathParts = explode('/', trim($requestPath, '/'));
$resolvedPath = $ctx->contentDir;
foreach ($pathParts as $part) {
$resolved = resolveSlugToFolder($resolvedPath, $part);
if ($resolved === null) {
// Slug not found, return not_found
return ['type' => 'not_found', 'path' => $ctx->contentDir . '/' . $requestPath];
}
$resolvedPath .= '/' . $resolved;
}
$contentPath = $resolvedPath;
// Check if it's a directory
if (is_dir($contentPath)) {
@ -78,6 +113,7 @@ function loadMetadata(string $dirPath): ?array {
function buildNavigation(Context $ctx): array {
$items = scandir($ctx->contentDir) ?: [];
$navItems = [];
$langPrefix = $ctx->get('langPrefix', '');
foreach ($items as $item) {
if ($item === '.' || $item === '..' || !is_dir($ctx->contentDir . "/$item")) continue;
@ -99,7 +135,7 @@ function buildNavigation(Context $ctx): array {
$navItems[] = [
'title' => $title,
'url' => '/' . urlencode($urlSlug) . '/',
'url' => $langPrefix . '/' . urlencode($urlSlug) . '/',
'order' => (int)($metadata['menu_order'] ?? 999)
];
}