From 7c655a95058aa753355251eb78b83d31d44972ab Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 23 Aug 2022 14:07:20 +0200 Subject: Better navigation of related content --- scripts/lib/common.lua | 34 +++++++++++++++++++++------------- scripts/metadata.lua | 13 ++++--------- scripts/page.lua | 11 ++++++++++- 3 files changed, 35 insertions(+), 23 deletions(-) (limited to 'scripts') diff --git a/scripts/lib/common.lua b/scripts/lib/common.lua index ee7c6aa..d3c6c44 100644 --- a/scripts/lib/common.lua +++ b/scripts/lib/common.lua @@ -1,15 +1,4 @@ -function dump(o) - if type(o) == 'table' then - local s = '{ ' - for k, v in pairs(o) do - if type(k) ~= 'number' then k = '"' .. k .. '"' end - s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' - end - return s .. '} ' - else - return tostring(o) - end -end +local utils = require 'pandoc.utils' function string.split(str, sep) sep = sep or '%s' @@ -82,6 +71,25 @@ function pandoc.List:shuffle() return self end +function dump(o) + if type(o) == 'table' then + local s = '{ ' + for k, v in pairs(o) do + if type(k) ~= 'number' then k = '"' .. k .. '"' end + s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' + end + return s .. '} ' + else + return tostring(o) + end +end + +function prep_layout(layout) + layout = utils.stringify(layout) + return { id = layout, ["is_" .. layout] = true } +end + return { - dump = dump + dump = dump, + prep_layout = prep_layout } diff --git a/scripts/metadata.lua b/scripts/metadata.lua index 9c8a964..843ed3c 100644 --- a/scripts/metadata.lua +++ b/scripts/metadata.lua @@ -42,11 +42,6 @@ function resolve_layout(depth) return layout end -function prep_layout(layout) - layout = utils.stringify(layout) - return { id = layout, ["is_" .. layout] = true } -end - function prep_namespace(namespace) namespace = utils.stringify(namespace) @@ -138,11 +133,11 @@ function process_base(build, meta) meta.title = meta.title and utils.stringify(meta.title) or "" meta.slug = slug(meta.title) meta.schema_type = meta.schema_type or "CreativeWork" - meta.list_layout = prep_layout(meta.list_layout or "list") + meta.list_layout = common.prep_layout(meta.list_layout or "list") 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 make_absolute("thumbnail." .. utils.stringify(meta.thumbnail), meta.file_out) - meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or resolve_layout(meta.depth)) + meta.layout = common.prep_layout(meta.layout or (meta.redirect and "redirect") or resolve_layout(meta.depth)) meta.show_date = meta.parent and meta.parent.list_order == "date_desc" meta.category = meta.parent and meta.parent.title @@ -179,8 +174,8 @@ function find_pos(meta) 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 + meta.prev = index <= #pages - 1 and pages[index + 1] or nil + meta.next = index >= 2 and pages[index - 1] or nil end function Meta(meta) diff --git a/scripts/page.lua b/scripts/page.lua index 7378bfc..48eef6e 100644 --- a/scripts/page.lua +++ b/scripts/page.lua @@ -144,11 +144,20 @@ function generate_related(meta) 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 meta.prev then meta.prev.is_prev = true end + if meta.next then meta.next.is_next = true end + if not meta.prev and not meta.next then return nil end + local layout = meta.parent.list_layout + local layout_id = utils.stringify(layout.id) + if layout_id ~= "grid-2" and layout_id ~= "gallery-2" then + layout = common.prep_layout("grid-2") + end + return { url = meta.parent.url, - layout = meta.parent.list_layout, + layout = layout, prev = meta.prev, next = meta.next } -- cgit v1.2.3-54-g00ecf