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 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'scripts/metadata.lua') 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 -- cgit v1.2.3-54-g00ecf