aboutsummaryrefslogtreecommitdiff
path: root/src/Pages/blog/blog.php
blob: f944c2e1542e8df740395695a9623aafa55a3e95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
require 'Parsedown.php';
$Parsedown = new Parsedown();

$postsDir = __DIR__ . '/posts/';

// Get requested post from URL (pretty slug)
$postParam = $_GET['post'] ?? null;

if ($postParam) {
    // Search for matching file by slug (ignoring date prefix)
    $files = glob($postsDir . '*.md');
    $found = null;
    foreach ($files as $file) {
        $filename = pathinfo($file, PATHINFO_FILENAME); // e.g., 2026-03-05-why-i-use-nixos
        $slug = preg_replace('/^\d{4}-\d{2}-\d{2}-/', '', $filename); // remove date prefix
        if ($slug === $postParam) {
            $found = $file;
            break;
        }
    }

    if ($found) {
        $markdown = file_get_contents($found);

        // Extract first heading line (# Title) as title
        if (preg_match('/^# (.+)/m', $markdown, $matches)) {
            $title = trim($matches[1]);
            // Remove the first heading from content
            $markdown = preg_replace('/^# .*/', '', $markdown, 1);
        } else {
            $title = "Blog Post";
        }

        $content = $Parsedown->text($markdown);
    } else {
        // Fallback to list if slug not found
        $postParam = null;
    }
}

// If no valid post requested → show list of all posts
if (!$postParam) {
    $files = array_reverse(glob($postsDir . '*.md')); // newest first
    $content = "<ul class='blog-list'>";
    foreach ($files as $file) {
        $filename = pathinfo($file, PATHINFO_FILENAME);
        $slug = preg_replace('/^\d{4}-\d{2}-\d{2}-/', '', $filename);

        // Extract first # heading as title
        $mdContent = file_get_contents($file);
        if (preg_match('/^# (.+)/m', $mdContent, $matches)) {
            $name = htmlspecialchars(trim($matches[1]));
        } else {
            $name = htmlspecialchars($slug);
        }

        $content .= "<li><a href='/blog/$slug'>$name</a></li>";
    }
    $content .= "</ul>";
    $title = "Blog";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($title) ?></title>
<link rel="stylesheet" href="../style.css">
<style>
/* Blog-specific tweaks */
.blog-content {
    margin-top: 20px;
    line-height: 1.6;
}
.blog-content h1, .blog-content h2, .blog-content h3 {
    color: #ff00ff;
    text-shadow: 0 0 5px #ff00ff, 0 0 15px rgba(255,0,255,0.6);
}
.blog-content p, .blog-content li {
    color: #e0d6ff;
}
.blog-list li {
    margin-bottom: 8px;
}
.blog-list a {
    color: #00ffff;
    text-decoration: none;
}
.blog-list a:hover {
    text-shadow: 0 0 8px #00ffff;
}
</style>
</head>
<body>
<canvas id="bg"></canvas>
<div class="container">
    <h1><?= htmlspecialchars($title) ?></h1>
    <div class="blog-content">
        <?= $content ?>
    </div>
    <footer>
        <p>© 256phi | 2026 | <a href="/home">Home</a></p>
    </footer>
</div>
<script type="module" src="../main.js"></script>
</body>
</html>