41

Is there a way to include an HTML partial from a Markdown file with Jekyll?

Example:

File index.md:

---
layout: default
title: Home
---

This is a [Markdown](http://daringfireball.net/projects/markdown/) file.

{% include foobar.html %}

File _includes/foobar.html:

<ul>
    <li>Foo</li>
    <li>Bar</li>
</ul>

This unfortunately does not seem to work in my case.

For completeness, here is the entire content of my _config.yml file:

encoding: utf-8
markdown: kramdown
baseurl: 
François Beaune
  • 4,270
  • 7
  • 41
  • 65
  • 2
    No problemo. Just do it ! – David Jacquel Jan 19 '15 at 18:13
  • 1
    Doesn't seem to work :) The `
      ` and `
    ` tags appear as plain text while the rest appears as code since it's indented with four spaces.
    – François Beaune Jan 19 '15 at 18:15
  • I updated my question to include the content of my `_config.yml` file. – François Beaune Jan 19 '15 at 18:18
  • Actually, you were right: raw HTML in Markdown files just work. I had other issues. Thanks for your help! – François Beaune Jan 20 '15 at 12:34
  • Then maybe you can approve my answer. – David Jacquel Jan 20 '15 at 12:51
  • I'm not sure you understand your answer. You suggest to unindent my list, but it wasn't indented in the first place. Since that wasn't the problem, I don't think it's a good idea to mark it as the actual answer to my question. What about changing your answer to "No problemo. Just do it!" and then I'll accept it, since that was absolutely the correct answer :) – François Beaune Jan 20 '15 at 17:35
  • That was a comment not an answer. The fact that you `ul` was indented four spaces was the problem. In markdown `line break + four space = pre>code` that's how I reproduced it. – David Jacquel Jan 20 '15 at 17:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69245/discussion-between-francois-beaune-and-david-jacquel). – François Beaune Jan 20 '15 at 19:15

2 Answers2

38

A common reason the html shows up as plain text is when the html snippet is indented with at least four spaces.

This causes jekyll to interpret the html as a code block that is to be displayed literally.

(I know this was already mentioned in the comments, but it took me a while to find and understand that I had the exact same problem)

Johan Gorter
  • 1,253
  • 10
  • 13
10

If the .md file you mentioned is located in _posts and the layout type is post, you can use markdown="0" or "1" to set related part as Markdown or HTML as you like because you configurated markdown to kramdown. Try following code:

---
layout: post
title: Home
---

# Markdown part

This is a [Markdown](http://daringfireball.net/projects/markdown/) file.

<section id="categories" markdown="1">

A list of categories:

- foo
- bar

</section>

<div id="html" markdown="0">
<h1>HTML part</h1>

  <ul>
    <li>Foo</li>
    <li>Bar</li>
  </ul>

</div>

If the .md file you mentioned is located in _includes, _layouts or page, you can directly use your code or change the layout type to page:

---
layout: default
title: Home
---

This is a [Markdown](http://daringfireball.net/projects/markdown/) file.

{% include foobar.html %}

See an example here: https://raw.githubusercontent.com/plusjade/jekyll-bootstrap/master/index.md.

Just enjoy.

Bravo Yeung
  • 8,654
  • 5
  • 38
  • 45