Skip to main content

Lektor Markdown Extension for Image and Link attributes

PyPI version PyPI Supported Python Versions GitHub license GitHub Actions (Tests)

This plugin hacks up Lektor’s Markdown renderer to add syntax which allows arbitrary HTML attributes to be set on image and link tags.

Motivation

Markdown, as implemented in Lektor, provides nice shorthand syntax for marking up images. It does not provide any syntax for controlling image styling, e.g., sizing and alignment. This plugin molests Markdowns syntax for image (and link) titles to allow setting arbitrary HTML attributes on image and link tags.

On using raw HTML

It is true that one can simply include raw HTML <img> tags within Markdown text. However, since this bypasses Markdown processing altogether, it also bypasses Lektor’s resolution of image/link URLs. This can be undesirable.

Setting attributes on link and image tags in Markdown text

These hacks allow one to set attributes on image and link tags, by overloading Markdown’s syntax for setting title attributes.

If the title of a Markdown image or link looks like a set of (non-empty) HTML attributes, they are interpreted as such, and stuck onto the produced <a> or <img> tag.

![My cat, Fluffy](fluffy.jpg "class=img-responsive")

will produce

<img src="fluffy.jpg" class=img-responsive>

If you do want to set a title as well, you may surround the extra attributes with angle brackets and append them at the end of the title. Both of these are equivalent:

![My cat, Fluffy](fluffy.jpg "Fluffy at rest <class=img-responsive>")
![My cat, Fluffy](fluffy.jpg "title='Fluffy at rest' class=img-responsive")

If you’d like to set a title, when the title text looks like a set of HTML attributes, simply append an empty set of angle brackets to the title:

![My cat, Fluffy](fluffy.jpg "Fluffy=resting <>")

This works with reference-style links and images as well:

Here’s a photo of [my cat][]:  ![fluffy][]

[fluffy]: fluffy.jpg   (style='width: 80px;')
[my cat]: <http://fluffy.example.org>   'Fluffy’s website <class="external link">'

Testing

We now use hatch for packaging and development.

To run all tests, including under a full matrix of python and lektor versions, as well as combined coverage tests:

hatch run cover:full

Acknowledgments

Ideas were gleaned from this blog post by Baron Schwartz.

Author

Jeff Dairiki dairiki@dairiki.org

Metadata

Release files for lektor-markdown-image-attrs 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lektor-markdown-image-attrs 0.2.1
File Size Uploaded
lektor_markdown_image_attrs-0.2.1.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lektor-markdown-image-attrs 0.2.1
File Interpreter ABI Platform
lektor_markdown_image_attrs-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / lektor_markdown_image_attrs-0.2.1.tar.gz

Download URL lektor_markdown_image_attrs-0.2.1.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
141e127d7c210c1305eb7880b9267160c807919f10bc7a73056bc58c791dd3cc
BLAKE2b-256 checksum
How to use checksums
f50734c918fd46ec8e1a5aaa1ee30457776f03b480f887fc407a6587a3a951a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.23.0

Release files / lektor_markdown_image_attrs-0.2.1-py3-none-any.whl

Download URL lektor_markdown_image_attrs-0.2.1-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e76a191aa9da92716a6bc695b1aecde296e9c4656254cf5dc105208b31fc5fd5
BLAKE2b-256 checksum
How to use checksums
520ddc37b3d8e7138cca6c20bdce3c2a728af5130fa514b15c8204ab10c0b902
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.23.0

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page