@@ -23,79 +23,53 @@ enum BrokenLinkReason {
2323    MultipleLines , 
2424} 
2525
26- #[ derive( Debug ) ]  
27- enum  State  { 
28-     ProcessingLinkText , 
29-     ProcessedLinkText , 
30-     ProcessingLinkUrl ( UrlState ) , 
31- } 
32- 
33- #[ derive( Debug ) ]  
34- enum  UrlState  { 
35-     Empty , 
36-     FilledEntireSingleLine , 
37-     FilledBrokenMultipleLines , 
38- } 
39- 
4026fn  warn_if_broken_link ( cx :  & LateContext < ' _ > ,  bl :  & PullDownBrokenLink < ' _ > ,  doc :  & String ,  fragments :  & Vec < DocFragment > )  { 
4127    if  let  Some ( span)  = source_span_for_markdown_range ( cx. tcx ,  doc,  & bl. span ,  fragments)  { 
42-         // `PullDownBrokenLink` provided by pulldown_cmark always 
43-         // start with `[` which makes pulldown_cmark consider this a link tag. 
44-         let  mut  state = State :: ProcessingLinkText ; 
28+         let  mut  len = 0 ; 
4529
46-         // Whether it was detected a line break within the  link tag url part.  
47-         let  mut  reading_link_url_new_line  = false ; 
30+         // grab raw  link data  
31+         let  ( _ ,  raw_link )  = doc . split_at ( bl . span . start ) ; 
4832
49-         // Skip the first char because we already know it is a `[` char. 
50-         for  ( abs_pos,  c)  in  doc. char_indices ( ) . skip ( bl. span . start  + 1 )  { 
51-             match  & state { 
52-                 State :: ProcessingLinkText  => { 
53-                     if  c == ']'  { 
54-                         state = State :: ProcessedLinkText ; 
55-                     } 
56-                 } , 
57-                 State :: ProcessedLinkText  => { 
58-                     if  c == '('  { 
59-                         state = State :: ProcessingLinkUrl ( UrlState :: Empty ) ; 
60-                     }  else  { 
61-                         // not a real link, just skip it without reporting a broken link for it. 
62-                         break ; 
63-                     } 
64-                 } , 
65-                 State :: ProcessingLinkUrl ( url_state)  => { 
66-                     if  c == '\n'  { 
67-                         reading_link_url_new_line = true ; 
68-                         continue ; 
69-                     } 
33+         // strip off link text part 
34+         let  raw_link = match  raw_link. split_once ( ']' )  { 
35+             None  => return , 
36+             Some ( ( prefix,  suffix) )  => { 
37+                 len += prefix. len ( )  + 1 ; 
38+                 suffix
39+             } , 
40+         } ; 
7041
71-                     if  c == ')'  { 
72-                         // record full broken link tag 
73-                         if  let  UrlState :: FilledBrokenMultipleLines  = url_state { 
74-                             let  offset = abs_pos - bl. span . start ; 
75-                             report_broken_link ( cx,  span,  offset,  BrokenLinkReason :: MultipleLines ) ; 
76-                         } 
77-                         break ; 
78-                     } 
42+         let  raw_link = match  raw_link. split_once ( '(' )  { 
43+             None  => return , 
44+             Some ( ( prefix,  suffix) )  => { 
45+                 if  !prefix. is_empty ( )  { 
46+                     // there is text between ']' and '(' chars, so it is not a valid link 
47+                     return ; 
48+                 } 
49+                 len += prefix. len ( )  + 1 ; 
50+                 suffix
51+             } , 
52+         } ; 
7953
80-                     if  !c. is_whitespace ( )  { 
81-                         if  reading_link_url_new_line { 
82-                             // It was reading a link url which was entirely in a single line, but a new char 
83-                             // was found in this new line which turned the url into a broken state. 
84-                             state = State :: ProcessingLinkUrl ( UrlState :: FilledBrokenMultipleLines ) ; 
85-                             continue ; 
86-                         } 
54+         for  c in  raw_link. chars ( )  { 
55+             if  c == ')'  { 
56+                 // it is a valid link 
57+                 return ; 
58+             } 
8759
88-                         state = State :: ProcessingLinkUrl ( UrlState :: FilledEntireSingleLine ) ; 
89-                     } 
90-                 } , 
91-             } ; 
60+             if  c == '\n'  { 
61+                 // detected break line within the url part 
62+                 report_broken_link ( cx,  span,  len,  BrokenLinkReason :: MultipleLines ) ; 
63+                 break ; 
64+             } 
65+             len += 1 ; 
9266        } 
9367    } 
9468} 
9569
9670fn  report_broken_link ( cx :  & LateContext < ' _ > ,  frag_span :  Span ,  offset :  usize ,  reason :  BrokenLinkReason )  { 
9771    let  start = frag_span. lo ( ) ; 
98-     let  end = start + BytePos :: from_usize ( offset +  5 ) ; 
72+     let  end = start + BytePos :: from_usize ( offset) ; 
9973
10074    let  span = Span :: new ( start,  end,  frag_span. ctxt ( ) ,  frag_span. parent ( ) ) ; 
10175
0 commit comments