Content reuse directives allow you to include external files directly in your markdown content, enabling a "Write Once, Publish Everywhere" strategy.
See also: Content Reuse for detailed strategies, best practices, and common patterns.
Key Terms
- Include Directive
- A directive (
{include}) that includes markdown files directly in your content. Supports line ranges and relative path resolution. - Literal Include Directive
- A directive (
{literalinclude}) that includes code files as syntax-highlighted code blocks. Auto-detects language from file extension and supports line ranges, emphasis, and line numbers. - Snippet
- A reusable content file (typically markdown or code) that can be included in multiple pages. Organized in dedicated directories like
content/snippets/. - Path Resolution
- The process of finding included files. Bengal resolves paths relative to the current page's directory first, then falls back to the site root.
- Path Traversal Prevention
- Security feature that prevents including files outside the site root. Blocks
../sequences and absolute paths to protect against unauthorized file access.
Include
Include markdown files directly in your content.
Syntax:
1 2 | |
With Options:
1 2 3 4 | |
Options:
:start-line:- Starting line number (1-indexed):end-line:- Ending line number (1-indexed)
Path Resolution
Paths are resolved relative to:
- Current page's directory - If you're in
content/docs/content/,snippets/warning.mdlooks incontent/docs/content/snippets/ - Site root - Falls back to site root if not found relative to page
Example: Path Resolution
- Page:
content/docs/getting-started/installation.md - Include:
snippets/warning.md - Resolves to:
content/docs/getting-started/snippets/warning.md(orcontent/snippets/warning.mdif not found)
Examples
Include Entire File:
1 2 | |
Include Specific Lines:
1 2 3 4 | |
Nested Directives: Included content can use other directives:
1 2 3 4 | |
Literal Include
Include code files as syntax-highlighted code blocks.
Syntax:
1 2 | |
With Options:
1 2 3 4 5 6 7 | |
Options:
:language:- Override auto-detected language:start-line:- Starting line number (1-indexed):end-line:- Ending line number (1-indexed):emphasize-lines:- Highlight specific lines:7,8,9or7-9:linenos:- Show line numbers:true,false(default:false)
Language Detection
literalincludeauto-detects language from file extensions:
- Python:
.py - JavaScript:
.js,.ts - Web:
.html,.css - Config:
.yaml,.yml,.json,.toml - Shell:
.sh,.bash,.zsh - And many more
Examples
Basic Include:
1 2 | |
With Line Range:
1 2 3 4 | |
With Emphasized Lines:
1 2 3 | |
With Line Numbers:
1 2 3 | |
Complete Example:
1 2 3 4 5 6 7 | |
Path Resolution
Same as{include}- paths resolve relative to current page's directory, then site root.
Security
Both directives prevent path traversal attacks:
- Only allows paths within the site root
- Rejects paths with
..sequences - Validates file existence before inclusion
Best Practices
Organize Snippets
Create a dedicated directory structure:
content/
├── snippets/
│ ├── warnings/
│ │ ├── beta-notice.md
│ │ └── deprecated-feature.md
│ ├── code-examples/
│ │ ├── api-client.py
│ │ └── config.yaml
│ └── common-content/
│ ├── installation-steps.md
│ └── troubleshooting.md
└── docs/
└── guides/
└── my-guide.md # Uses snippets via {include}
Keep Snippets Focused
Each snippet should have a single purpose:
✅ Good:snippets/warnings/beta-notice.md- One warning
❌ Bad:snippets/all-warnings.md- Multiple warnings mixed together
Use Relative Paths
Prefer relative paths for portability:
✅ Good:snippets/warning.md
❌ Bad:/absolute/path/to/warning.md
Document Snippets
Add comments explaining purpose:
1 2 3 4 5 6 7 8 9 | |
Common Patterns
Reusable Warnings
1 2 3 4 | |
Use in multiple pages:
1 2 | |
Code Examples
1 2 3 4 | |
Include with emphasis:
1 2 3 | |
Troubleshooting
File Not Found
Error:Include error: File not found: snippets/warning.md
Solutions:
- Check the path is correct relative to your page
- Verify the file exists
- Try an absolute path from site root:
content/snippets/warning.md
Path Traversal Blocked
Error:Include path traversal rejected
Cause: Attempted to include files outside the site root (security feature)
Solution: Keep all snippets within your site's content directory
Syntax Errors in Included Content
If included markdown has syntax errors, they'll appear in the rendered page.
Solution: Validate snippet files independently before including them
Related
- Content Reuse - Detailed strategies and best practices
- Formatting Directives - Other formatting options