Markdown Extensions¶
MkDocs supports Python-Markdown extensions that enhance the standard Markdown syntax with additional features and capabilities.
Overview¶
Markdown extensions provide:
- Enhanced syntax - Additional markup options
- Better formatting - Tables, footnotes, and more
- Meta information - Document metadata
- Smart processing - Automatic conversions
- HTML integration - Mix Markdown with HTML
- Custom elements - Extend with your own syntax
Core Extensions¶
Table of Contents¶
Generate automatic table of contents:
Features:
- permalink
- Add anchor links to headings
- title
- TOC section title
- toc_depth
- Maximum heading level
- slugify
- Custom slug function
Tables¶
Enhanced table support:
Example:
| Header 1 | Header 2 | Header 3 |
|----------|----------|----------|
| Cell 1 | Cell 2 | Cell 3 |
| Cell 4 | Cell 5 | Cell 6 |
Abbreviations¶
Define abbreviations with tooltips:
Usage:
The HTML specification is maintained by the W3C.
*[HTML]: Hyper Text Markup Language
*[W3C]: World Wide Web Consortium
Result: The HTML specification is maintained by the W3C.
Admonition¶
Create callout boxes:
See the Admonitions page for detailed usage.
Attribute Lists¶
Add HTML attributes to elements:
Examples:
# Heading with ID {#custom-id}
[Link with class](https://example.com){.external-link}
<img loading="lazy" src="image.jpg" alt="Image with attributes">{width="300" height="200"}
Paragraph with custom class.
{.custom-paragraph}
Definition Lists¶
Create term definitions:
Example:
Footnotes¶
Add footnotes to your content:
Example:
This is a statement[^1] that needs a citation[^2].
[^1]: This is the first footnote.
[^2]: This is the second footnote.
Meta-Data¶
Add YAML front matter:
Example:
---
title: Page Title
author: John Doe
date: 2024-01-15
tags: [python, tutorial]
---
# Document content starts here
Markdown in HTML¶
Enable Markdown inside HTML:
Example:
Python-Markdown Extensions¶
Extra¶
Bundle of useful extensions:
Includes:
- abbr
- attr_list
- def_list
- footnotes
- md_in_html
- tables
CodeHilite (Deprecated)¶
For syntax highlighting (use Pygments instead):
Fenced Code Blocks¶
Enable GitHub-style code blocks:
Smart Strong¶
Improve bold/emphasis handling:
SmartyPants¶
Convert quotes and dashes:
Configuration¶
Basic Setup¶
# mkdocs.yml
markdown_extensions:
# Python Markdown
- abbr
- admonition
- attr_list
- def_list
- footnotes
- meta
- md_in_html
- toc:
permalink: true
- tables
# Python Markdown Extensions
- pymdownx.arithmatex:
generic: true
- pymdownx.betterem
- pymdownx.caret
- pymdownx.mark
- pymdownx.tilde
- pymdownx.details
- pymdownx.emoji
- pymdownx.highlight
- pymdownx.superfences
- pymdownx.tabbed:
alternate_style: true
- pymdownx.tasklist:
custom_checkbox: true
Advanced Configuration¶
markdown_extensions:
# TOC with custom slugify
- toc:
permalink: true
slugify: !!python/object/apply:pymdownx.slugs.slugify
kwds:
case: lower
toc_depth: 3
# Highlight with line numbers
- pymdownx.highlight:
anchor_linenums: true
line_spans: __span
pygments_lang_class: true
# SuperFences with custom handlers
- pymdownx.superfences:
custom_fences:
- name: mermaid
class: mermaid
format: !!python/name:pymdownx.superfences.fence_code_format
- name: math
class: arithmatex
format: !!python/object/apply:pymdownx.arithmatex.arithmatex_fence_format
# Emoji with custom icons
- pymdownx.emoji:
emoji_index: !!python/name:material.extensions.emoji.twemoji
emoji_generator: !!python/name:material.extensions.emoji.to_svg
options:
custom_icons:
- overrides/.icons
Extension Categories¶
Text Processing¶
Extension | Purpose | Example |
---|---|---|
betterem |
Better emphasis | **_bold italic_** |
caret |
Superscript | x^2^ |
mark |
Highlight | ==marked== |
tilde |
Subscript/strike | H~2~O , ~~deleted~~ |
smartsymbols |
Smart symbols | (c) → © |
keys |
Keyboard keys | ++ctrl+c++ |
Content Organization¶
Extension | Purpose | Features |
---|---|---|
tabbed |
Content tabs | Organize alternatives |
details |
Collapsible | Hide/show content |
snippets |
Include files | Reuse content |
blocks |
Block elements | Custom containers |
Code Features¶
Extension | Purpose | Configuration |
---|---|---|
highlight |
Syntax highlighting | Line numbers, highlighting |
inlinehilite |
Inline code | Language-specific |
superfences |
Nested blocks | Custom processors |
striphtml |
Remove HTML | Security feature |
Custom Extensions¶
Creating Extensions¶
Basic extension structure:
# my_extension.py
from markdown import Extension
from markdown.preprocessors import Preprocessor
class MyPreprocessor(Preprocessor):
def run(self, lines):
new_lines = []
for line in lines:
# Process line
new_lines.append(line)
return new_lines
class MyExtension(Extension):
def extendMarkdown(self, md):
md.preprocessors.register(
MyPreprocessor(md),
'my_preprocessor',
priority=25
)
def makeExtension(**kwargs):
return MyExtension(**kwargs)
Using Custom Extensions¶
Best Practices¶
1. Start Simple¶
Begin with essential extensions:
2. Progressive Enhancement¶
Add extensions as needed:
# Phase 1: Basics
- toc
- admonition
- codehilite
# Phase 2: Enhanced
- attr_list
- def_list
- footnotes
# Phase 3: Advanced
- pymdownx.superfences
- pymdownx.tabbed
- pymdownx.arithmatex
3. Test Compatibility¶
Some extensions may conflict: - Test new extensions thoroughly - Check rendering in different contexts - Verify mobile compatibility
4. Document Usage¶
Create a style guide:
# Markdown Style Guide
## Admonitions
Use `!!! note` for general information
Use `!!! warning` for important cautions
## Code Blocks
Always specify language: ` ```python`
Use line highlighting: ` ```python hl_lines="2-4"`
## Tables
Keep tables simple and readable
Use alignment sparingly
Performance Considerations¶
Heavy Extensions¶
Some extensions impact build time:
- search
- Indexes all content
- minify
- Processes all HTML
- Complex regex patterns
Optimization¶
# Disable unused features
markdown_extensions:
- pymdownx.highlight:
guess_lang: false # Faster
- toc:
toc_depth: 3 # Limit depth
Troubleshooting¶
Common Issues¶
-
Extension not found
-
Conflict between extensions
- Check extension order
-
Some extensions must load first
-
Rendering issues
- Verify syntax is correct
- Check extension configuration
- Test with minimal config
Debug Mode¶
Migration Guide¶
From GitHub Markdown¶
GitHub | MkDocs Extension |
---|---|
Tables | tables |
Task lists | pymdownx.tasklist |
Strikethrough | pymdownx.tilde |
Emoji | pymdownx.emoji |
Syntax highlight | pymdownx.highlight |
From CommonMark¶
Add extensions for CommonMark features:
Resources¶
Documentation¶
Extension Lists¶
See Also¶
- PyMdown Extensions - Enhanced extensions
- Code Blocks - Code highlighting features
- Content Tabs - Tabbed content
- Admonitions - Callout boxes