Skip to content

Commit

Permalink
Prevent inline images to be converted to figures
Browse files Browse the repository at this point in the history
This commit pervents inline images do be converted to
figure even if the have an alt text. In addition a new
option is added that disables the usage of the alt text
completely.
  • Loading branch information
tobiasah committed Jan 12, 2024
1 parent 94af93e commit b37ded7
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 0.0.11

* Prevent conversion of inline images even if they have an alt text (#9)

## Version 0.0.10

* Allow caption elements within indented blocks (e.g. admonitions). Can be disabled
Expand Down
2 changes: 2 additions & 0 deletions demo/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ and it goes on for multiple lines

![](assets/demo.png){width="200"}

Inline images should not be converted ![Hello](assets/demo.png){width="30"}, even if they have a alt text.

## Tables

Table: Table caption
Expand Down
3 changes: 3 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ plugins:
caption_prefix: 'Figure {index}:'
markdown_identifier: 'Figure:'
allow_indented_caption: True
ignore_alt: False
custom: # (4)!
enable: true
start_index: 1
Expand Down Expand Up @@ -72,6 +73,8 @@ The following table lists all available options.
| caption_prefix | The prefix put before of the caption text |
| markdown_identifier | The identifier that this plugin will search for in the markdown. (Note that every match of this identifier will be treated as a caption element. A false match will most likely result in an error) |
| allow_indented_caption | Flag if caption elements should also be parsed within indented blocks. By default this is enabled. |
| ignore_alt | Flag if the alt attribute should be ignored. This will disable the feature that
uses the alt text as a caption. (Only available for figures)|

## Overwriting the default configuration

Expand Down
16 changes: 15 additions & 1 deletion src/mkdocs_caption/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ def get_default_id(self, identifier: str, index: int) -> str:
return self._format_string(self.default_id, identifier, index=index)


class FigureCaption(IdentifierCaption):
"""The configuration options for the figure identifier.
This configuration derived from IdentifierCaption and adds additional
configuration options.
Args:
ignore_alt: Flag if the alt text should be ignored for captions or
not.
"""

ignore_alt = config_options.Type(bool, default=False)


class CaptionConfig(base.Config):
"""The configuration options for the Caption plugin.
Expand All @@ -115,7 +129,7 @@ class CaptionConfig(base.Config):
default=[],
)
table = config_options.SubConfig(IdentifierCaption)
figure = config_options.SubConfig(IdentifierCaption)
figure = config_options.SubConfig(FigureCaption)
custom = config_options.SubConfig(IdentifierCaption)


Expand Down
17 changes: 10 additions & 7 deletions src/mkdocs_caption/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
from mkdocs_caption.helper import TreeElement, update_references, wrap_md_captions

if TYPE_CHECKING:
from mkdocs_caption.config import IdentifierCaption
from mkdocs_caption.config import FigureCaption
from mkdocs_caption.logger import PluginLogger

IMG_CAPTION_TAG = "figure-caption"


def preprocess_markdown(markdown: str, *, config: IdentifierCaption) -> str:
def preprocess_markdown(markdown: str, *, config: FigureCaption) -> str:
"""Preprocess markdown to wrap image captions.
The image captions are wrapped in a custom html
Expand Down Expand Up @@ -77,7 +77,7 @@ def postprocess_image(
img_element: TreeElement,
title: str,
tree: TreeElement,
config: IdentifierCaption,
config: FigureCaption,
logger: PluginLogger,
index: int,
figure_attrib: dict[str, str] | None,
Expand Down Expand Up @@ -139,7 +139,7 @@ def postprocess_image(
def postprocess_html(
*,
tree: TreeElement,
config: IdentifierCaption,
config: FigureCaption,
logger: PluginLogger,
) -> None:
"""Postprocess an XML tree to handle custom image captions.
Expand Down Expand Up @@ -185,9 +185,12 @@ def postprocess_html(
for img_element in tree.xpath("//p/a/img|//p/img"):
figure_attrib = custom_figure_attrib.get(img_element, {})
# We pop the title here so its not duplicated in the img element
title = img_element.attrib.pop("title", img_element.get("alt", None))
if not title:
continue
title = img_element.attrib.pop("title", None)
if title is None:
# Use the alt text if provided
title = img_element.get("alt", None)
if config.ignore_alt or not title or img_element.tail is not None:
continue
postprocess_image(
img_element=img_element,
title=title,
Expand Down
Loading

0 comments on commit b37ded7

Please sign in to comment.