Path: blob/main/build-assets/prefix-path.lua
898 views
pathPrefixMetaName = "pathToProjectRoot"12-- Global variable to store path prefix once it has been3-- extracted from the metadata4pathPrefix = nil56-- Traversal should run top down, in order for the7-- Meta function to be called before Link or Image8-- functions. Otherwise the path prefix is not known9-- when the Link/Image functions are run.10traverse = 'topdown'1112-- Utility function with prepends paths with the path prefix13local function prefix_path(p)14return pandoc.path.normalize(pathPrefix .. "/" .. p)15end1617-- Adjust absolute link targets18function Link(l)19if not pathPrefix then return end20if pandoc.path.is_absolute(l.target) then21local new_link = l:clone()22new_link.target = prefix_path(l.target)23return new_link24end25end2627-- Adjust absolute src links of images28function Image(i)29if not pathPrefix then return end30if pandoc.path.is_absolute(i.src) then31local new_img = i:clone()32new_img.src = prefix_path(i.src)33return new_img34end35end3637-- Optimize traversal, stop if no meta variable available38function Blocks(p)39if not pathPrefix then return nil, false end40end4142-- Extract required meta variable43function Meta(m)44local meta_var = m[pathPrefixMetaName]45if not meta_var then return nil, false end46if type(meta_var) == "string" then47-- When specified via command line option -M, then48-- the meta variable will be a bare string49pathPrefix = meta_var50elseif type(meta_var) == "table"51and type(meta_var[1]) == "userdata"52and pandoc.utils.type(meta_var[1]) == "Inline" then53-- When specified via YAML, then the meta variable54-- will be a pandoc [Str "path/"]55pathPrefix = meta_var[1].text56end57end585960