@@ -60,7 +60,7 @@ pub(crate) fn rewrite_links(
60
60
let doc = Parser :: new_with_broken_link_callback ( markdown, MARKDOWN_OPTIONS , Some ( & mut cb) )
61
61
. into_offset_iter ( ) ;
62
62
63
- let doc = map_links ( doc, |target, title, range| {
63
+ let doc = map_links ( doc, |target, title, range, link_type | {
64
64
// This check is imperfect, there's some overlap between valid intra-doc links
65
65
// and valid URLs so we choose to be too eager to try to resolve what might be
66
66
// a URL.
@@ -78,7 +78,7 @@ pub(crate) fn rewrite_links(
78
78
. map ( |( _, attr_id) | attr_id. is_inner_attr ( ) )
79
79
. unwrap_or ( false ) ;
80
80
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 )
82
82
{
83
83
( None , target, title)
84
84
} else if let Some ( target) = rewrite_url_link ( db, definition, target) {
@@ -417,6 +417,7 @@ fn rewrite_intra_doc_link(
417
417
target : & str ,
418
418
title : & str ,
419
419
is_inner_doc : bool ,
420
+ link_type : LinkType ,
420
421
) -> Option < ( String , String ) > {
421
422
let ( link, ns) = parse_intra_doc_link ( target) ;
422
423
@@ -438,7 +439,21 @@ fn rewrite_intra_doc_link(
438
439
url = url. join ( & file) . ok ( ) ?;
439
440
url. set_fragment ( frag) ;
440
441
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) )
442
457
}
443
458
444
459
/// 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> {
470
485
/// Rewrites a markdown document, applying 'callback' to each link.
471
486
fn map_links < ' e > (
472
487
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 ) ,
474
489
) -> impl Iterator < Item = Event < ' e > > {
475
490
let mut in_link = false ;
476
491
// holds the origin link target on start event and the rewritten one on end event
@@ -497,7 +512,7 @@ fn map_links<'e>(
497
512
}
498
513
Event :: Text ( s) if in_link => {
499
514
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 ( ) ) ;
501
516
end_link_target = Some ( CowStr :: Boxed ( link_target_s. into ( ) ) ) ;
502
517
if !matches ! ( end_link_type, Some ( LinkType :: Autolink ) ) {
503
518
end_link_type = link_type;
@@ -506,7 +521,7 @@ fn map_links<'e>(
506
521
}
507
522
Event :: Code ( s) if in_link => {
508
523
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 ( ) ) ;
510
525
end_link_target = Some ( CowStr :: Boxed ( link_target_s. into ( ) ) ) ;
511
526
if !matches ! ( end_link_type, Some ( LinkType :: Autolink ) ) {
512
527
end_link_type = link_type;
0 commit comments