markdown_that/plugins/cmark/inline/
image.rs1use crate::generics::inline::full_link;
7use crate::{MarkdownThat, Node, NodeValue, Renderer};
8
9#[derive(Debug)]
10pub struct Image {
11 pub url: String,
12 pub title: Option<String>,
13}
14
15impl 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
29pub fn add(md: &mut MarkdownThat) {
30 full_link::add_prefix::<'!', true>(md, |href, title| {
31 Node::new(Image {
32 url: href.unwrap_or_default(),
33 title,
34 })
35 });
36}