Markdown Include

0.8.1 · active · verified Thu Apr 16

Markdown-Include is a Python-Markdown extension that enables the inclusion of content from other Markdown files within a main document. It facilitates modular documentation by replacing a special syntax, `{!filename!}`, with the contents of the specified file. The library is currently at version 0.8.1 and is actively maintained, with its last release in February 2023.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up `markdown-include` to process a main Markdown file that includes another partial Markdown file. It creates temporary files, initializes the Markdown parser with the `MarkdownInclude` extension, and converts the content to HTML, showing how the `base_path` configuration is used to resolve included file paths.

import markdown
from markdown_include.include import MarkdownInclude
import os

# Create a dummy base directory and included file
if not os.path.exists('docs'):
    os.makedirs('docs')
with open('docs/main.md', 'w') as f:
    f.write('# Main Document\n\nThis is the main content.\n\n{!partial.md!}\n\nEnd of document.')
with open('docs/partial.md', 'w') as f:
    f.write('## Included Section\n\nThis content comes from `partial.md`.')

# Configure the MarkdownInclude extension
# base_path specifies the directory where included files are sought
configs = {
    'base_path': 'docs'
}

# Initialize Markdown with the extension
md_instance = markdown.Markdown(extensions=[MarkdownInclude(configs=configs)])

# Read the main Markdown content
with open('docs/main.md', 'r') as f:
    markdown_text = f.read()

# Convert Markdown to HTML
html = md_instance.convert(markdown_text)

print(html)

# Expected output (simplified):
# <h1>Main Document</h1>
# <p>This is the main content.</p>
# <h2>Included Section</h2>
# <p>This content comes from <code>partial.md</code>.</p>
# <p>End of document.</p>

view raw JSON →