Of course! This is a very common and useful requirement for managing documentation.
The short answer is that standard Markdown does not have a native feature to include content from another file.
However, almost every tool and platform that uses Markdown (like static site generators, documentation tools, and even some note-taking apps) provides a way to do this. This is often called "transclusion," "includes," or "partials."
Here’s a breakdown of how to do it in various popular environments.
1. Static Site Generators (The most common solution)
If you are building a website or documentation, you are likely using a Static Site Generator (SSG). This is the most powerful and common way to handle includes.
Jekyll (used by GitHub Pages)
Jekyll uses the Liquid templating language.
Create a file to be included, for example, _includes/my_content.md.
In your main Markdown file, use the include tag:
markdown
# My Main Document
Here is some content.
{% include_relative my_content.md %}
And here is some more content.
Or, if the file is in the standard _includes directory:
markdown
{% include my_content.md %}
Hugo
Hugo uses Go templates and provides powerful "shortcodes" for this.
Let's say you have content/posts/my-post.md and you want to include content/snippets/reusable-warning.md.
Create a shortcode in layouts/shortcodes/include.html:
Then, in your my-post.md, you can call it like this:
markdown
# My Post Title
Here is a standard warning.
{{< include "snippets/reusable-warning.md" >}}
MkDocs
MkDocs requires a plugin, the most popular being mkdocs-include-markdown-plugin.
Install the plugin:
bash
pip install mkdocs-include-markdown-plugin
Enable it in your mkdocs.yml file:
yaml
plugins:
- search
- include-markdown
Use the custom syntax in your Markdown file:
markdown
# Main Page
Here is some shared documentation.
{!include "shared/disclaimer.md"!}
MDX (Docusaurus, Next.js, etc.)
MDX blends Markdown and JSX, allowing you to import content like a JavaScript component.
Your content to be included (_shared_message.mdx):
mdx
export const SharedMessage = () => (
<p>
This is a shared message that can be reused everywhere!
</p>
);
Your main file (my-page.mdx):
mdx
import { SharedMessage } from './_shared_message.mdx';
# My Page
This page needs to show a shared message.
<SharedMessage />
The page continues here...
2. Pandoc (Command-line document converter)
If you are using Pandoc to convert Markdown files to PDF, HTML, or other formats, you can simply list the files in order.
bash
# This will concatenate file1.md and file2.md before converting
pandoc file1.md reusable_content.md file2.md -o output.pdf
For more complex includes, you can use a filter like pandoc-include-code or panflute.
3. Obsidian (Note-taking app)
Obsidian has first-class support for transclusion. It's one of its core features.
To embed an entire file, use the ![[filename]] syntax.
markdown
# My Daily Note
Here are my thoughts for today.
Below is my standard weekly goal list.
![[My Weekly Goals]]
You can even embed a specific header or block from another file:
![[My Weekly Goals#Monday]] (embeds the "Monday" section)
![[My Weekly Goals#^d2e8e4]] (embeds a specific block)
4. GitHub README.md files (The "Tricky" One)
You cannot directly include another Markdown file in a README.md that is rendered on GitHub. GitHub's renderer is fast and simple; it does not process includes.
The Workaround: Use a CI/CD pipeline like GitHub Actions to build your README.md from multiple source files.
Create your partial files, e.g., docs/_header.md, docs/_body.md, docs/_footer.md.
Create a GitHub Actions workflow file, e.g., .github/workflows/build-readme.yml:
yaml
name: Build README
on:
push:
branches:
- main # Or your default branch
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@v3
- name: Concatenate README parts
run: |
cat docs/_header.md docs/_body.md docs/_footer.md > README.md
- name: Commit and push if changed
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "docs: auto-generate README.md"
file_pattern: README.md
This action will automatically combine your partial files into the main README.md and commit it back to the repository whenever you push changes.
Summary Table
Tool / Platform
Syntax / Method
Notes
Standard Markdown
Not Supported
Markdown itself has no include syntax.
Jekyll / GitHub Pages
{% include_relative file.md %}
Uses Liquid templating. The standard for GitHub Pages sites.
Hugo
{{< include "path/to/file.md" >}} (with a shortcode)
Very fast and flexible. Requires creating a simple shortcode file.
MkDocs
{!include "path/to/file.md"!}
Requires installing and enabling the mkdocs-include-markdown-plugin.
MDX (Docusaurus, etc)
import and use as a component: <MyContent />
Blends Markdown with JSX. Modern and very powerful.
Pandoc
pandoc file1.md include.md file2.md -o out.pdf
Combines files at build time via the command line.
Obsidian
![[filename]]
Core feature for personal knowledge management. Very easy to use.
GitHub READMEs
Not Supported Directly. Use GitHub Actions to build the file.
The final README.md is a static, generated file in your repo.