GitHub Pages上的Jekyll:在另一个markdown文件中包含markdown

28

我正在使用 kramdown 与 GitHub Pages 上的 Jekyll。我有一段 Markdown,想要在两个地方使用,而不想维护两个版本。如何将它包含在其他 Markdown 文件中?

例如,我想输入类似以下内容:

{: include otherFile.md }

请注意,父文件也是Markdown文件,而不是HTML模板。并且要使otherFile.md的内容直接在该命令所在处呈现。

另外需要考虑一点:最好能够使用默认的GitHub pages工作流程来实现此操作,而不必使用一个被GH pages禁用并且需要手动推送站点编译版本的插件。

4个回答

38
走的方法似乎是 include_relative。我在旧的 Jekyll论坛 上找到了用户gerald b的答案。

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.


自从我最初写下这个答案以来,原始论坛帖子似乎已经完全消失了,并且我没有在archive.org上找到它的存档。旧论坛中的回答链接是this(现在指向一个无关的帖子),新论坛中geraldb的个人资料在这里
引用答案中的第一个链接已更新为指向仓库中正确的文件,因为它今天存在1,第二个链接已失效,但未更改,因为我在引用原作者。

1感谢Ben Leggiero的发现!


3
include_relative 对我也起作用。尽管 Jekyll 教程页面上的示例代码仅展示了嵌入 .html 文件的情况,但它也适用于 .md 文件。 - nglee

19

对我来说,问题无法通过使用include_relative导入并使用normalinclude来渲染包含的Markdown文件来解决。

但是捕获包含内容然后通过markdownify进行处理是一个相当不错的解决方法:

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

我在GH:/jekyll/jekyll#1303 (comment)中找到了这个。那是一个避免捕获的功能请求。不幸的是,它已经关闭了。


0

0

我添加了:

---
layout: default
---

{% include my_partial.html %}代码放到html文件的顶部,最终成功了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接