Skip to content

Commit ddbeb9c

Browse files
authored
Fix parsing of nested links
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.
1 parent 24d78c3 commit ddbeb9c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/construct/label_end.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ fn inject_labels(tokenizer: &mut Tokenizer, labels: &[Label]) {
752752
// Though: if this was what looked like a footnote, but didn’t match,
753753
// it’s a link instead, and we need to inject the `^`.
754754
if label.start.1 != label.end.0 || !caret.is_empty() {
755-
tokenizer.map.add(
755+
tokenizer.map.add_before(
756756
label.start.1 + 1,
757757
0,
758758
vec![Event {

tests/link_resource.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use markdown::{
2-
mdast::{Link, Node, Paragraph, Root, Text},
2+
mdast::{Image, Link, Node, Paragraph, Root, Text},
33
to_html, to_html_with_options, to_mdast,
44
unist::Position,
55
CompileOptions, Options,
@@ -511,5 +511,27 @@ fn link_resource() -> Result<(), String> {
511511
"should support link (resource) as `Link`s in mdast"
512512
);
513513

514+
assert_eq!(
515+
to_mdast("[![name](image)](url)", &Default::default())?,
516+
Node::Root(Root {
517+
children: vec![Node::Paragraph(Paragraph {
518+
children: vec![Node::Link(Link {
519+
children: vec![Node::Image(Image {
520+
alt: "name".into(),
521+
url: "image".into(),
522+
title: None,
523+
position: Some(Position::new(1, 2, 1, 1, 16, 15)),
524+
}),],
525+
url: "url".into(),
526+
title: None,
527+
position: Some(Position::new(1, 1, 0, 1, 22, 21)),
528+
}),],
529+
position: Some(Position::new(1, 1, 0, 1, 22, 21)),
530+
}),],
531+
position: Some(Position::new(1, 1, 0, 1, 22, 21))
532+
}),
533+
"should support nested links in mdast"
534+
);
535+
514536
Ok(())
515537
}

0 commit comments

Comments
 (0)