| 1 | //! Links |
| 2 | //! |
| 3 | //! `` |
| 4 | //! |
| 5 | //! <https://spec.commonmark.org/0.30/#links> |
| 6 | use crate::generics::inline::full_link; |
| 7 | use crate::{MarkdownIt, Node, NodeValue, Renderer}; |
| 8 | |
| 9 | #[derive(Debug)] |
| 10 | pub struct Link { |
| 11 | pub url: String, |
| 12 | pub title: Option<String>, |
| 13 | } |
| 14 | |
| 15 | impl 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 | |
| 30 | pub fn add(md: &mut MarkdownIt) { |
| 31 | full_link::add::<false>(md, |href, title| Node::new(Link { |
| 32 | url: href.unwrap_or_default(), |
| 33 | title, |
| 34 | })); |
| 35 | } |