Skip to main content

Convert markdown imgs into captioned <figure>s

Project description

Markdown img2fig

Convert inline markdown images to captioned figures.

Inspirated by markdown-captions and yafg.

Installation

pip install markdown-img2fig

Usage

Markdown module

import markdown
import img2fig

data = markdown.markdown(
    md,
    extensions=[
        img2fig.Img2FigExtension(
            source_attr="title",
            remove_attr=True,
            force_convert=True,
            empty_as_none=True,
        ),
        'attr_list',  # optional
    ]
)

MkDocs mkdocs.yml

markdown_extensions:
  - img2fig:
      source_attr: title
      remove_attr: true
      force_convert: true
      empty_as_none: true
  - attr_list # optional

Result

![Alt text](image.jpg "Title text"){: .someclass }
<figure class="someclass">
    <img alt="Alt text" src="image.jpg" />
    <figcaption>Title text</figcaption>
</figure>

Options

  • source_attr (default: 'title')
    Use 'title' or 'alt' attribute as the caption.
  • remove_attr (default: True)
    Remove the alt/title attribute after conversion.
  • force_convert (default: True)
    Convert all images to figures, missing alt/title will cause figures without figcaptions.
    If false, images will be left as is.
  • empty_as_none (default: True)
    Treat empty alt/title as if they don't exist.

License

Licensed under the MIT License.

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

markdown_img2fig-1.0.1.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

markdown_img2fig-1.0.1-py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 3

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