Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Ignore all descriptions for rendered images #287

Merged
merged 1 commit into from
Aug 4, 2024
Merged

Conversation

pengjiz
Copy link
Contributor

@pengjiz pengjiz commented Aug 2, 2024

First of all, thanks for creating and maintaining this awesome tool! It is incredibly handy and useful to me.

Back to the PR. I was bitten by #285 as well so I decided to have a look at it.

According to the CommonMark Spec, all inline elements are allowed in the image description part. However, previously we only handled plain text elements (by ignoring them). So in this PR we change to ignore all elements other than image elements.

To correctly handle nested images inside the image description, here we push dummy RenderedImage states to the stack. An alternative solution would be maintaining a counter for nested image level and checking the counter when dealing with end of image events, but that feels less tasteful to me.

Note that this is a bit more lenient than the spec because it allows block elements as well. However, this behaviour aligns with the upstream library, and the more correct version is a bit tedious to implement because we have to list all inline tags ourselves.

By the way, this issue should be already covered in the 'commonmark-spec' test cases (e.g., 573-images.md). However, because the image files for those fixtures are missing, the images are never rendered as images but links (golden/iterm2/commonmark-spec/573-images). So those problems are not detected in testing. I think we perhaps need to address this as well, but I do not have a good solution. What do you think?

Re: #285, #194.

@swsnr swsnr self-assigned this Aug 2, 2024
@swsnr
Copy link
Owner

swsnr commented Aug 2, 2024

@pengjiz Thanks. Would you take a look at the Github actions failure?

Wrt to testing, I'm not sure we can easily rely on the spec test cases here, given that those reference non-existing images, but we have additional test cases in pulldown-cmark-mdcat/tests/render/md/samples, so perhaps create one for nested markup in image descriptions?

Add a markdown file, and some kind of dummy image (a 1x1px white PNG image would suffice), and then regenerate the golden files (see test_with_golden_file in pulldown-cmark-mdcat/tests/render.rs).

@pengjiz
Copy link
Contributor Author

pengjiz commented Aug 3, 2024 via email

CommonMark allows all inline elements in the image description part[1],
not only plain text. So in this commit we change to ignore all such
elements instead of panicking.

To correctly handle nested images inside the image description, here we
push dummy 'RenderedImage' states to the stack. An alternative solution
would be maintaining a counter for nested image level and checking the
counter when dealing with end of image events, but that feels less
tasteful to me.

Note that this is a bit more lenient than the spec because it allows
block elements as well. However, this behaviour aligns with the upstream
library[2], and the more correct version is a bit tedious to implement
because we have to list all inline tags ourselves.

Add tests for markups in image descriptions

Those cases are actually covered in the 'commonmark-spec' test suite.
However, those tests use non-existent images so they are not checked for
rendered images. As such, in this commit we add a new case in
'samples/images.md', primarily to test markups in descriptions of
rendered images.

Closes swsnrGH-285.

[1] https://spec.commonmark.org/0.31.2/#images
[2] https://docs.rs/pulldown-cmark/0.9.6/src/pulldown_cmark/html.rs.html#377-399
@swsnr swsnr enabled auto-merge August 4, 2024 06:00
@swsnr swsnr merged commit f67b44d into swsnr:main Aug 4, 2024
6 checks passed
@swsnr
Copy link
Owner

swsnr commented Aug 4, 2024

Thanks 🙏 rebased and merged, I'll make a bugfix release soon.

@pengjiz pengjiz deleted the fix/gh-285 branch August 4, 2024 07:17
@pengjiz
Copy link
Contributor Author

pengjiz commented Aug 4, 2024 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants