Extends Markdown with support for inline SVG images.
Project description
Inline SVG Extension for Markdown
A Python-Markdown extension that enables inline embedding of local SVG
files directly into rendered HTML. Unlike standard Markdown image
syntax—which outputs <img> tags. This extension inserts the actual SVG
markup, enabling:
- full CSS styling of embedded SVGs
- consistent scaling behaviour
- figure + caption support
- improved control over accessibility and semantics
Features
Inline SVG injection
Embed SVG files using a syntax similar to normal images:

or without caption:
!(path/to/image.svg)
Automatic <figure> + <figcaption> wrapping
If a caption is provided, the output becomes:
<figure>
<svg> ... </svg>
<figcaption>Your caption</figcaption>
</figure>
Without a caption, only the SVG element is inserted.
Safe placeholder handling
The extension first inserts placeholder tokens, preventing Python-Markdown from escaping the SVG. Placeholders are later replaced with the final SVG markup in the postprocessor phase.
Internal caching
SVG files are parsed once and stored in a global cache for the lifetime of the process, improving performance.
Installation
pip install markdown isvg
Use the extension:
import markdown
from isvg import InlineSVGExtension
md = markdown.Markdown(extensions=[InlineSVGExtension(root="assets/svg")])
html = md.convert("")
Usage
Basic Inline SVG
!(diagram.svg)
Inline SVG with Caption

Example HTML Output
<figure>
<svg xmlns="http://www.w3.org/2000/svg" ...> ... </svg>
<figcaption>Data Flow</figcaption>
</figure>
Configuration
root (default: "./")
Defines the base directory from which SVG paths are resolved.
InlineSVGExtension(root="/var/www/assets/svg")
A Markdown reference such as:

resolves to:
/var/www/assets/svg/ui/menu.svg
How it Works
- Regex detection
Matches
or!(file.svg). - Path resolution Relative paths are resolved using the configured root.
- SVG parsing & caching
Files are parsed with
xml.etree.ElementTree. Parsed SVGs are cached globally in_CACHE. - Placeholder insertion
A placeholder token like
\x02/path/to/file.svg\x03is inserted instead of the SVG. - Postprocessing Placeholders are replaced with raw SVG markup in the final HTML output.
Error Handling
The extension silently ignores:
- Non-SVG files
- Remote URLs (
http://,https://,//) - Missing paths
- Invalid or malformed SVGs
In such cases, the original Markdown text is left unchanged.
Security Considerations
Inlining SVGs introduces risks if files are not trusted. SVGs can contain:
- JavaScript
- External resource references
- Embedded HTML
- CSS injections
If processing user-provided content, sanitize SVGs beforehand using tools such as: external SVG sanitizers or whitelist-based filtering.
Known Limitations
- A global cache is used, which persists for the lifetime of the process.
- Relative links inside SVGs are not rewritten.
- Captions are treated as plain text; no Markdown is rendered inside captions.
- Interactions with other Markdown extensions may affect final output order.
License
This project is licensed under the GNU General Public License v3.0 (GPLv3). You may redistribute and/or modify it under the terms of the GPLv3.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file isvg-1.0.2.tar.gz.
File metadata
- Download URL: isvg-1.0.2.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3332f1a883aa2b87a2d4481e59b55518cc7fdf7e98fa0297dc1b0bd488c03dd
|
|
| MD5 |
6d0528d7d8492dad3e3de1ce2a91ae0b
|
|
| BLAKE2b-256 |
c89f80338da0788ca1f6d325a45cb234849bd9707fe801d96311c697c70ab863
|
File details
Details for the file isvg-1.0.2-py3-none-any.whl.
File metadata
- Download URL: isvg-1.0.2-py3-none-any.whl
- Upload date:
- Size: 16.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4307b21e692d4e2b5aa2d778d8eea569b33186b2136e6b09e493799219267305
|
|
| MD5 |
7a55b81ba532c86adc8431e1922efd92
|
|
| BLAKE2b-256 |
0199e4f17973026c7ba3a5f0ee8970224ccf935f64bba0c671d6d17b8693b4b4
|