@@ -60,7 +60,7 @@ pub(crate) fn rewrite_links(
6060 let doc = Parser :: new_with_broken_link_callback ( markdown, MARKDOWN_OPTIONS , Some ( & mut cb) )
6161 . into_offset_iter ( ) ;
6262
63- let doc = map_links ( doc, |target, title, range| {
63+ let doc = map_links ( doc, |target, title, range, link_type | {
6464 // This check is imperfect, there's some overlap between valid intra-doc links
6565 // and valid URLs so we choose to be too eager to try to resolve what might be
6666 // a URL.
@@ -78,7 +78,7 @@ pub(crate) fn rewrite_links(
7878 . map ( |( _, attr_id) | attr_id. is_inner_attr ( ) )
7979 . unwrap_or ( false ) ;
8080 if let Some ( ( target, title) ) =
81- rewrite_intra_doc_link ( db, definition, target, title, is_inner_doc)
81+ rewrite_intra_doc_link ( db, definition, target, title, is_inner_doc, link_type )
8282 {
8383 ( None , target, title)
8484 } else if let Some ( target) = rewrite_url_link ( db, definition, target) {
@@ -417,6 +417,7 @@ fn rewrite_intra_doc_link(
417417 target : & str ,
418418 title : & str ,
419419 is_inner_doc : bool ,
420+ link_type : LinkType ,
420421) -> Option < ( String , String ) > {
421422 let ( link, ns) = parse_intra_doc_link ( target) ;
422423
@@ -438,7 +439,21 @@ fn rewrite_intra_doc_link(
438439 url = url. join ( & file) . ok ( ) ?;
439440 url. set_fragment ( frag) ;
440441
441- Some ( ( url. into ( ) , strip_prefixes_suffixes ( title) . to_owned ( ) ) )
442+ // We want to strip the keyword prefix from the title, but only if the target is implicitly the same
443+ // as the title.
444+ let title = match link_type {
445+ LinkType :: Email
446+ | LinkType :: Autolink
447+ | LinkType :: Shortcut
448+ | LinkType :: Collapsed
449+ | LinkType :: Reference
450+ | LinkType :: Inline => title. to_owned ( ) ,
451+ LinkType :: ShortcutUnknown | LinkType :: CollapsedUnknown | LinkType :: ReferenceUnknown => {
452+ strip_prefixes_suffixes ( title) . to_owned ( )
453+ }
454+ } ;
455+
456+ Some ( ( url. into ( ) , title) )
442457}
443458
444459/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
@@ -470,7 +485,7 @@ fn mod_path_of_def(db: &RootDatabase, def: Definition) -> Option<String> {
470485/// Rewrites a markdown document, applying 'callback' to each link.
471486fn map_links < ' e > (
472487 events : impl Iterator < Item = ( Event < ' e > , Range < usize > ) > ,
473- callback : impl Fn ( & str , & str , Range < usize > ) -> ( Option < LinkType > , String , String ) ,
488+ callback : impl Fn ( & str , & str , Range < usize > , LinkType ) -> ( Option < LinkType > , String , String ) ,
474489) -> impl Iterator < Item = Event < ' e > > {
475490 let mut in_link = false ;
476491 // holds the origin link target on start event and the rewritten one on end event
@@ -497,7 +512,7 @@ fn map_links<'e>(
497512 }
498513 Event :: Text ( s) if in_link => {
499514 let ( link_type, link_target_s, link_name) =
500- callback ( & end_link_target. take ( ) . unwrap ( ) , & s, range) ;
515+ callback ( & end_link_target. take ( ) . unwrap ( ) , & s, range, end_link_type . unwrap ( ) ) ;
501516 end_link_target = Some ( CowStr :: Boxed ( link_target_s. into ( ) ) ) ;
502517 if !matches ! ( end_link_type, Some ( LinkType :: Autolink ) ) {
503518 end_link_type = link_type;
@@ -506,7 +521,7 @@ fn map_links<'e>(
506521 }
507522 Event :: Code ( s) if in_link => {
508523 let ( link_type, link_target_s, link_name) =
509- callback ( & end_link_target. take ( ) . unwrap ( ) , & s, range) ;
524+ callback ( & end_link_target. take ( ) . unwrap ( ) , & s, range, end_link_type . unwrap ( ) ) ;
510525 end_link_target = Some ( CowStr :: Boxed ( link_target_s. into ( ) ) ) ;
511526 if !matches ! ( end_link_type, Some ( LinkType :: Autolink ) ) {
512527 end_link_type = link_type;
0 commit comments