Skip to content

Commit

Permalink
Fix parsing of nested links
Browse files Browse the repository at this point in the history
Enter event for `LabelText` should be inserted before existing events at the index in order
to have correct nesting. Otherwise nested elements could have Enter event first and that
would result in incorrect nesting in the tree when converting to AST.

Closes GH-73.
Closes GH-50.
  • Loading branch information
sheremetyev authored Jul 10, 2023
1 parent 24d78c3 commit ddbeb9c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/construct/label_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ fn inject_labels(tokenizer: &mut Tokenizer, labels: &[Label]) {
// Though: if this was what looked like a footnote, but didn’t match,
// it’s a link instead, and we need to inject the `^`.
if label.start.1 != label.end.0 || !caret.is_empty() {
tokenizer.map.add(
tokenizer.map.add_before(
label.start.1 + 1,
0,
vec![Event {
Expand Down
24 changes: 23 additions & 1 deletion tests/link_resource.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use markdown::{
mdast::{Link, Node, Paragraph, Root, Text},
mdast::{Image, Link, Node, Paragraph, Root, Text},
to_html, to_html_with_options, to_mdast,
unist::Position,
CompileOptions, Options,
Expand Down Expand Up @@ -511,5 +511,27 @@ fn link_resource() -> Result<(), String> {
"should support link (resource) as `Link`s in mdast"
);

assert_eq!(
to_mdast("[![name](image)](url)", &Default::default())?,
Node::Root(Root {
children: vec![Node::Paragraph(Paragraph {
children: vec![Node::Link(Link {
children: vec![Node::Image(Image {
alt: "name".into(),
url: "image".into(),
title: None,
position: Some(Position::new(1, 2, 1, 1, 16, 15)),
}),],
url: "url".into(),
title: None,
position: Some(Position::new(1, 1, 0, 1, 22, 21)),
}),],
position: Some(Position::new(1, 1, 0, 1, 22, 21)),
}),],
position: Some(Position::new(1, 1, 0, 1, 22, 21))
}),
"should support nested links in mdast"
);

Ok(())
}

0 comments on commit ddbeb9c

Please sign in to comment.