Skip to content

Commit 7ad1cd7

Browse files
committed
fix: #38 - Fix reference links disappearing when appearing anywhere after a table.
1 parent f4c47bc commit 7ad1cd7

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "dprint-plugin-markdown"
33
description = "Markdown formatter for dprint."
4-
version = "0.6.0"
4+
version = "0.6.1"
55
authors = ["David Sherret <[email protected]>"]
66
license = "MIT"
77
edition = "2018"

src/parsing/cmark/parse_cmark_ast.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct EventIterator<'a> {
88
last_range: Range,
99
next: Option<(Event<'a>, Range)>,
1010
allow_empty_text_events: bool,
11+
in_table_count: i8,
1112
}
1213

1314
impl<'a> EventIterator<'a> {
@@ -23,22 +24,23 @@ impl<'a> EventIterator<'a> {
2324
},
2425
next,
2526
allow_empty_text_events: false,
27+
in_table_count: 0,
2628
}
2729
}
2830

2931
pub fn next(&mut self) -> Option<Event<'a>> {
3032
if let Some((event, range)) = self.next.take() {
3133
// println!("{:?} {:?}", range, event);
3234
self.last_range = range;
33-
self.next = self.iterator.next();
35+
self.next = self.move_iterator_next();
3436

3537
if !self.allow_empty_text_events {
3638
// skip over any empty text or html events
3739
loop {
3840
match &self.next {
3941
Some((Event::Text(text), _)) | Some((Event::Html(text), _)) => {
4042
if text.trim().is_empty() {
41-
self.next = self.iterator.next();
43+
self.next = self.move_iterator_next();
4244
} else {
4345
break;
4446
}
@@ -54,6 +56,22 @@ impl<'a> EventIterator<'a> {
5456
}
5557
}
5658

59+
fn move_iterator_next(&mut self) -> Option<(Event<'a>, Range)> {
60+
let next = self.iterator.next();
61+
62+
match next {
63+
Some((Event::Start(Tag::Table(_)), _)) => self.in_table_count += 1,
64+
Some((Event::End(Tag::Table(_)), _)) => self.in_table_count -= 1,
65+
_ => {}
66+
}
67+
68+
next
69+
}
70+
71+
pub fn is_in_table(&self) -> bool {
72+
self.in_table_count > 0
73+
}
74+
5775
pub fn start(&self) -> usize {
5876
self.last_range.start
5977
}
@@ -91,19 +109,12 @@ pub fn parse_cmark_ast(markdown_text: &str) -> Result<SourceFile, ParseError> {
91109
let mut children: Vec<Node> = Vec::new();
92110
let mut iterator = EventIterator::new(markdown_text, Parser::new_ext(markdown_text, options).into_offset_iter());
93111
let mut last_event_range: Option<Range> = None;
94-
let mut is_in_table = 0;
95112

96113
while let Some(event) = iterator.next() {
97-
match event {
98-
Event::Start(Tag::Table(_)) => is_in_table += 1,
99-
Event::End(Tag::Table(_)) => is_in_table += 1,
100-
_ => {}
101-
}
102-
103114
let current_range = iterator.get_last_range();
104115

105116
// do not parse for link references while inside a table
106-
if is_in_table == 0 {
117+
if !iterator.is_in_table() {
107118
if let Some(references) = parse_references(&last_event_range, current_range.start, &mut iterator)? {
108119
children.push(references);
109120
}

tests/specs/Issues/Issue0038.txt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
!! should handle reference links not at the end of the file !!
2+
3+
# Test
4+
5+
| Company |
6+
| -------------------------- |
7+
| [GitHub][github] |
8+
| [Google][google] |
9+
| [Amazon Web Services][aws] |
10+
11+
[github]: https://github.com
12+
13+
test
14+
15+
[google]: https://google.com
16+
17+
A paragraph.
18+
19+
[aws]: https://aws.amazon.com
20+
21+
[expect]
22+
# Test
23+
24+
| Company |
25+
| -------------------------- |
26+
| [GitHub][github] |
27+
| [Google][google] |
28+
| [Amazon Web Services][aws] |
29+
30+
[github]: https://github.com
31+
32+
test
33+
34+
[google]: https://google.com
35+
36+
A paragraph.
37+
38+
[aws]: https://aws.amazon.com

0 commit comments

Comments
 (0)