local utils = require 'pandoc.utils' function page_sort(order, pages) order = order and utils.stringify(order) return function(ref1, ref2) local p1 = pages and pages[utils.stringify(ref1)] or ref1 local p2 = pages and pages[utils.stringify(ref2)] or ref2 if p1.position then if p2.position then return tonumber(utils.stringify(p1.position)) < tonumber(utils.stringify(p2.position)) else return true end elseif p2.position then return false elseif order == "date_desc" then if p1.last_update then if p2.last_update then return utils.stringify(p1.last_update.yyyy_mm_dd) > utils.stringify(p2.last_update.yyyy_mm_dd) else return true end else return false end else return utils.stringify(p1.title):upper() < utils.stringify(p2.title):upper() end end end return { page_sort = page_sort }