SuperFences¶
SuperFences is a powerful PyMdown extension that enhances code blocks with advanced features like nested fences, custom fence processors, and live rendering capabilities.
Overview¶
SuperFences extends the standard Markdown fenced code blocks with:
- Nested code blocks - Place code blocks inside other content
- Custom fence processors - Support for Mermaid diagrams and other languages
- Syntax highlighting - Advanced highlighting with line numbers
- Language detection - Automatic language detection
- Copy button - Easy code copying
Basic Usage¶
Standard Code Blocks¶
Nested Code Blocks¶
SuperFences allows you to nest code blocks inside other content:
Nested Code Example
Here's how to use nested code blocks:
Mermaid Diagrams¶
SuperFences is configured to render Mermaid diagrams directly in your documentation.
Flowchart Example¶
Sequence Diagram¶
sequenceDiagram
participant User
participant System
participant Database
User->>System: Request Data
System->>Database: Query
Database-->>System: Results
System-->>User: Response
Class Diagram¶
classDiagram
class Animal {
+String name
+int age
+makeSound()
}
class Dog {
+String breed
+bark()
}
class Cat {
+String color
+meow()
}
Animal <|-- Dog
Animal <|-- Cat
Advanced Features¶
Line Numbers¶
Enable line numbers for better code reference:
Highlighting Specific Lines¶
Highlight important lines in your code:
def validate_email(email):
"""Validate email format."""
if "@" not in email:
return False
if "." not in email.split("@")[1]:
return False
return True
Title and Line Number Start¶
Add titles and custom line number starts:
utils/validators.py | |
---|---|
Configuration¶
SuperFences is configured in mkdocs.yml
:
markdown_extensions:
- pymdownx.superfences:
custom_fences:
- name: mermaid
class: mermaid
format: !!python/name:pymdownx.superfences.fence_code_format
Working with Highlight¶
SuperFences works seamlessly with the Highlight extension:
markdown_extensions:
- pymdownx.highlight:
anchor_linenums: true
line_spans: __span
pygments_lang_class: true
- pymdownx.superfences
Live Examples¶
Interactive Python Example¶
# This is a live example that demonstrates Python features
import json
data = {
"name": "MkDocs",
"type": "Documentation Generator",
"features": ["Markdown", "Themes", "Plugins"]
}
# Pretty print JSON
print(json.dumps(data, indent=2))
Shell Commands¶
# Install MkDocs and dependencies
pip install mkdocs-material
pip install pymdown-extensions
# Serve documentation locally
mkdocs serve --dev-addr localhost:8000
Configuration Files¶
.github/workflows/docs.yml
name: Deploy Documentation
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- run: pip install mkdocs-material
- run: mkdocs gh-deploy --force
Best Practices¶
- Choose the right diagram type: Use flowcharts for processes, sequence diagrams for interactions
- Keep diagrams simple: Complex diagrams are hard to maintain
- Use consistent styling: Follow your documentation's visual style
- Add descriptions: Explain complex diagrams with text
- Test rendering: Verify diagrams render correctly across devices
Browser Compatibility¶
Mermaid diagrams are rendered using JavaScript and require:
- Modern browsers with ES6 support
- JavaScript enabled
- Sufficient viewport width for complex diagrams
Troubleshooting¶
Diagrams Not Rendering¶
- Check JavaScript console for errors
- Verify Mermaid library is loaded
- Ensure proper fence syntax
- Check for syntax errors in diagram code
Syntax Highlighting Issues¶
- Verify language identifier is correct
- Check Pygments supports the language
- Ensure proper indentation
- Clear browser cache
Related Features¶
- Code Blocks - Basic code block features
- PyMdown Extensions - Other PyMdown features
- Diagrams - Dedicated diagram documentation