Reputation: 129
Can I somehow include a file into a file in mkdocs? I do not want to copy paste the same sections again and again. Thanks
Upvotes: 5
Views: 5681
Reputation: 3839
@doktoric is correct: to set the record straight, there is a solution in MkDocs for including external files, which involves using the markdown-include extension.
The syntax is the following:
{!filename.md!}
The procedure for achieving this is:
pip install markdown-include
)mkdocs.yml
file, to register this extension for your website sees explanations, and below.In the mkdocs.yml
file:
markdown_extensions:
include
The solution I would suggest at this stage is through mkdocs-macros plugin (which I wrote). The plugin implements a jinja2 template engine, which means that the {% include ... %}
statement is available out of the box:
## Paragraph
{% include 'snippet.md' %}
Just install the plugin, declare it in mkdocs.yml
, and you are good to go.
Upvotes: 10
Reputation: 42607
Including one document in another is not a Markdown feature, and currently MkDocs only does Markdown processing on Markdown documents. Therefore, at this time there is no way to do this with MkDocs.
Interestingly, it does not even appear that anyone has made such a feature request. There is the pending feature to make various variables available to be displayed within a Markdown page (see issue #304), but no mention is made there of including other documents. That said, depending on the solution chosen, its possible that if/when the issue is addressed it will also provide for the ability to include other pages.
Either way, if/when a Plugin API is added (see issue #206) you should be able to do any prepossessing of your own which would include running the Markdown text through a template system which support "including" pages before the Markdown is parsed. In fact, I expect that if you were to make a feature request, the answer would be to punt it to a Plugin.
Upvotes: 1