Search code examples
luapandoc

Pandoc Lua : how to add a markdown block around a header without losing the markdown syntax #


I am trying to add a div around a markdown header in a Lua filter, but the # in front of the title disappear in the output.

Header = function(el)
    if el.level == 1 then
        local content = el.content
        local pre = pandoc.RawBlock('markdown','::: test')
        local post = pandoc.RawBlock('markdown',':::')
        table.insert(content,1,pre)
        table.insert(content, post)
        return content
    else
        return el
    end
end

Input:

# Linux
## Support for Linux users
Create a shell script


Expected Output

::: test
# Linux
:::
## Support for Linux users
Create a shell script

Solution

  • The content field contains the heading text, but the heading itself is the el element that's not returned. Returning it together with the raw blocks should work though:

    return {
      pre, el, post
    }
    

    Or use a Div element:

    function Header (el)
      if h.level == 1 then
        return pandoc.Div(el, {class = 'test'})
      end
    end