@@ -27,7 +27,6 @@ use rustc_session::lint;
2727use rustc_span:: edition:: Edition ;
2828use rustc_span:: Span ;
2929use std:: borrow:: Cow ;
30- use std:: cell:: RefCell ;
3130use std:: collections:: VecDeque ;
3231use std:: default:: Default ;
3332use std:: fmt:: Write ;
@@ -39,7 +38,7 @@ use crate::doctest;
3938use crate :: html:: highlight;
4039use crate :: html:: toc:: TocBuilder ;
4140
42- use pulldown_cmark:: { html, CodeBlockKind , CowStr , Event , Options , Parser , Tag } ;
41+ use pulldown_cmark:: { html, BrokenLink , CodeBlockKind , CowStr , Event , Options , Parser , Tag } ;
4342
4443#[ cfg( test) ]
4544mod tests;
@@ -931,15 +930,17 @@ impl Markdown<'_> {
931930 if md. is_empty ( ) {
932931 return String :: new ( ) ;
933932 }
934- let replacer = |_: & str , s : & str | {
935- if let Some ( link) = links. iter ( ) . find ( |link| & * link. original_text == s) {
936- Some ( ( link. href . clone ( ) , link. new_text . clone ( ) ) )
933+ let mut replacer = |broken_link : BrokenLink < ' _ > | {
934+ if let Some ( link) =
935+ links. iter ( ) . find ( |link| & * link. original_text == broken_link. reference )
936+ {
937+ Some ( ( link. href . as_str ( ) . into ( ) , link. new_text . as_str ( ) . into ( ) ) )
937938 } else {
938939 None
939940 }
940941 } ;
941942
942- let p = Parser :: new_with_broken_link_callback ( md, opts ( ) , Some ( & replacer) ) ;
943+ let p = Parser :: new_with_broken_link_callback ( md, opts ( ) , Some ( & mut replacer) ) ;
943944
944945 let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
945946
@@ -1009,9 +1010,11 @@ impl MarkdownSummaryLine<'_> {
10091010 return String :: new ( ) ;
10101011 }
10111012
1012- let replacer = |_: & str , s : & str | {
1013- if let Some ( link) = links. iter ( ) . find ( |link| & * link. original_text == s) {
1014- Some ( ( link. href . clone ( ) , link. new_text . clone ( ) ) )
1013+ let mut replacer = |broken_link : BrokenLink < ' _ > | {
1014+ if let Some ( link) =
1015+ links. iter ( ) . find ( |link| & * link. original_text == broken_link. reference )
1016+ {
1017+ Some ( ( link. href . as_str ( ) . into ( ) , link. new_text . as_str ( ) . into ( ) ) )
10151018 } else {
10161019 None
10171020 }
@@ -1020,7 +1023,7 @@ impl MarkdownSummaryLine<'_> {
10201023 let p = Parser :: new_with_broken_link_callback (
10211024 md,
10221025 Options :: ENABLE_STRIKETHROUGH ,
1023- Some ( & replacer) ,
1026+ Some ( & mut replacer) ,
10241027 ) ;
10251028
10261029 let mut s = String :: new ( ) ;
@@ -1067,7 +1070,7 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
10671070 }
10681071
10691072 let mut links = vec ! [ ] ;
1070- let shortcut_links = RefCell :: new ( vec ! [ ] ) ;
1073+ let mut shortcut_links = vec ! [ ] ;
10711074
10721075 {
10731076 let locate = |s : & str | unsafe {
@@ -1084,11 +1087,13 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
10841087 }
10851088 } ;
10861089
1087- let push = |_: & str , s : & str | {
1088- shortcut_links. borrow_mut ( ) . push ( ( s. to_owned ( ) , locate ( s) ) ) ;
1090+ let mut push = |link : BrokenLink < ' _ > | {
1091+ // FIXME: use `link.span` instead of `locate`
1092+ // (doing it now includes the `[]` as well as the text)
1093+ shortcut_links. push ( ( link. reference . to_owned ( ) , locate ( link. reference ) ) ) ;
10891094 None
10901095 } ;
1091- let p = Parser :: new_with_broken_link_callback ( md, opts ( ) , Some ( & push) ) ;
1096+ let p = Parser :: new_with_broken_link_callback ( md, opts ( ) , Some ( & mut push) ) ;
10921097
10931098 // There's no need to thread an IdMap through to here because
10941099 // the IDs generated aren't going to be emitted anywhere.
@@ -1106,8 +1111,7 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
11061111 }
11071112 }
11081113
1109- let mut shortcut_links = shortcut_links. into_inner ( ) ;
1110- links. extend ( shortcut_links. drain ( ..) ) ;
1114+ links. append ( & mut shortcut_links) ;
11111115
11121116 links
11131117}
0 commit comments