determine doc link type from start instead of text or code

This commit is contained in:
Raymond Luo 2022-06-20 21:10:45 -04:00
parent eb9b360752
commit c6f776c5f9

View file

@ -362,14 +362,15 @@ fn map_links<'e>(
// holds the origin link target on start event and the rewritten one on end event // holds the origin link target on start event and the rewritten one on end event
let mut end_link_target: Option<CowStr> = None; let mut end_link_target: Option<CowStr> = None;
// normally link's type is determined by the type of link tag in the end event, // normally link's type is determined by the type of link tag in the end event,
// however in same cases we want to change the link type, for example, // however in some cases we want to change the link type, for example,
// `Shortcut` type doesn't make sense for url links // `Shortcut` type doesn't make sense for url links
let mut end_link_type: Option<LinkType> = None; let mut end_link_type: Option<LinkType> = None;
events.map(move |evt| match evt { events.map(move |evt| match evt {
Event::Start(Tag::Link(_, ref target, _)) => { Event::Start(Tag::Link(link_type, ref target, _)) => {
in_link = true; in_link = true;
end_link_target = Some(target.clone()); end_link_target = Some(target.clone());
end_link_type = Some(link_type);
evt evt
} }
Event::End(Tag::Link(link_type, target, _)) => { Event::End(Tag::Link(link_type, target, _)) => {
@ -381,17 +382,13 @@ fn map_links<'e>(
)) ))
} }
Event::Text(s) if in_link => { Event::Text(s) if in_link => {
let (link_type, link_target_s, link_name) = let (_, link_target_s, link_name) = callback(&end_link_target.take().unwrap(), &s);
callback(&end_link_target.take().unwrap(), &s);
end_link_target = Some(CowStr::Boxed(link_target_s.into())); end_link_target = Some(CowStr::Boxed(link_target_s.into()));
end_link_type = link_type;
Event::Text(CowStr::Boxed(link_name.into())) Event::Text(CowStr::Boxed(link_name.into()))
} }
Event::Code(s) if in_link => { Event::Code(s) if in_link => {
let (link_type, link_target_s, link_name) = let (_, link_target_s, link_name) = callback(&end_link_target.take().unwrap(), &s);
callback(&end_link_target.take().unwrap(), &s);
end_link_target = Some(CowStr::Boxed(link_target_s.into())); end_link_target = Some(CowStr::Boxed(link_target_s.into()));
end_link_type = link_type;
Event::Code(CowStr::Boxed(link_name.into())) Event::Code(CowStr::Boxed(link_name.into()))
} }
_ => evt, _ => evt,