inline: fix label of empty multi-line ref links

e.g.

    [some
    text][]

    [some text]: url
This commit is contained in:
Noah Hellman 2023-06-25 18:03:46 +02:00
parent 253d1d2d4b
commit b60650dd0d
2 changed files with 59 additions and 24 deletions

View file

@ -732,29 +732,27 @@ impl<'s> Parser<'s> {
image, image,
} => { } => {
let span_spec = self.events[e_opener].span.end..self.input.span.start; let span_spec = self.events[e_opener].span.end..self.input.span.start;
let multiline = let multiline_spec =
self.events[e_opener].span.start < self.input.span_line.start; self.events[e_opener].span.start < self.input.span_line.start;
let spec: CowStr = if span_spec.is_empty() && !inline { let spec: CowStr = if span_spec.is_empty() && !inline {
let span_spec = self.events[event_span].span.end
..self.events[e_opener - 1].span.start;
let events_text = self let events_text = self
.events .events
.iter() .iter()
.skip(event_span + 1) .skip(event_span + 1)
.take(e_opener - event_span - 2); .take(e_opener - event_span - 2);
if multiline
|| events_text.clone().any(|ev| {
!matches!(ev.kind, EventKind::Str | EventKind::Atom(..))
})
{
let mut spec = String::new(); let mut spec = String::new();
let mut span = 0..0; let mut span = 0..0;
for ev in events_text.filter(|ev| { for ev in events_text.filter(|ev| {
matches!(ev.kind, EventKind::Str | EventKind::Atom(..)) matches!(ev.kind, EventKind::Str | EventKind::Atom(..))
&& !matches!(ev.kind, EventKind::Atom(Escape))
}) { }) {
if span.end == ev.span.start { if matches!(ev.kind, EventKind::Atom(Softbreak | Hardbreak)) {
spec.push_str(&self.input.src[span.clone()]);
spec.push(' ');
span = ev.span.end..ev.span.end;
} else if span.end == ev.span.start {
span.end = ev.span.end; span.end = ev.span.end;
} else { } else {
spec.push_str(&self.input.src[span.clone()]); spec.push_str(&self.input.src[span.clone()]);
@ -763,10 +761,7 @@ impl<'s> Parser<'s> {
} }
spec.push_str(&self.input.src[span]); spec.push_str(&self.input.src[span]);
spec.into() spec.into()
} else { } else if multiline_spec {
self.input.src[span_spec].into()
}
} else if multiline {
let mut spec = String::new(); let mut spec = String::new();
let mut first_part = true; let mut first_part = true;
let mut span = let mut span =

View file

@ -1697,6 +1697,46 @@ mod test {
); );
} }
#[test]
fn link_reference_multiline_empty() {
test_parse!(
concat!(
"> [a\n", //
"> b][]\n", //
"> [a\\\n", //
"> b][]\n", //
"\n", //
"[a b]: url\n", //
),
Start(Blockquote, Attributes::new()),
Start(Paragraph, Attributes::new()),
Start(
Link("url".into(), LinkType::Span(SpanLinkType::Reference)),
Attributes::new()
),
Str("a".into()),
Softbreak,
Str("b".into()),
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
Softbreak,
Start(
Link("url".into(), LinkType::Span(SpanLinkType::Reference)),
Attributes::new()
),
Str("a".into()),
Escape,
Hardbreak,
Str("b".into()),
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
End(Paragraph),
End(Blockquote),
Blankline,
Start(LinkDefinition { label: "a b" }, Attributes::new()),
Str("url".into()),
End(LinkDefinition { label: "a b" }),
);
}
#[test] #[test]
fn link_definition_multiline() { fn link_definition_multiline() {
test_parse!( test_parse!(