diff --git a/app/content.php b/app/content.php index 8c8782c..2420533 100644 --- a/app/content.php +++ b/app/content.php @@ -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) ]; }