Skip to main content

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:

![caption](path/to/image.svg)

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("![Logo](logo.svg)")

Usage

Basic Inline SVG

!(diagram.svg)

Inline SVG with Caption

![Data Flow](images/flow.svg)

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:

![icon](ui/menu.svg)

resolves to:

/var/www/assets/svg/ui/menu.svg

How it Works

  1. Regex detection Matches ![caption](file.svg) or !(file.svg).
  2. Path resolution Relative paths are resolved using the configured root.
  3. SVG parsing & caching Files are parsed with xml.etree.ElementTree. Parsed SVGs are cached globally in _CACHE.
  4. Placeholder insertion A placeholder token like \x02/path/to/file.svg\x03 is inserted instead of the SVG.
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

isvg-1.0.2.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

isvg-1.0.2-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

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

Hashes for isvg-1.0.2.tar.gz
Algorithm Hash digest
SHA256 b3332f1a883aa2b87a2d4481e59b55518cc7fdf7e98fa0297dc1b0bd488c03dd
MD5 6d0528d7d8492dad3e3de1ce2a91ae0b
BLAKE2b-256 c89f80338da0788ca1f6d325a45cb234849bd9707fe801d96311c697c70ab863

See more details on using hashes here.

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

Hashes for isvg-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4307b21e692d4e2b5aa2d778d8eea569b33186b2136e6b09e493799219267305
MD5 7a55b81ba532c86adc8431e1922efd92
BLAKE2b-256 0199e4f17973026c7ba3a5f0ee8970224ccf935f64bba0c671d6d17b8693b4b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page