markdown_that/plugins/cmark/inline/
link.rs

1//! Links
2//!
3//! `![link](<to> "stuff")`
4//!
5//! <https://spec.commonmark.org/0.30/#links>
6use crate::generics::inline::full_link;
7use crate::{MarkdownThat, Node, NodeValue, Renderer};
8
9#[derive(Debug)]
10pub struct Link {
11    pub url: String,
12    pub title: Option<String>,
13}
14
15impl NodeValue for Link {
16    fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
17        let mut attrs = node.attrs.clone();
18        attrs.push(("href", self.url.clone()));
19
20        if let Some(title) = &self.title {
21            attrs.push(("title", title.clone()));
22        }
23
24        fmt.open("a", &attrs);
25        fmt.contents(&node.children);
26        fmt.close("a");
27    }
28}
29
30pub fn add(md: &mut MarkdownThat) {
31    full_link::add::<false>(md, |href, title| {
32        Node::new(Link {
33            url: href.unwrap_or_default(),
34            title,
35        })
36    });
37}