Add meta description support to pages

Add description meta tag to base template Fix navigation list structure
in header Add meta description extraction helper function Update
rendering and routing to include meta description
This commit is contained in:
Ruben 2025-11-03 22:30:49 +01:00
parent 6009cb451e
commit 0b84615bf9
5 changed files with 49 additions and 3 deletions

View file

@ -64,3 +64,43 @@ function findPdfFile(string $dirPath): ?string {
$pdfs = glob("$dirPath/*.pdf") ?: [];
return $pdfs ? basename($pdfs[0]) : null;
}
function extractMetaDescription(string $dirPath, ?array $metadata, string $lang, string $defaultLang): ?string {
// 1. Check for search_description in metadata
if ($metadata && isset($metadata['search_description'])) {
return $metadata['search_description'];
}
// 2. Fall back to summary in metadata
if ($metadata && isset($metadata['summary'])) {
return $metadata['summary'];
}
// 3. Fall back to first paragraph in content files
$files = findAllContentFiles($dirPath, $lang, $defaultLang, []);
if (empty($files)) return null;
foreach ($files as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
$content = file_get_contents($file);
if ($ext === 'md') {
// Skip headings and extract first paragraph
$lines = explode("\n", $content);
foreach ($lines as $line) {
$line = trim($line);
if (empty($line) || str_starts_with($line, '#')) continue;
if (strlen($line) > 20) { // Ignore very short lines
return strip_tags($line);
}
}
} elseif (in_array($ext, ['html', 'php'])) {
// Extract first <p> tag content
if (preg_match('/<p[^>]*>(.*?)<\/p>/is', $content, $matches)) {
return strip_tags($matches[1]);
}
}
}
return null;
}