Last active
May 1, 2023 14:12
-
-
Save tarleb/1a690d38508d99c88c331f63ce7f6a2c to your computer and use it in GitHub Desktop.
Filter to include Markdown files via code blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 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 |
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
Concluding this dialog for posterity, @tarleb and I conversed offline and he came up with a modification to his lua plugin, and a couple more lua plugins.
include.lua
delink.lua
delink.lua
is shorter and faster thandelink.hs
, and doesn't require Haskell to be installed.no-includes.lua
This
no-includes.lua
filter removes allinclude
blocks. The filter is used when targeting man output.Using
include.lua
would also work, but this filter is faster.I also updated my bash script to use the plugins: