Skip to content

Commit

Permalink
Introduce option to ignore specific classes during the conversion.
Browse files Browse the repository at this point in the history
This commit adds a new config element `ignore_classes` to explicitly
prevent certain classes from beeing converted.

This option was added to ignore things like emojis when creating
the figures.
  • Loading branch information
tobiasah committed Jan 25, 2024
1 parent 3a2ba25 commit 9bb8c61
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Version 0.0.11

* Prevent conversion of emojis into figure elements with captions.
* Add new config parameter `ignore_classes` to explicitly prevent conversion
of specific images into figures.

## Version 0.0.11

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

## Version 0.0.10
Expand Down
2 changes: 1 addition & 1 deletion demo/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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.
Inline images should not be converted ![Hello](assets/demo.png){width="30"}, even if they have a alt text. :smile:

## Tables

Expand Down
5 changes: 4 additions & 1 deletion demo/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ markdown_extensions:
- admonition
- pymdownx.details
- pymdownx.superfences

- attr_list
- pymdownx.emoji:
emoji_index: !!python/name:material.extensions.emoji.twemoji
emoji_generator: !!python/name:material.extensions.emoji.to_svg
plugins:
caption:
additional_identifier: ["List", "Code"]
Expand Down
4 changes: 3 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ plugins:
markdown_identifier: 'Figure:'
allow_indented_caption: True
ignore_alt: False
ignore_classes: ["twemoji"]
custom: # (4)!
enable: true
start_index: 1
Expand Down Expand Up @@ -74,7 +75,8 @@ The following table lists all available options.
| 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)|
uses the alt text as a caption. (Only available for figures) |
| ignore_classes | List of classes ignored when adding the captions. (Only available for figures) |

## Overwriting the default configuration

Expand Down
4 changes: 4 additions & 0 deletions src/mkdocs_caption/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class FigureCaption(IdentifierCaption):
"""

ignore_alt = config_options.Type(bool, default=False)
ignore_classes = config_options.ListOfItems(
config_options.Type(str),
default=["twemoji"],
)


class CaptionConfig(base.Config):
Expand Down
2 changes: 2 additions & 0 deletions src/mkdocs_caption/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ def postprocess_html(
index = config.start_index
for img_element in tree.xpath("//p/a/img|//p/img"):
figure_attrib = custom_figure_attrib.get(img_element, {})
if img_element.attrib.get("class", None) in config.ignore_classes:
continue
# We pop the title here so its not duplicated in the img element
title = img_element.attrib.pop("title", None)
if title is None:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,33 @@ def test_figure_caption_ignores_alt_if_disabled():
result = result[len("<html><body>") : -len("</body></html>")]
result = result[len("<p>") : -len("</p>")]
assert result == img


def test_figure_caption_uses_unmarked_classes():
config = FigureCaption()
img = '<img class="custom_class" src="test.png" title="=text" id="test_id">'
html = p(img)
parser = etree.HTMLParser()
tree = etree.fromstring(html, parser)
logger = get_logger("test.md")
image.postprocess_html(tree=tree, config=config, logger=logger)
result = etree.tostring(tree, encoding="unicode", method="html")
# htmlparser adds <html><body> tags, remove them
result = result[len("<html><body>") : -len("</body></html>")]
result = result[len("<p>") : -len("</p>")]
assert "figcaption" in result


def test_figure_caption_ignores_marked_classes():
config = FigureCaption()
img = '<img class="twemoji" src="test.png" title="=text" id="test_id">'
html = p(img)
parser = etree.HTMLParser()
tree = etree.fromstring(html, parser)
logger = get_logger("test.md")
image.postprocess_html(tree=tree, config=config, logger=logger)
result = etree.tostring(tree, encoding="unicode", method="html")
# htmlparser adds <html><body> tags, remove them
result = result[len("<html><body>") : -len("</body></html>")]
result = result[len("<p>") : -len("</p>")]
assert result == img

0 comments on commit 9bb8c61

Please sign in to comment.