Jekyll on GitHub Pages: include markdown in another markdown file
Asked Answered
M

4

31

I'm using kramdown with Jekyll on GitHub Pages. I have a piece of markdown that I want to use twice, and don't want to maintain two versions of it. How can I include this in other markdown files?

For example, I want to type something like

{: include otherFile.md }

and have the contents of otherFile.md rendered directly where this command is. Note that the parent file is also a markdown file, and not an HTML template.

Additional consideration: it would be great to be able to do this with the default GitHub pages workflow, and not have to use a plugin that is disabled by GH pages and have to push compiled versions of the site manually.

Melli answered 31/1, 2017 at 20:11 Comment(0)
A
43

The way to go seems to be include_relative. I found this answer by user geraldb on the old Jekyll forums:

Yes, there's a simple way. It works "out-of-the-box" in Jekyll (and GitHub Pages). Just tried it. See the Vienna.html test page e.g.:

---
layout: default
---

some text here

{% include_relative test_header.md %}

some more text here

{% include_relative test_footer.md %}

See the source -> test.md and the live page.

The "trick" if you want to call it so - is to use include_relative if you want to have the building block texts (e.g. intro.md, explanation.md, conclusion.md, etc.) along with your page (in the same folder). Cheers. Happy Jekylling.


Since I've first written this answer, the original forum post seems to be completely gone and I haven't found it archived on archive.org. The answer link on the old forum was this (now points to an unrelated post), and the profile of geraldb in the new forum is here.

The first link in the quoted answer has been updated to point to the correct file in the repo as it exists today,1 and the second link is dead but left unchanged as I'm quoting the original author here.


1Thanks to Ben Leggiero for finding it!

Aristotle answered 31/1, 2017 at 20:33 Comment(1)
include_relative worked for me, too. Although the example code on the Jekyll tutorial page only shows the case for embedding a .html file, it also works for .md files.Dotdotage
B
20

For me, the problem couldn't be solved with importing by include_relative and using the normal include does not render the included Markdown files.

But capturing the include and then piping it through markdownify is a pretty good workaround:

{% capture my_include %}{% include a_markdown_file.md %}{% endcapture %}
{{ my_include | markdownify }}

I found this in GH:/jekyll/jekyll#1303 (comment). That's a feature request for avoiding the capturing. It's closed, unfortunately.

Blame answered 23/8, 2019 at 16:7 Comment(0)
F
0

There's a Ruby gem markdown_helper that supports include files.

Fajardo answered 24/3, 2018 at 14:51 Comment(0)
H
0

I added:

---
layout: default
---

to the top of the .html file and {% include my_partial.html %} finally worked.

Handcuff answered 18/3, 2022 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.