How to include a HTML file inside a markdown file in jekyll?

☆樱花仙子☆ 提交于 2019-12-21 04:27:18

问题


I have a jekyll site with filestructure like so:

▾ _includes/
    post-entry.html
▾ _posts/
    2012-11-25-first-post.markdown
index.markdown

In my index.markdown, I want to include a post-entry.html like this:

{% for post in site.posts %}
* {% include post-entry.html %}
{% endfor %}

But this appears as a HTML code snippet in the blog. How can i prevent the HTML from being protected?


回答1:


You can use liquid tags in any markdown file so long as it has YAML header matter. For instance, check out the index.md given in the jekyllbootstrap template for Jekyll sites.

If you link to your actual index.markdown or your repository itself (e.g. if it's on github), we could probably get a better idea of what has gone wrong. Your example should work, though you might need to use HTML list element <li> rather than the markdown *.




回答2:


The problem is the parser views the HTML as code blocks. The best way would be to turn off code blocks like I asked in this question

To work around use the replace tag:

{% capture includeGuts %}
{% include post-entry.html %} 
{% endcapture %}
{{ includeGuts | replace: '    ', ''}}



回答3:


Use {{ post.content }} instead.

Here's the code I use on my website as an example:

{% for post in site.posts %}
    <a href="{{ post.url }}">
        <h2>{{ post.title }} &mdash; {{ post.date | date_to_string }}</h2>
    </a>
    {{ post.content }}
{% endfor %}



回答4:


You can't. Markdown files are just text files with styling. If you want to use the Liquid tags you will have to do so in your _layouts

If I am guessing right you want this markdown file on your index page? So in the default.html layout after the {{ content }} tags you could then use the liquid tags you want to use. There is no need to mix the markdown files with code.



来源:https://stackoverflow.com/questions/13549690/how-to-include-a-html-file-inside-a-markdown-file-in-jekyll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!