Skip to main content

A Django middleware for rendering Markdown responses.

Project description

Latest Version CI Status

A Django middleware that converts HTML responses to Markdown when the client sends an Accept: text/markdown request header.

Requirements

django-markdown-middleware supports Python 3.11+ and requires Django 5.2 or later.

Installation

$ pip install django-markdown-middleware

Add the middleware to your Django settings:

MIDDLEWARE = [
    ...
    "markdown_middleware.middleware.MarkdownMiddleware",
    ...
]

The middleware must be placed after any middleware that sets the response content type (e.g. after CommonMiddleware).

Usage

Any client that sends Accept: text/markdown in the request headers will receive the HTML response body converted to Markdown, with the response Content-Type changed to text/markdown; charset=utf-8.

The response also includes an X-Markdown-Tokens header containing an approximate token count of the converted Markdown content (estimated as len(markdown) // 4).

Only HTTP 200 responses with a text/html content type are converted. All other responses are passed through unchanged.

Caching

To enable caching of converted Markdown responses, set MARKDOWN_MIDDLEWARE_CACHE_TIMEOUT in your Django settings to the desired cache duration in seconds:

# Cache converted Markdown responses for 5 minutes
MARKDOWN_MIDDLEWARE_CACHE_TIMEOUT = 300

When this setting is absent or None, no caching is performed.

Cache keys have the form markdown_middleware:<path>, for example markdown_middleware:/api/products/, making them easy to inspect or manage directly in your cache backend.

Cache Invalidation

To invalidate cached Markdown responses for a specific path, use invalidate_cache. It removes all cached variants of that path, including responses with different query strings:

from markdown_middleware import invalidate_cache

# Invalidates /blog/my-post/, /blog/my-post/?page=2, etc.
invalidate_cache("/blog/my-post/")

This is useful in post-save signals or management commands when content changes and the cached Markdown must be refreshed.

Configuration

Set MARKDOWN_MIDDLEWARE_ANONYMOUS_ONLY to False to also convert responses for authenticated users (defaults to True):

MARKDOWN_MIDDLEWARE_ANONYMOUS_ONLY = False

Set MARKDOWN_MIDDLEWARE_CACHE_TIMEOUT to the desired cache duration in seconds for converted Markdown responses. When absent or None (the default), no caching is performed:

MARKDOWN_MIDDLEWARE_CACHE_TIMEOUT = 300

Set MARKDOWN_MIDDLEWARE_CONVERT_OPTIONS to a dictionary of keyword arguments passed directly to html-to-markdown’s ConversionOptions. When absent or None (the default), the library’s defaults are used.

Remove inline SVG elements:

MARKDOWN_MIDDLEWARE_CONVERT_OPTIONS = {"strip_tags": ["svg"]}

Remove inline (data URI) images while keeping linked images:

MARKDOWN_MIDDLEWARE_CONVERT_OPTIONS = {"exclude_selectors": ['img[src^="data:"]']}

Both options combined:

MARKDOWN_MIDDLEWARE_CONVERT_OPTIONS = {
    "strip_tags": ["svg"],
    "exclude_selectors": ['img[src^="data:"]'],
}

See the html-to-markdown documentation for the full list of available options.

Prepare for development

Install uv, then:

$ uv sync --group dev

Run the tests:

$ make tests

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

django_markdown_middleware-0.2.0.tar.gz (53.7 kB view details)

Uploaded Source

Built Distribution

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

django_markdown_middleware-0.2.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file django_markdown_middleware-0.2.0.tar.gz.

File metadata

  • Download URL: django_markdown_middleware-0.2.0.tar.gz
  • Upload date:
  • Size: 53.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_markdown_middleware-0.2.0.tar.gz
Algorithm Hash digest
SHA256 de4b630a1a1e74fdb480b83d1b56705fbc1403af0e740d4cf8a0d9ebdc175a65
MD5 7899eaae5c67370297562457331aa8cd
BLAKE2b-256 591cce66843c276a9b73497d078b5fa6c61296b5e4d99c740c22ff893b50c303

See more details on using hashes here.

File details

Details for the file django_markdown_middleware-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_markdown_middleware-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_markdown_middleware-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41ee3adfcffbcf9fd3f377208174065cb3dc3bd586ef8f30124cd7860610248c
MD5 f3049843b98a0f12e3f979c8304d742d
BLAKE2b-256 6c90ec53ab167e85d20be3f0849a2b3b79791a0480cdb7563696fb22f2574ab5

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