Skip to content

Commit b2e582b

Browse files
authored
Merge pull request #2020 from ehuss/ignore-more
Ignore elements in markdown links
2 parents 9320907 + 919b375 commit b2e582b

File tree

1 file changed

+57
-27
lines changed

1 file changed

+57
-27
lines changed

parser/src/ignore_block.rs

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,48 @@ impl IgnoreBlocks {
1212
let mut ignore = Vec::new();
1313
let mut parser = Parser::new(s).into_offset_iter();
1414
while let Some((event, range)) = parser.next() {
15-
if let Event::Start(Tag::CodeBlock(_)) = event {
16-
let start = range.start;
17-
while let Some((event, range)) = parser.next() {
18-
if let Event::End(TagEnd::CodeBlock) = event {
19-
ignore.push(start..range.end);
20-
break;
21-
}
22-
}
23-
} else if let Event::Start(Tag::BlockQuote(_)) = event {
24-
let start = range.start;
25-
let mut count = 1;
26-
while let Some((event, range)) = parser.next() {
27-
if let Event::Start(Tag::BlockQuote(_)) = event {
28-
count += 1;
29-
} else if let Event::End(TagEnd::BlockQuote(_)) = event {
30-
count -= 1;
31-
if count == 0 {
15+
macro_rules! ignore_till_end {
16+
($p:pat) => {
17+
let start = range.start;
18+
while let Some((event, range)) = parser.next() {
19+
if let Event::End($p) = event {
3220
ignore.push(start..range.end);
3321
break;
3422
}
3523
}
24+
};
25+
}
26+
match event {
27+
Event::Start(Tag::CodeBlock(_)) => {
28+
ignore_till_end!(TagEnd::CodeBlock);
29+
}
30+
Event::Start(Tag::Link { .. }) => {
31+
ignore_till_end!(TagEnd::Link { .. });
32+
}
33+
Event::Start(Tag::Image { .. }) => {
34+
ignore_till_end!(TagEnd::Image { .. });
3635
}
37-
} else if let Event::Start(Tag::HtmlBlock) = event {
38-
let start = range.start;
39-
while let Some((event, range)) = parser.next() {
40-
if let Event::End(TagEnd::HtmlBlock) = event {
41-
ignore.push(start..range.end);
42-
break;
36+
Event::Start(Tag::BlockQuote(_)) => {
37+
let start = range.start;
38+
let mut count = 1;
39+
while let Some((event, range)) = parser.next() {
40+
if let Event::Start(Tag::BlockQuote(_)) = event {
41+
count += 1;
42+
} else if let Event::End(TagEnd::BlockQuote(_)) = event {
43+
count -= 1;
44+
if count == 0 {
45+
ignore.push(start..range.end);
46+
break;
47+
}
48+
}
4349
}
4450
}
45-
} else if let Event::InlineHtml(_) = event {
46-
ignore.push(range);
47-
} else if let Event::Code(_) = event {
48-
ignore.push(range);
51+
Event::Start(Tag::HtmlBlock) => {
52+
ignore_till_end!(TagEnd::HtmlBlock);
53+
}
54+
Event::InlineHtml(_) => ignore.push(range),
55+
Event::Code(_) => ignore.push(range),
56+
_ => {}
4957
}
5058
}
5159

@@ -319,3 +327,25 @@ fn cbs_13() {
319327
],
320328
);
321329
}
330+
331+
#[test]
332+
fn ignore_link() {
333+
assert_eq!(
334+
bodies("[This is a link](https://example.com)"),
335+
[Ignore::Yes("[This is a link](https://example.com)")]
336+
);
337+
assert_eq!(
338+
bodies("![This is an image](foo.png)"),
339+
[Ignore::Yes("![This is an image](foo.png)")]
340+
);
341+
342+
// Unfortunately pulldown_cmark does not give ranges for the link
343+
// definition.
344+
assert_eq!(
345+
bodies("[Link from def]\n\n[Link from def]: https://example.com"),
346+
[
347+
Ignore::Yes("[Link from def]"),
348+
Ignore::No("\n\n[Link from def]: https://example.com")
349+
]
350+
);
351+
}

0 commit comments

Comments
 (0)