From 18321ad1c5c09534e39817d9b75070b5481950bd Mon Sep 17 00:00:00 2001 From: Volpeon Date: Wed, 17 Nov 2021 17:43:40 +0100 Subject: Improved list, made header sticky, removed git link --- scripts/metadata_filter.lua | 101 +++++++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 38 deletions(-) (limited to 'scripts') diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 5a547ee..5fa7dfc 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -9,6 +9,16 @@ function pandoc.List:flatMap(fn) return result end +function pandoc.List:take(n) + if n >= #self then return self end + + local result = pandoc.List() + + for i = 1, n do result:insert(self[i]) end + + return result +end + function format_date(date) if not date then return date end @@ -186,6 +196,8 @@ end function generate_list(meta) if not meta.pages then return nil end + local limit = (meta.list_limit and tonumber(pandoc.utils.stringify(meta.list_limit))) or 9999 + if meta.depth == "1" then return meta.pages.all:map(function(p) return { @@ -200,57 +212,69 @@ function generate_list(meta) end) elseif meta.depth == "2" then return meta.pages.all:map(function(cat) + local allItems = ((cat.pages and cat.pages.all) or pandoc.List()):map(function(p) + return { + title = p.title, + subtitle = p.subtitle, + date = p.date, + url = p.url, + icon = p.icon or cat.icon, + post_icon = cat.list_post_icon or meta.list_post_icon, + indicator = cat.list_read_indicators, + } + end) + local items = allItems:take(limit) + local omitted = #allItems - #items + return { title = cat.title, content = cat.content, url = cat.url, grid = cat.list_grid, - items = ((cat.pages and cat.pages.all) or pandoc.List()):map(function(p) - return { - title = p.title, - subtitle = p.subtitle, - date = p.date, - url = p.url, - icon = p.icon or cat.icon, - post_icon = cat.list_post_icon or meta.list_post_icon, - indicator = cat.list_read_indicators, - } - end), + items = items, + total = tostring(#allItems), + omitted = omitted ~= 0 and tostring(omitted), } end):filter(function(cat) return #cat.items ~= 0 end) elseif meta.depth == "3" then return meta.pages.all:map(function(cat) + local allItems = (cat.pages and cat.pages.all or pandoc.List()):flatMap(function(c) + if c.pages then + return c.pages.all:map(function(p) + return { + title = p.title, + subtitle = p.subtitle, + category = c.title, + url = p.url, + icon = p.icon or c.icon, + post_icon = c.list_post_icon or cat.list_post_icon, + indicator = c.list_read_indicators, + } + end) + else + local l = pandoc.List() + l:insert({ + title = c.title, + subtitle = c.subtitle, + url = c.url, + icon = c.icon or cat.icon, + post_icon = cat.list_post_icon, + indicator = cat.list_read_indicators, + }) + return l + end + end) + local items = allItems:take(limit) + local omitted = #allItems - #items + return { title = cat.title, content = cat.content, url = cat.url, grid = cat.list_grid, - items = (cat.pages and cat.pages.all or pandoc.List()):flatMap(function(c) - if c.pages then - return c.pages.all:map(function(p) - return { - title = p.title, - subtitle = p.subtitle, - category = c.title, - url = p.url, - icon = p.icon or c.icon, - post_icon = c.list_post_icon or cat.list_post_icon, - indicator = c.list_read_indicators, - } - end) - else - local l = pandoc.List() - l:insert({ - title = c.title, - subtitle = c.subtitle, - url = c.url, - icon = c.icon or cat.icon, - post_icon = cat.list_post_icon, - indicator = cat.list_read_indicators, - }) - return l - end - end), + items = items, + total = tostring(#allItems), + omitted = omitted ~= 0 and tostring(omitted), } end):filter(function(cat) return #cat.items ~= 0 end) end @@ -293,7 +317,8 @@ function process(global, parent, meta) meta.pages = process_pages(global, { parent = parent, meta = meta }, meta.list_order, meta.pages) meta.depth = (meta.pages and find_depth(meta.pages.all)) or "0" - meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or resolve_layout(meta.depth)) + meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or + resolve_layout(meta.depth)) if meta.date then meta.date = format_date(meta.date) -- cgit v1.2.3-70-g09d2