| 1 | //! Images |
| 2 | //! |
| 3 | //! `` |
| 4 | //! |
| 5 | //! <https://spec.commonmark.org/0.30/#images> |
| 6 | use crate::generics::inline::full_link; |
| 7 | use crate::{MarkdownIt, Node, NodeValue, Renderer}; |
| 8 | |
| 9 | #[derive(Debug)] |
| 10 | pub struct Image { |
| 11 | pub url: String, |
| 12 | pub title: Option<String>, |
| 13 | } |
| 14 | |
| 15 | impl NodeValue for Image { |
| 16 | fn render(&self, node: &Node, fmt: &mut dyn Renderer) { |
| 17 | let mut attrs = node.attrs.clone(); |
| 18 | attrs.push(("src", self.url.clone())); |
| 19 | attrs.push(("alt", node.collect_text())); |
| 20 | |
| 21 | if let Some(title) = &self.title { |
| 22 | attrs.push(("title", title.clone())); |
| 23 | } |
| 24 | |
| 25 | fmt.self_close("img", &attrs); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | pub fn add(md: &mut MarkdownIt) { |
| 30 | full_link::add_prefix::<'!', true>(md, |href, title| Node::new(Image { |
| 31 | url: href.unwrap_or_default(), |
| 32 | title, |
| 33 | })); |
| 34 | } |