factor out post-link method

This commit is contained in:
Nathan McCarty 2025-02-07 02:33:40 -05:00
parent 68e20af5d7
commit 72bd2a238c
2 changed files with 16 additions and 14 deletions

View file

@ -3,6 +3,7 @@ use v6.e.PREVIEW;
use HTML::Functional; use HTML::Functional;
use Render::Head; use Render::Head;
use Render::Util;
use DB::BlogMeta; use DB::BlogMeta;
use DB::Post; use DB::Post;
@ -194,13 +195,7 @@ method generate-post(Post:D $post, BlogMeta:D $meta) {
method generate-blurb(Int:D $id, $db) { method generate-blurb(Int:D $id, $db) {
my $post = $db.posts{$id}; my $post = $db.posts{$id};
my $desc = $post.description; my $desc = $post.description;
my @slugs = $post.all-slugs; my $link = post-link $id, $post;
# Use the primary slug if there is one, the id if there isn't
my $link = do if @slugs.elems {
"/posts/by-slug/{@slugs[*-1]}.html"
} else {
"/posts/by-id/$id.html"
}
div :class<post-blurb>, [ div :class<post-blurb>, [
div :class<post-blurb-title>, [ div :class<post-blurb-title>, [
a :href($link), span [ a :href($link), span [
@ -272,13 +267,7 @@ method generate-archive($db) {
method generate-tag-blurb($db, $tag, $limit?) { method generate-tag-blurb($db, $tag, $limit?) {
sub post-to-link($id, $post) { sub post-to-link($id, $post) {
my $desc = $post.description; my $desc = $post.description;
my @slugs = $post.all-slugs; my $link = post-link $id, $post;
# Use the primary slug if there is one, the id if there isn't
my $link = do if @slugs.elems {
"/posts/by-slug/{@slugs[*-1]}.html"
} else {
"/posts/by-id/$id.html"
}
div :class<tag-blurb-post>, [ div :class<tag-blurb-post>, [
div :class<tag-blurb-post-title>, [ div :class<tag-blurb-post-title>, [
a :href($link), span [ a :href($link), span [

View file

@ -1,6 +1,8 @@
use v6.e.PREVIEW; use v6.e.PREVIEW;
unit module Render::Util; unit module Render::Util;
use DB::Post;
sub opt($test, $item) is export { sub opt($test, $item) is export {
if $test { if $test {
$item $item
@ -16,3 +18,14 @@ sub optl($test, &item) is export {
[] []
} }
} }
#| Link to the post by the primary slug, if there is one, linking to it by id
#| otherwise
sub post-link(Int:D $id, Post:D $post --> Str:D) is export {
my @slugs = $post.all-slugs;
if @slugs {
"/posts/by-slug/{@slugs[*-1]}.html"
} else {
"/posts/by-id/$id.html"
}
}