borrow link def url if on single line

This commit is contained in:
Noah Hellman 2023-01-18 21:44:58 +01:00
parent c7b3aa560b
commit 1dde9e57e2
2 changed files with 18 additions and 3 deletions

View file

@ -268,7 +268,7 @@ pub struct Parser<'s> {
tree: tree::Branch<block::Node, block::Atom>, tree: tree::Branch<block::Node, block::Atom>,
inlines: span::InlineSpans<'s>, inlines: span::InlineSpans<'s>,
inline_parser: Option<inline::Parser<span::InlineCharsIter<'s>>>, inline_parser: Option<inline::Parser<span::InlineCharsIter<'s>>>,
link_definitions: std::collections::HashMap<&'s str, String>, link_definitions: std::collections::HashMap<&'s str, CowStr<'s>>,
_tree_data: block::Tree, _tree_data: block::Tree,
} }
@ -286,8 +286,11 @@ impl<'s> Parser<'s> {
e.kind e.kind
{ {
let tag = e.span.of(src); let tag = e.span.of(src);
// TODO borrow url string if single inline let url = match branch.count_children() {
let url = branch.take_inlines().map(|sp| sp.of(src).trim()).collect(); 0 => "".into(),
1 => branch.take_inlines().next().unwrap().of(src).trim().into(),
_ => branch.take_inlines().map(|sp| sp.of(src).trim()).collect(),
};
defs.insert(tag, url); defs.insert(tag, url);
} }
} }

View file

@ -50,6 +50,18 @@ pub struct Branch<C: 'static, A: 'static> {
} }
impl<C, A> Branch<C, A> { impl<C, A> Branch<C, A> {
/// Count number of direct children nodes.
pub fn count_children(&self) -> usize {
let mut head = self.head;
let mut count = 0;
while let Some(h) = head {
let n = &self.nodes[h.index()];
head = n.next;
count += 1;
}
count
}
/// Retrieve all inlines until the end of the current container. Panics if any upcoming node is /// Retrieve all inlines until the end of the current container. Panics if any upcoming node is
/// not an inline node. /// not an inline node.
pub fn take_inlines(&mut self) -> impl Iterator<Item = Span> + '_ { pub fn take_inlines(&mut self) -> impl Iterator<Item = Span> + '_ {