Skip to content

Commit

Permalink
parse: do not inline parse link definitions
Browse files Browse the repository at this point in the history
better match the actual url produced, which is verbatim
  • Loading branch information
hellux committed May 15, 2023
1 parent 7c28a06 commit bbdb314
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl<'s> TreeParser<'s> {
k: &Kind,
span_start: Span,
span_end: Span,
lines: &mut [Span],
mut lines: &mut [Span],
) {
if let Kind::Fenced { indent, .. } = k {
for line in lines.iter_mut() {
Expand All @@ -367,6 +367,18 @@ impl<'s> TreeParser<'s> {
*line = line.trim_start(self.src);
}

// skip first inline if empty
if lines.get(0).map_or(false, |l| l.is_empty()) {
lines = &mut lines[1..];
};

if matches!(leaf, LinkDefinition { .. }) {
// trim ending whitespace of each inline
for line in lines.iter_mut() {
*line = line.trim_end(self.src);
}
}

// trim ending whitespace of block
let l = lines.len();
if l > 0 {
Expand Down Expand Up @@ -412,7 +424,7 @@ impl<'s> TreeParser<'s> {
}

// trim '#' characters
for line in lines[1..].iter_mut() {
for line in lines.iter_mut().skip(1) {
*line = line.trim_start_matches(self.src, |c| c == '#' || c.is_whitespace());
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ impl<'s> Parser<'s> {
},
block::Leaf::Caption => Container::Caption,
block::Leaf::LinkDefinition { label } => {
self.verbatim = enter;
Container::LinkDefinition { label }
}
}
Expand Down Expand Up @@ -1709,7 +1710,6 @@ mod test {
Blankline,
Start(LinkDefinition { label: "tag" }, Attributes::new()),
Str("u".into()),
Softbreak,
Str("rl".into()),
End(LinkDefinition { label: "tag" }),
);
Expand All @@ -1718,19 +1718,24 @@ mod test {
"[text][tag]\n",
"\n",
"[tag]:\n",
" url\n", //
" url\n", //
" cont\n", //
),
Start(Paragraph, Attributes::new()),
Start(
Link("url".into(), LinkType::Span(SpanLinkType::Reference)),
Link("urlcont".into(), LinkType::Span(SpanLinkType::Reference)),
Attributes::new()
),
Str("text".into()),
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
End(Link(
"urlcont".into(),
LinkType::Span(SpanLinkType::Reference)
)),
End(Paragraph),
Blankline,
Start(LinkDefinition { label: "tag" }, Attributes::new()),
Str("url".into()),
Str("cont".into()),
End(LinkDefinition { label: "tag" }),
);
}
Expand Down

0 comments on commit bbdb314

Please sign in to comment.