diff --git a/src/html.rs b/src/html.rs index abe29be1..3dbcae85 100644 --- a/src/html.rs +++ b/src/html.rs @@ -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"
")?; + } self.output.write_all(b" 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"
")?; + self.escape(nl.title.as_bytes())?; + self.output.write_all(b"
")?; + } + self.output.write_all(b"
")?; + } } } #[cfg(feature = "shortcodes")] diff --git a/src/parser/mod.rs b/src/parser/mod.rs index b506b502..96e83d10 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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), + /// "

\"image\"

\n"); + /// + /// options.render.figure_with_caption = true; + /// assert_eq!(markdown_to_html(input, &options), + /// "

\"image\"
this is an image

\n"); + /// ``` + pub figure_with_caption: bool, } #[non_exhaustive] diff --git a/src/tests/core.rs b/src/tests/core.rs index 848426c0..714ced6b 100644 --- a/src/tests/core.rs +++ b/src/tests/core.rs @@ -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!("

\"image\"
this is an image

\n"), + ); +} + +#[test] +fn figure_with_caption_without_title() { + html_opts!( + [render.figure_with_caption], + concat!("![image](https://example.com/image.png)\n"), + concat!( + "

\"image\"

\n" + ), + ); +} + #[test] fn html_block_1() { html_opts!(