💻 My personal website blog.kacaii.dev/
blog gleam lustre

:recycle: sort posts by date

kacaii.dev 5aecf6e4 bf585843

Waiting for spindle ...
+31 -8
+11 -4
src/blog.gleam
··· 2 2 import filepath as path 3 3 import gleam/erlang/process 4 4 import gleam/list 5 + import gleam/result 5 6 import simplifile 6 7 import supervision_tree 7 8 import web ··· 25 26 let assert Ok(entries) = simplifile.read_directory(posts_path) 26 27 as "Read posts directory" 27 28 28 - use file_name <- list.try_map(entries) 29 - posts_path 30 - |> path.join(file_name) 31 - |> post.parse 29 + use posts <- result.map({ 30 + use file_name <- list.try_map(entries) 31 + 32 + posts_path 33 + |> path.join(file_name) 34 + |> post.parse 35 + }) 36 + 37 + list.sort(posts, post.compare) 38 + |> list.reverse 32 39 } 33 40 34 41 pub fn priv_directory() -> Result(String, Nil) {
+3 -3
src/blog/page/posts.gleam
··· 13 13 import wisp 14 14 15 15 pub fn handle_request(ctx: web.Context) -> wisp.Response { 16 - let ul_styles = class("grid-cols-1 gap-4 mx-auto max-w-lg") 16 + let ul_styles = class("grid grid-cols-1 gap-4 mx-auto max-w-lg") 17 17 18 18 let content = [ 19 19 navbar.view(), ··· 39 39 } 40 40 41 41 let li_styles = 42 - class("flex-col p-4 mx-auto rounded-lg shadow-sm bg-ctp-mantle") 42 + class("flex-col p-4 mx-auto w-full rounded-lg shadow-sm bg-ctp-mantle") 43 43 44 44 html.li([li_styles], [ 45 45 html.a([attr.href("/posts/" <> post.to_uri_path(post))], [ ··· 55 55 } 56 56 57 57 fn tag_to_li(tag: String) -> element.Element(_) { 58 - html.li([class(" py-0.5 px-2.5 rounded-md bg-ctp-surface0")], [html.text(tag)]) 58 + html.li([class("py-0.5 px-2.5 rounded-md bg-ctp-surface0")], [html.text(tag)]) 59 59 }
+1 -1
src/blog/page/recent_posts.gleam
··· 12 12 |> list.take(2) 13 13 |> list.map(post_to_li) 14 14 15 - let articles_section = html.ul([], previews) 15 + let articles_section = html.ul([class("grid grid-cols-1 gap-2")], previews) 16 16 let section_title = html.h3([class("text-lg")], [html.text("Recent posts")]) 17 17 let more_style = class("underline text-md underline-offset-2") 18 18 let more = html.a([attr.href("/posts"), more_style], [html.text("More")])
+16
src/blog/post.gleam
··· 1 1 import contour 2 2 import frontmatter 3 + import gleam/int 3 4 import gleam/list 4 5 import gleam/option 6 + import gleam/order 5 7 import gleam/result 6 8 import gleam/string 7 9 import gleam/time/calendar ··· 111 113 other -> other 112 114 } 113 115 } 116 + 117 + pub fn compare(a: Post, b: Post) -> order.Order { 118 + let a_day = a.meta.date.day 119 + let a_month = calendar.month_to_int(a.meta.date.month) 120 + let a_year = a.meta.date.year 121 + 122 + let b_day = b.meta.date.day 123 + let b_month = calendar.month_to_int(b.meta.date.month) 124 + let b_year = b.meta.date.year 125 + 126 + use <- order.lazy_break_tie(int.compare(a_year, b_year)) 127 + use <- order.lazy_break_tie(int.compare(a_month, b_month)) 128 + int.compare(a_day, b_day) 129 + }