Added tag filtering and RSS feed

This commit is contained in:
2026-08-13 19:31:59 -05:00
parent 2b2ab3a115
commit afebcae714
5 changed files with 217 additions and 82 deletions

View File

@@ -1,49 +1,12 @@
<?php
header('Content-Type: application/json');
require __DIR__ . '/posts.php';
$dir = __DIR__;
$files = array_diff(scandir($dir), ['.', '..', 'list.php']);
$posts = getPosts(__DIR__);
function parseFrontMatter($raw) {
if (preg_match('/^---\r?\n(.+?)\r?\n---\r?\n(.*)$/s', $raw, $m)) {
$meta = [];
foreach (preg_split('/\r?\n/', $m[1]) as $line) {
if (strpos($line, ':') === false) continue;
[$key, $value] = explode(':', $line, 2);
$meta[trim($key)] = trim($value, " \t\"'");
}
return $meta;
}
return [];
}
$posts = [];
foreach ($files as $file) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (!in_array($ext, ['html', 'md'])) continue;
$path = $dir . '/' . $file;
$fallbackTitle = ucwords(str_replace(['-', '_'], ' ', pathinfo($file, PATHINFO_FILENAME)));
$created = filectime($path); // fallback if no front matter date
$meta = [];
if ($ext === 'md') {
$raw = file_get_contents($path, false, null, 0, 4096); // just need the header
$meta = parseFrontMatter($raw);
}
$title = $meta['title'] ?? $fallbackTitle;
$date = isset($meta['date']) ? strtotime($meta['date']) : $created;
$posts[] = [
'file' => $file,
'title' => $title,
'type' => $ext,
'created' => $date ?: $created,
'date' => date('F j, Y', $date ?: $created),
];
}
usort($posts, fn($a, $b) => $b['created'] - $a['created']); // newest first
$posts = array_map(function ($p) {
unset($p['excerpt']);
return $p;
}, $posts);
echo json_encode($posts);