Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding one markdown document in another

Tags:

markdown

Is there a way to embed one markdown (or its sub-flavors - I'm using PanDoc) document in another except for using a jQuery().load(url)?

I'd like to have a document that has eg. main.md, chapter1.md, chapter2.md, with main.md loading chapter1.md and chapter2.md automatically.

main.md will have text in between the two chapters e.g.

main.md:

Some opening text... ... [chapter1.md]  Some additional text... ... [chapter2.md] ... something else. 

So I can't use a cat *.md > final.md approach

like image 979
Tahnoon Pasha Avatar asked Aug 26 '13 07:08

Tahnoon Pasha


People also ask

How do I embed a file in markdown?

You can now attach files, including images, to markdown files while you're editing them in the web. This works just like file attachments in issues and pull requests and supports the same file types. Just drag and drag, click and select, or paste.

Can markdown include other files?

The long answer is yes. :-) Markdown was designed to allow people to write simple, readable text that could be easily converted to a simple HTML markup. It doesn't really do document layout.

How do you create a table of contents in markdown?

Press CTRL + SHIFT + P. Select Markdown: Create Table of Contents.


2 Answers

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.

like image 149
Oliver Matthews Avatar answered Sep 24 '22 16:09

Oliver Matthews


If you don't want the sub chapters inline, you can simply insert links into the main chapter. This way, you don't need a preprocessing step.

As Oliver Matthews has written, you can write your own preprocessing step with perl.

Or you can use an existing preprocessor, like m4 or cpp (The C PreProcessor) which allows including files. cpp probably doesn't cope nicely with C code sections in the documents, though. Especially when there are more include directives in them.

There are also toolchains available on the internet which combine separate mardown documents into something bigger. Unfortunately, I've got currently problems to access the backup of my bookmarks.

And here's a similar question, with answers: Markdown and including multiple files although the answers don't explain how to insert sub chapters in between other text.

like image 37
Sebastian Avatar answered Sep 22 '22 16:09

Sebastian