stuebersystems / mkdocs-img2fig-plugin

A MkDocs plugin that converts markdown encoded images into <figure> elements.
https://pypi.org/project/mkdocs-img2fig-plugin
MIT License
20 stars 10 forks source link

Add option to ignore converting certain figures #3

Open timvink opened 3 years ago

timvink commented 3 years ago

I have a feature request.

I would like the option to specify that some images in my page should not be converted to figures.

For example:

[![Actions Status](https://github.com/timvink/mkdocs-print-site-plugin/workflows/pytest/badge.svg)](https://github.com/timvink/mkdocs-print-site-plugin/actions)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/mkdocs-print-site-plugin)
![PyPI](https://img.shields.io/pypi/v/mkdocs-print-site-plugin)
![PyPI - Downloads](https://img.shields.io/pypi/dm/mkdocs-print-site-plugin)
[![codecov](https://codecov.io/gh/timvink/mkdocs-print-site-plugin/branch/master/graph/badge.svg)](https://codecov.io/gh/timvink/mkdocs-print-site-plugin)
![GitHub contributors](https://img.shields.io/github/contributors/timvink/mkdocs-print-site-plugin)
![PyPI - License](https://img.shields.io/pypi/l/mkdocs-print-site-plugin)

Should render as:

image

But renders as:

image

Proposal

I would suggest two updates:

fstueber commented 3 years ago

Hi Tim, makes sense. I would propose following solution:

What do you think?

fstueber commented 3 years ago

One more: We can control whether images without captions are converted or not via plugin config.

fstueber commented 3 years ago

While thinking more about this plugin I would propose a complete different approach:

timvink commented 3 years ago

Good idea, much cleaner!

I would prefer a more explicit syntax, f.e.: ![caption:<your text>](url). Adding a caption: prefix to your markdown images alttext is a syntax that should be easy to memorize for users.

While we're brainstorming, I also think the figure captions are rendered too big, and the whitespace underneath is too narrow. I think it would be a good idea to update the docs with some custom CSS styles for the mkdocs and material themes, and show users how to add those snippets via the extra_css parameter.

image

timvink commented 3 years ago

So I also thought about this a bit more.

What this plugin is doing is actually just extending the markdown syntax while parsing to HTML. So instead of being a mkdocs plugin, it could be a markdown extension (which you can also install via pip and use in your mkdocs.yml, see https://www.mkdocs.org/user-guide/configuration/#markdown_extensions)

When you install mkdocs-material, the excellent facelessuser/pymdown-extensions is also installed, which contains many extensions to markdown.

@facelessuser have you ever considered the use case of adding an extension to automatically create HTML figcaptions from markdown? Would that fit the scope of pymdown-extensions? Any ideas/advice?

EDIT:

OK there are already lots of markdown extensions that do this. see https://github.com/Python-Markdown/markdown/wiki/Third-Party-Extensions#generic--structure. The syntax most commonly used is with the title attribute:

![Alt text](/path/to/image.png "This is the title of the image.")

becomes

<figure>
<img alt="Alt text" src="/path/to/image.png" title="This is the title of the image." />
<figcaption>This is the title of the image.</figcaption>
</figure>

This meant also mean that this plugin is redundant and could be deprecated..

fstueber commented 3 years ago

I will have a closer look on those markdown extensions. But it seems you are right and someone else has already solved the problem.

timvink commented 3 years ago

I had a look, and:

So there is still an opportunity here to improve. So not using the caption: prefix should do nothing:

![Alt text](/path/to/image.png "This is the title of the image.")
<img alt="Alt text" src="/path/to/image.png" title="This is the title of the image.">

But adding the caption: prefix to the title should create the figcaption:

![Alt text](/path/to/image.png "caption: This is the title of the image.")
<figure>
     <img alt="Alt text" src="/path/to/image.png" title="This is the title of the image." />
      <figcaption>This is the title of the image.</figcaption>
</figure>

@facelessuser would you be open to a PR to pymdown-extensions that adds a figure caption extension? I'd be happy to contribute!

facelessuser commented 3 years ago

@timvink I have not yet considered adding a figure caption extension. It isn't something I've really need yet.

With that said, I see a lot of custom syntax, but I imagine this could be done quite simply using the existing attr_list extension paired with either a Python Markdown extension or MkDocs plugin.

Basically, just attach some attribute via attr_list to identify this specific image wants to have a caption:

![alt](image.png "title"){: data-fig="My caption or you could just use title."}

Then you just look for images with that attribute in either your Python Markdown extension or MkDocs plugin, and wrap the image as required.

timvink commented 3 years ago

Thanks for the input!

@fstueber if you decide to do the implementation via the img2fig plugin, another cool option would be to add lazy image loading by default (controlled via a plugin option). See https://github.com/squidfunk/mkdocs-material/issues/1867

fstueber commented 3 years ago

I'll see what I can do. Must also think whether the extension (and not plugin) approach is better. Will keep you informed.

timvink commented 3 years ago

Cool, thanks!

From the mkdocs-material docs on figure captions:

Sadly, the Markdown syntax doesn't provide native support for image captions, but it's always possible to resort to HTML. Using figure and figcaption, captions can be added to images.

The section is about markdown extensions, so once there is a proper solution (and nice syntax), perhaps it could be promoted there as well.