Implement directory content detection for list views

Add support for custom list-grid template Load directory metadata and
page content Improve directory handling logic
This commit is contained in:
Ruben 2025-10-02 22:50:31 +02:00
parent d937ca6166
commit 60ba4b5545

View file

@ -129,7 +129,15 @@ function parseRequestPath(string $requestPath, string $contentDir, array $patter
}
if (is_dir($contentPath)) {
if ($file = findMatchingFile($contentPath, $patterns['single']) ?: findMatchingFile($contentPath, $patterns['page'])) {
// Check if directory has subdirectories
$hasSubdirs = !empty(array_filter(
scandir($contentPath) ?: [],
fn($item) => !in_array($item, ['.', '..']) && is_dir("$contentPath/$item")
));
// If directory has subdirectories, treat as directory (for list views)
// Otherwise, if it has a content file, treat as file
if (!$hasSubdirs && ($file = findMatchingFile($contentPath, $patterns['single']) ?: findMatchingFile($contentPath, $patterns['page']))) {
return ['type' => 'file', 'path' => $file, 'needsSlash' => !$hasTrailingSlash];
}
return ['type' => 'directory', 'path' => realpath($contentPath)];
@ -376,6 +384,31 @@ switch ($parsedPath['type']) {
renderFile("$dir/index.php");
}
// Check for page content file in this directory
$pageContent = null;
if ($pageFile = findMatchingFile($dir, $pageFilePatterns['page'])) {
$ext = pathinfo($pageFile, PATHINFO_EXTENSION);
ob_start();
if ($ext === 'md') {
if (!class_exists('Parsedown')) {
require_once __DIR__ . '/vendor/Parsedown.php';
}
echo (new Parsedown())->text(file_get_contents($pageFile));
} else {
include $pageFile;
}
$pageContent = ob_get_clean();
}
// Load metadata for this directory
$metadata = loadMetadata($dir, $currentLang, $defaultLang);
// Check for custom list template (prefer list-grid.php over list.php)
$customListGridTemplate = dirname(__DIR__) . '/custom/templates/list-grid.php';
if (file_exists($customListGridTemplate)) {
$listTemplate = $customListGridTemplate;
}
// Default directory listing
$subdirs = array_filter(
scandir($dir) ?: [],