~technomancy/fennel-lang.org

ref: 720592c61574fec92d38bf2f884b92d0368c4acc fennel-lang.org/markdown-to-website.lua -rw-r--r-- 1.0 KiB
720592c6Phil Hagelberg august user group 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
local title

local function warn(msg)
  io.stderr:write(("%s\n"):format(msg))
end

-- Set title from level 1 headers, unless it has been set before.
local function promote_header (header)
  if header.level >= 2 then
    return header
  end

  if not title then
    title = header.content
    return {}
  end

  local msg = '[WARNING] title already set; demoting header "%s"'
  warn(msg:format(pandoc.utils.stringify(header)))
  header.level = header.level + 1
  return header
end

local function link_strip_md_ext_from_link_tgt(link)
  if not link.target:match("^https?") then
    local old_target = link.target
    link.target = link.target:gsub("%.md$", "")
    if os.getenv('PANDOC_FILTER_VERBOSE') and old_target ~= link.target then
      warn(('[DEBUG] %s -> %s'):format(old_target, link.target))
    end
  end
  return link
end

return {
  {Meta = function (meta) title = meta.title end}, -- init title
  {Header = promote_header, Link = link_strip_md_ext_from_link_tgt},
  {Meta = function (meta) meta.title = title; return meta end}, -- set title
}