From e6215f84bf2cab0bbd8bb0a42ddae0ee6de3cfd6 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Wed, 11 Nov 2026 19:57:38 +0100 Subject: Vendor responsive image plugin locally This adds the responsive-image plugin directly to the project, including its source code, documentation, and tests, allowing it to be maintained locally. It also configures responsive image sizes, updates gallery images to use srcset, and preserves original image URLs for full-resolution viewing. Markdown images gain lazy loading while retaining compatibility with the existing image viewer. --- packages/markdown-figure/lektor_markdown_figure.py | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'packages/markdown-figure/lektor_markdown_figure.py') diff --git a/packages/markdown-figure/lektor_markdown_figure.py b/packages/markdown-figure/lektor_markdown_figure.py index 173535c..648110f 100644 --- a/packages/markdown-figure/lektor_markdown_figure.py +++ b/packages/markdown-figure/lektor_markdown_figure.py @@ -17,9 +17,13 @@ from __future__ import annotations import html import re from typing import Any +from urllib.parse import urlparse import mistune +from lektor.context import get_ctx +from lektor.db import Image from lektor.pluginsystem import Plugin +from lektor.utils import join_path FIGURE_PATTERN = re.compile( r" {0,3}:::figure(?:[ \t]+(?P[^\n]*?))?[ \t]*\n" @@ -66,6 +70,40 @@ def _split_figure(match: re.Match[str]) -> tuple[str, str, str]: return classes, image_source, caption_source +class FullsizeImageMixin: + """Remember the original attachment URL on resized Markdown images. + + Lektor’s responsive-image plugin replaces ``src`` with a thumbnail; the + image viewer needs the unmodified attachment to show the full resolution. + This mixin must run outside the other image renderer mixins so existing + Markdown image attributes still work. + """ + + def image(self, src: str, *args: Any, **kwargs: Any) -> str: + markup = super().image(src, *args, **kwargs) # type: ignore[misc] + parsed = urlparse(src) + if parsed.scheme or parsed.netloc or not parsed.path: + return markup + + renderer = getattr(self, "lektor", self) + record = getattr(renderer, "record", None) + if record is None or record.path is None: + return markup + + path = parsed.path + if not path.startswith("/"): + path = join_path(record.path, path) + attachment = record.pad.get(path) + if not isinstance(attachment, Image): + return markup + + original_url = get_ctx().url_to(attachment) + original_attr = html.escape(original_url, quote=True) + return markup.replace( + " None: + # Run outside the responsive-image and image-attrs renderer mixins. + if FullsizeImageMixin not in config.renderer_mixins: + config.renderer_mixins.insert(0, FullsizeImageMixin) + # Newer Lektor controller using Mistune 2. if hasattr(config, "parser_options"): print("Using mistune 2") -- cgit v1.3.1