Embedding one markdown document in another

Markdown by itself lacks a notation for including files, which rather screws that.

pandoc has an example on using a custom haskell filter on code blocks to include files but this a. leaves a code block around the text and (more importantly) b. doesn’t parse the new file as markdown, and frankly my haskell isn’t up to the task of fixing that.

However, you can achieve this by doing a pre-process pass, using perl*. I’m assuming that each include is on a line by itself of the form shown above.

perl -ne 's/^\[(.+)\].*/`cat $1`/e;print' main.md > final.md

*I dislike having to resort to perl, but sed lacks the ability to read from a file using a match pattern to determine the name.

Leave a Comment