Skip to main content

Extend Lektor’s Markdown syntax to allow setting arbitrary HTML attributes on images and links.

Project description

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

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

lektor_markdown_image_attrs-0.2.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file lektor_markdown_image_attrs-0.2.1.tar.gz.

File metadata

File hashes

Hashes for lektor_markdown_image_attrs-0.2.1.tar.gz
Algorithm Hash digest
SHA256 141e127d7c210c1305eb7880b9267160c807919f10bc7a73056bc58c791dd3cc
MD5 e81ab29cfbcd9a63020b61922572190d
BLAKE2b-256 f50734c918fd46ec8e1a5aaa1ee30457776f03b480f887fc407a6587a3a951a6

See more details on using hashes here.

File details

Details for the file lektor_markdown_image_attrs-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for lektor_markdown_image_attrs-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e76a191aa9da92716a6bc695b1aecde296e9c4656254cf5dc105208b31fc5fd5
MD5 e1441ce51459022cae89bfd98378435f
BLAKE2b-256 520ddc37b3d8e7138cca6c20bdce3c2a728af5130fa514b15c8204ab10c0b902

See more details on using hashes here.

Supported by

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