WIP: list posts function

This commit is contained in:
Catalin Constantin Mititiuc 2025-05-20 13:40:20 -07:00
parent c8a1632902
commit bae7126189
2 changed files with 46 additions and 0 deletions

View File

@ -1,3 +1,11 @@
<h2>Posts</h2> <h2>Posts</h2>
$list_posts2{limit = 3}[[
<section>
<h3><a href="$target">$post_title</a></h3>
<time>$publish_date</time> — $blurb
<a class="read-post-link" href="$target">Read post →</a>
</section>
]]
$list_posts $list_posts

View File

@ -58,6 +58,16 @@ extract_date = (source) -> string.match Path.filename(source), "%d+%-%d%d%-%d%d"
format_date = (str) -> date(str)\fmt "%b %d, %Y" format_date = (str) -> date(str)\fmt "%b %d, %Y"
publish_date = (path) -> format_date extract_date path publish_date = (path) -> format_date extract_date path
posts2 = (path=".") ->
files = reverse get_files path
[{
path: join(path, file)
target: Path.join Path.basepath(target(file, "/posts")), extract_id(join(path, file))
post_id: extract_id join(path, file)
publish_date: publish_date join(path, file)
} for file in *files]
common = require("sitegen.common") common = require("sitegen.common")
h = (word) -> word\sub(1, 1)\upper! h = (word) -> word\sub(1, 1)\upper!
t = (word) -> word\sub(2, -1)\lower! t = (word) -> word\sub(2, -1)\lower!
@ -83,7 +93,34 @@ list_posts = (page, limit) ->
} }
} for { meta: meta, source: source } in *posts[1, limit or #posts]] } for { meta: meta, source: source } in *posts[1, limit or #posts]]
-- list_posts2 = (page) ->
-- posts = page.site\query_pages { is_a: "post" }
-- table.sort posts, (a, b) -> a.source > b.source
--
-- list = [{
-- target: meta.target
-- post_title: meta.title or table.concat(titleize_slug(meta.id), " ")
-- publish_date: publish_date source
-- } for { meta: meta, source: source } in *posts[1, limit or #posts]]
-- p list
-- list
cosmo = require "sitegen.cosmo" cosmo = require "sitegen.cosmo"
list_posts2 = (page, arg={}) ->
posts = page.site\query_pages { is_a: "post" }
table.sort posts, (a, b) -> a.source > b.source
list = [{
target: meta.target
post_title: meta.title or table.concat(titleize_slug(meta.id), " ")
publish_date: publish_date source
blurb: meta.blurb
} for { meta: meta, source: source } in *posts[1, arg.limit or #posts]]
for post in *list
cosmo.yield post
html_renderer = require "sitegen.renderers.html" html_renderer = require "sitegen.renderers.html"
html_renderer.cosmo_helpers.if = (args, has_block) => cosmo.cif args, has_block html_renderer.cosmo_helpers.if = (args, has_block) => cosmo.cif args, has_block
html_renderer.cosmo_helpers.titleize = (slug) => html.build -> html_renderer.cosmo_helpers.titleize = (slug) => html.build ->
@ -94,6 +131,7 @@ sitegen.create =>
@app_name = "stasis" @app_name = "stasis"
@version = "0.2.12" @version = "0.2.12"
@list_posts = list_posts @list_posts = list_posts
@list_posts2 = list_posts2
add "index.html", title: "Catalin Mititiuc" add "index.html", title: "Catalin Mititiuc"
add "blog.html", title: "Posts", target: "posts/index", template: "blog" add "blog.html", title: "Posts", target: "posts/index", template: "blog"