Skip to main content

Flask extension to serve HTML pages as Markdown via ?format=md query parameter.

Project description

canonicalwebteam.markdown-response

Flask extension that adds ?format=md support to all HTML responses, converting pages to clean Markdown with YAML frontmatter. Designed for LLM and crawler optimization.

Installation

pip install canonicalwebteam.markdown-response

Usage

from canonicalwebteam.markdown_response import MarkdownResponse

app = Flask(__name__)
MarkdownResponse(app)

Or with the application factory pattern:

md = MarkdownResponse()
md.init_app(app)

Any page can now be accessed as Markdown by appending ?format=md to the URL.

Configuration

MarkdownResponse(app,
    content_selector="#main-content",  # CSS selector for content extraction
    strip_elements=["script", "style", "nav", "noscript"],  # Tags to remove
    strip_classes=["u-hide", "u-off-screen"],  # Classes to remove
    query_param="format",  # Query parameter name
    query_value="md",  # Query parameter value
)

Template-level exclusion

Add data-md-strip to any HTML element to exclude it from the Markdown output:

<section data-md-strip>
    <form>This form won't appear in markdown output</form>
</section>

How it works

  1. An after_request handler intercepts responses when ?format=md is present
  2. Only processes HTML 200 responses (JSON, XML, errors pass through)
  3. Extracts the content area using BeautifulSoup (#main-content by default)
  4. Strips unwanted elements (scripts, styles, nav, hidden elements, data-md-strip)
  5. Converts remaining HTML to Markdown via markdownify
  6. Prepends YAML frontmatter extracted from <head> meta tags
  7. Returns with Content-Type: text/markdown; charset=utf-8

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

canonicalwebteam_markdown_response-0.1.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file canonicalwebteam_markdown_response-0.1.1.tar.gz.

File metadata

File hashes

Hashes for canonicalwebteam_markdown_response-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a6cf9e70e7eafb4ab57c3d9af507403ce817ac9509eeed8a3282160853f1e108
MD5 adf6f92ca275864e97862fcc415c3389
BLAKE2b-256 0daf0d39551801fabe2b9c1b2b17c116f1712545962cbef9e5b04c4e7be65756

See more details on using hashes here.

Provenance

The following attestation bundles were made for canonicalwebteam_markdown_response-0.1.1.tar.gz:

Publisher: publish.yaml on canonical/canonicalwebteam.markdown-response

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file canonicalwebteam_markdown_response-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for canonicalwebteam_markdown_response-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 967f79d6968483203da6ad0d5956fabdcd050e7f5ece6d89d072fedbc9984610
MD5 d47df359bdf1044395687ae5b1d8b6a5
BLAKE2b-256 e01aa7f5860ca3122174d8c1014817d2b3ba37852d8b8d300b68625f1b809df3

See more details on using hashes here.

Provenance

The following attestation bundles were made for canonicalwebteam_markdown_response-0.1.1-py3-none-any.whl:

Publisher: publish.yaml on canonical/canonicalwebteam.markdown-response

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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