From 72bd2a238c9c801bb3fa7d6774a9b3a580d14648 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Fri, 7 Feb 2025 02:33:40 -0500 Subject: [PATCH] factor out post-link method --- lib/Config.rakumod | 17 +++-------------- lib/Render/Util.rakumod | 13 +++++++++++++ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/Config.rakumod b/lib/Config.rakumod index 838d8c0..298d2ed 100644 --- a/lib/Config.rakumod +++ b/lib/Config.rakumod @@ -3,6 +3,7 @@ use v6.e.PREVIEW; use HTML::Functional; use Render::Head; +use Render::Util; use DB::BlogMeta; use DB::Post; @@ -194,13 +195,7 @@ method generate-post(Post:D $post, BlogMeta:D $meta) { 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 { - "/posts/by-slug/{@slugs[*-1]}.html" - } else { - "/posts/by-id/$id.html" - } + my $link = post-link $id, $post; div :class, [ div :class, [ a :href($link), span [ @@ -272,13 +267,7 @@ method generate-archive($db) { method generate-tag-blurb($db, $tag, $limit?) { sub post-to-link($id, $post) { 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 { - "/posts/by-slug/{@slugs[*-1]}.html" - } else { - "/posts/by-id/$id.html" - } + my $link = post-link $id, $post; div :class, [ div :class, [ a :href($link), span [ diff --git a/lib/Render/Util.rakumod b/lib/Render/Util.rakumod index 5a24dd6..4cf7778 100644 --- a/lib/Render/Util.rakumod +++ b/lib/Render/Util.rakumod @@ -1,6 +1,8 @@ use v6.e.PREVIEW; unit module Render::Util; +use DB::Post; + sub opt($test, $item) is export { if $test { $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" + } +}