markdownpandoc

Use fenced code blocks in pandoc markdown output


Is there a way to make pandoc output fenced code blocks (i.e. using triple backticks) when outputting markdown, rather than indenting with four spaces?


Solution

  • One can use a Lua filter to "manually" create the block output:

    local fenced = '```\n%s\n```\n'
    function CodeBlock (cb)
      -- use pandoc's default behavior if the block has classes or attribs
      if cb.classes[1] or cb.attributes[1] then
        return nil
      end
      return pandoc.RawBlock('markdown', fenced:format(cb.text))
    end
    

    Save the above to a file, then pass the file to pandoc via the --lua-filter option.