Skip to content

Instantly share code, notes, and snippets.

@tarleb
Last active May 1, 2023 14:12
Show Gist options
  • Save tarleb/1a690d38508d99c88c331f63ce7f6a2c to your computer and use it in GitHub Desktop.
Save tarleb/1a690d38508d99c88c331f63ce7f6a2c to your computer and use it in GitHub Desktop.
Filter to include Markdown files via code blocks
--- Pandoc Lua filter to include other Markdown files
---
--- Usage: Use a special code block with class `include` to
--- include Markdown files. Each code line is treated as the
--- filename of a Markdown file, parsed as Markdown, and
--- included. Metadata from include files is discarded.
---
--- Example:
---
--- ``` {.include}
--- chapters/introduction.md
--- chapters/methods.md
--- chapters/results.md
--- chapters/discussion.md
--- ```
local List = require 'pandoc.List'
function CodeBlock(cb)
if cb.classes:includes 'include' then
local blocks = List:new()
for line in cb.text:gmatch('[^\n]+') do
if line:sub(1,1) ~= '#' then
local fh = io.open(line)
blocks:extend(pandoc.read (fh:read '*a').blocks)
fh:close()
end
end
return blocks
end
end
@ricopicone
Copy link

This is a very nice little filter -- thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment