Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a render option to render the image as <figure> #458

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,9 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
NodeValue::Image(ref nl) => {
// Unreliable sourcepos.
if entering {
if self.options.render.figure_with_caption {
self.output.write_all(b"<figure>")?;
}
self.output.write_all(b"<img")?;
if self.options.render.experimental_inline_sourcepos {
self.render_sourcepos(node)?;
Expand All @@ -877,6 +880,14 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
self.escape(nl.title.as_bytes())?;
}
self.output.write_all(b"\" />")?;
if self.options.render.figure_with_caption {
if !nl.title.is_empty() {
self.output.write_all(b"<figcaption>")?;
self.escape(nl.title.as_bytes())?;
self.output.write_all(b"</figcaption>")?;
}
self.output.write_all(b"</figure>")?;
}
}
}
#[cfg(feature = "shortcodes")]
Expand Down
16 changes: 16 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,22 @@ pub struct RenderOptions {
/// assert_eq!(str::from_utf8(&buf).unwrap(), "```\nhello\n```\n");
/// ```
pub prefer_fenced: bool,

/// Render the image as a figure element with the title as its caption.
///
/// ```rust
/// # use comrak::{markdown_to_html, Options};
/// let mut options = Options::default();
/// let input = "![image](https://example.com/image.png \"this is an image\")";
///
/// assert_eq!(markdown_to_html(input, &options),
/// "<p><img src=\"https://example.com/image.png\" alt=\"image\" title=\"this is an image\" /></p>\n");
///
/// options.render.figure_with_caption = true;
/// assert_eq!(markdown_to_html(input, &options),
/// "<p><figure><img src=\"https://example.com/image.png\" alt=\"image\" title=\"this is an image\" /><figcaption>this is an image</figcaption></figure></p>\n");
/// ```
pub figure_with_caption: bool,
}

#[non_exhaustive]
Expand Down
20 changes: 20 additions & 0 deletions src/tests/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ fn ignore_setext_heading() {
);
}

#[test]
fn figure_with_caption_with_title() {
html_opts!(
[render.figure_with_caption],
concat!("![image](https://example.com/image.png \"this is an image\")\n"),
concat!("<p><figure><img src=\"https://example.com/image.png\" alt=\"image\" title=\"this is an image\" /><figcaption>this is an image</figcaption></figure></p>\n"),
);
}

#[test]
fn figure_with_caption_without_title() {
html_opts!(
[render.figure_with_caption],
concat!("![image](https://example.com/image.png)\n"),
concat!(
"<p><figure><img src=\"https://example.com/image.png\" alt=\"image\" /></figure></p>\n"
),
);
}

#[test]
fn html_block_1() {
html_opts!(
Expand Down