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

@ -86,7 +86,7 @@ header {
margin-top: .4rem;
padding: 0;
justify-content: flex-end;
a {
li {
margin-left:0.4rem;
margin-top:0.4rem;
}

View file

@ -17,6 +17,9 @@ function getActiveClass($href) { return rtrim(parse_url($_SERVER['REQUEST_URI'],
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php if (!empty($metaDescription)): ?>
<meta name="description" content="<?= htmlspecialchars($metaDescription) ?>">
<?php endif; ?>
<link rel="stylesheet" href="<?= $cssUrl ?>?v=<?= $cssHash ?>">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="icon" href="/favicon.png" type="image/png">
@ -36,10 +39,10 @@ function getActiveClass($href) { return rtrim(parse_url($_SERVER['REQUEST_URI'],
</div>
<nav>
<ul>
<a href="/" class="button <?php echo getActiveClass('/'); ?>"><li><?= htmlspecialchars($translations['home'] ?? 'Home') ?></li></a>
<li><a href="/" class="button <?php echo getActiveClass('/'); ?>"><?= htmlspecialchars($translations['home'] ?? 'Home') ?></a></li>
<?php if (!empty($navigation)): ?>
<?php foreach ($navigation as $item): ?>
<a href="<?= htmlspecialchars($item['url']) ?>" class="button <?php echo getActiveClass($item['url']); ?>"><li><?= htmlspecialchars($item['title']) ?></li></a>
<li><a href="<?= htmlspecialchars($item['url']) ?>" class="button <?php echo getActiveClass($item['url']); ?>"><?= htmlspecialchars($item['title']) ?></a></li>
<?php endforeach; ?>
<?php endif; ?>
</ul>

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;
}

View file

@ -57,6 +57,7 @@ function renderFile(Context $ctx, string $filePath): void {
$pageDir = dirname($realPath);
$pageMetadata = loadMetadata($pageDir, $ctx->currentLang, $ctx->defaultLang);
$pageTitle = $pageMetadata['title'] ?? null;
$metaDescription = extractMetaDescription($pageDir, $pageMetadata, $ctx->currentLang, $ctx->defaultLang);
// Wrap content with page template
ob_start();
@ -94,6 +95,7 @@ function renderMultipleFiles(Context $ctx, array $filePaths, string $pageDir): v
$pageMetadata = loadMetadata($pageDir, $ctx->currentLang, $ctx->defaultLang);
$pageTitle = $pageMetadata['title'] ?? null;
$metaDescription = extractMetaDescription($pageDir, $pageMetadata, $ctx->currentLang, $ctx->defaultLang);
// Wrap content with page template
ob_start();

View file

@ -135,6 +135,7 @@ switch ($parsedPath['type']) {
$homeLabel = $ctx->homeLabel;
$translations = $ctx->translations;
$pageTitle = $metadata['title'] ?? null;
$metaDescription = extractMetaDescription($dir, $metadata, $ctx->currentLang, $ctx->defaultLang);
include $ctx->templates->base;
exit;