@@ -12,40 +12,48 @@ impl IgnoreBlocks {
12
12
let mut ignore = Vec :: new ( ) ;
13
13
let mut parser = Parser :: new ( s) . into_offset_iter ( ) ;
14
14
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 {
32
20
ignore. push( start..range. end) ;
33
21
break ;
34
22
}
35
23
}
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 { .. } ) ;
36
35
}
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
+ }
43
49
}
44
50
}
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
+ _ => { }
49
57
}
50
58
}
51
59
@@ -319,3 +327,25 @@ fn cbs_13() {
319
327
] ,
320
328
) ;
321
329
}
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( "" ) ,
339
+ [ Ignore :: Yes ( "" ) ]
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