Generate Index

This commit is contained in:
Nathan McCarty 2025-02-05 03:26:39 -05:00
parent d861c7ef87
commit c59c266ee1
4 changed files with 115 additions and 12 deletions

View file

@ -6,12 +6,16 @@ use DB::Post;
unit class Config;
method generate-head(Str:D $title, BlogMeta:D $meta, $description?) {
method generate-head($title, BlogMeta:D $meta, $description?) {
head [
meta :charset<utf-8>;
meta :name<viewport>, :content<width=device-width, initial-scale=1>;
meta :author :content<Nathan McCarty>;
title "$title {$meta.title}";
do if $title ~~ Str:D {
title "$title {$meta.title}";
} else {
title $meta.title;
}
# Add description, if one exists
do if $description ~~ Str:D {
meta :description :content($description)
@ -123,15 +127,19 @@ method post-read-time(Post:D $post) {
]
}
method post-info(Post:D $post) {
div :class<post-info>, [
self.post-date: $post;
self.post-read-time: $post;
];
}
method post-header(Post:D $post) {
header :class<post-header>, [
div :class<post-title>, [
h1 $post.title;
];
div :class<post-info>, [
self.post-date: $post;
self.post-read-time: $post;
];
self.post-info: $post;
# TODO: Add tags once we have support for that
]
}
@ -161,6 +169,59 @@ method generate-post(Post:D $post, BlogMeta:D $meta) {
"<!doctype html>$html"
}
method generate-blurb(Int:D $id, $db) {
my $post = $db.posts{$id};
my $desc = $post.description;
my @slugs = $post.all-slugs;
# Use the primary slug if there is one, the id if there isn't
my $link = do if @slugs.elems > 0 {
"/posts/by-slug/{@slugs[0]}.html"
} else {
"/posts/by-id/$id.html"
}
div :class<post-blurb>, [
div :class<post-blurb-title>, [
a :href($link), span [
h2 $post.title;
];
];
self.post-info: $post;
if $desc ~~ Str:D {
div :class<post-blurb-description>, [
p $post.description;
];
} else {
[]
}
]
}
method generate-index($db) {
my @most-recent =
$db.sorted-posts
.head(10)
.grep(!*.value.hidden)
.map(-> $pair {
self.generate-blurb: $pair.key, $db
});
my $head = self.generate-head(Nil, $db.meta);
my $body = body [
self.site-header: $db.meta;
div :class<post-blurbs>, [
h1 "Recent Posts"
], @most-recent;
];
my $html =
html :lang<en>, [
$head,
$body
];
"<!doctype html>$html"
}
sub icon($icon) {
i(:class("bx bx-$icon"))
}