From bd42fbff628ca5831d7a0f9b18e3466bd6dbe8a5 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sun, 21 Aug 2022 18:33:02 +0200 Subject: Don't use random pages for "related" --- scripts/metadata.lua | 20 ++++++++++++++++++++ scripts/page.lua | 41 ++++++++++++++++++++++------------------- templates/partials/related.html | 18 ++++++++++++------ 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/scripts/metadata.lua b/scripts/metadata.lua index 49a9c55..9c8a964 100644 --- a/scripts/metadata.lua +++ b/scripts/metadata.lua @@ -130,6 +130,7 @@ end function process_base(build, meta) meta.namespace = prep_namespace(meta.namespace) + meta.id = meta.namespace.full:gsub("^.*/(.-)$", "%1") meta.file_out = utils.stringify(meta.file_out) meta.unlisted = meta.unlisted or (meta.draft and build.mode ~= "dev") meta.redirect = meta.url and true @@ -164,6 +165,24 @@ function process(global, build, meta) resolve_dates(meta) end +function find_pos(meta) + for _, p in pairs(meta.pages.by_id) do + p.parent = meta + find_pos(p) + p.parent = nil + end + + if not meta.parent then return end + + local pages = meta.parent.list_order == "date_desc" and meta.parent.pages.date_desc or meta.parent.pages.all + local _, index = pages:find(meta.id) + + if index == nil then return end + + meta.prev = index >= 2 and pages[index - 1] or nil + meta.next = index <= #pages - 1 and pages[index + 1] or nil +end + function Meta(meta) local build = {} @@ -180,6 +199,7 @@ function Meta(meta) find_depth(meta) process(meta, build, meta) + find_pos(meta) return meta end diff --git a/scripts/page.lua b/scripts/page.lua index dcbb36f..7378bfc 100644 --- a/scripts/page.lua +++ b/scripts/page.lua @@ -130,32 +130,35 @@ function generate_list(meta) end end +function deref_page(pages) + return function(ref) + return pages[utils.stringify(ref)] + end +end + function generate_related(meta) if not meta.parent then return nil end - meta = meta.parent + local deref = deref_page(meta.parent.pages.by_id) - local items = meta.pages.random:take(2):map(function (p) return d1_page_to_list_item(p) end) + meta.prev = meta.prev and d1_page_to_list_item(deref(meta.prev)) + meta.next = meta.next and d1_page_to_list_item(deref(meta.next)) - if #items == 0 then return nil end + if not meta.prev and not meta.next then return nil end return { - url = meta.url, - layout = meta.list_layout, - items = items + url = meta.parent.url, + layout = meta.parent.list_layout, + prev = meta.prev, + next = meta.next } end -function deref_page(pages) - return function(ref) - return pages[utils.stringify(ref)] - end -end - function process_pages(meta) - meta.pages.all = meta.pages.all:filterMap(deref_page(meta.pages.by_id)) - meta.pages.date_desc = meta.pages.date_desc:filterMap(deref_page(meta.pages.by_id)) - meta.pages.random = meta.pages.all :clone():shuffle() + local deref = deref_page(meta.pages.by_id) + + meta.pages.all = meta.pages.all:filterMap(deref) + meta.pages.date_desc = meta.pages.date_desc:filterMap(deref) end function resolve_urls(global, build, meta) @@ -179,6 +182,7 @@ function resolve_urls(global, build, meta) end function process_base(meta) + meta.id = utils.stringify(meta.id) meta.depth = tonumber(utils.stringify(meta.depth)) meta.file_out = utils.stringify(meta.file_out) meta.url = utils.stringify(meta.url) @@ -187,6 +191,8 @@ function process_base(meta) meta.list_order = meta.list_order and utils.stringify(meta.list_order) meta.position = meta.position and tonumber(utils.stringify(meta.position)) meta.thumbnail = meta.thumbnail and utils.stringify(meta.thumbnail) + meta.prev = meta.prev and utils.stringify(meta.prev) + meta.next = meta.next and utils.stringify(meta.next) end function process(global, build, meta, dir) @@ -207,7 +213,6 @@ function process(global, build, meta, dir) end function prune_pages(meta, up, down) - meta.pages.random = pandoc.List() meta.pages.by_id = {} if down == 0 then @@ -231,8 +236,6 @@ function prune(meta, up, down) end function Meta(meta) - math.randomseed(os.time()) - local build = {} for key, value in pairs(meta) do @@ -254,7 +257,7 @@ function Meta(meta) meta = meta.tree - local parts = utils.stringify(build.namespace):split('/'):skip(1) + local parts = build.namespace:split('/'):skip(1) for i = 1, #parts do local part = parts[i] local p = meta.pages.by_id[part] diff --git a/templates/partials/related.html b/templates/partials/related.html index b0d3f61..7d29c7a 100644 --- a/templates/partials/related.html +++ b/templates/partials/related.html @@ -14,19 +14,25 @@ $if(related)$
$if(related.layout.is_grid-2)$ - $related.items:partials/grid_card()$ + $related.prev:partials/grid_card()$ + $related.next:partials/grid_card()$ $elseif(related.layout.is_grid-3)$ - $related.items:partials/grid_card()$ + $related.prev:partials/grid_card()$ + $related.next:partials/grid_card()$ $elseif(related.layout.is_gallery-2)$ - $related.items:partials/gallery_card()$ + $related.prev:partials/gallery_card()$ + $related.next:partials/gallery_card()$ $elseif(related.layout.is_gallery-3)$ - $related.items:partials/gallery_card()$ + $related.prev:partials/gallery_card()$ + $related.next:partials/gallery_card()$ $else$
- $related.items:partials/list_card()$ + $related.prev:partials/list_card()$ + $related.next:partials/list_card()$
- $related.items:partials/grid_card()$ + $related.prev:partials/grid_card()$ + $related.next:partials/grid_card()$
$endif$
-- cgit v1.2.3-70-g09d2