Auto merge of #16385 - Urhengulas:fix-tra-doc-links, r=Veykril

Fix intra doc links

Fixes #15848
This commit is contained in:
bors 2024-01-18 14:00:35 +00:00
commit 6d3141679d
2 changed files with 38 additions and 8 deletions

View file

@ -64,14 +64,13 @@ pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: Defin
// * path-based links: `../../module/struct.MyStruct.html`
// * module-based links (AKA intra-doc links): `super::super::module::MyStruct`
if let Some((target, title)) = rewrite_intra_doc_link(db, definition, target, title) {
return (None, target, title);
}
if let Some(target) = rewrite_url_link(db, definition, target) {
return (Some(LinkType::Inline), target, title.to_string());
}
(None, target, title)
} else if let Some(target) = rewrite_url_link(db, definition, target) {
(Some(LinkType::Inline), target, title.to_string())
} else {
(None, target.to_string(), title.to_string())
}
}
});
let mut out = String::new();
cmark_resume_with_options(
@ -369,16 +368,21 @@ fn rewrite_intra_doc_link(
) -> Option<(String, String)> {
let (link, ns) = parse_intra_doc_link(target);
let (link, anchor) = match link.split_once('#') {
Some((new_link, anchor)) => (new_link, Some(anchor)),
None => (link, None),
};
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
let mut url = get_doc_base_urls(db, resolved, None, None).0?;
let (_, file, frag) = filename_and_frag_for_def(db, resolved)?;
let (_, file, _) = filename_and_frag_for_def(db, resolved)?;
if let Some(path) = mod_path_of_def(db, resolved) {
url = url.join(&path).ok()?;
}
url = url.join(&file).ok()?;
url.set_fragment(frag.as_deref());
url.set_fragment(anchor);
Some((url.into(), strip_prefixes_suffixes(title).to_string()))
}

View file

@ -664,3 +664,29 @@ pub struct $0Foo;
expect![["[`foo`]"]],
);
}
#[test]
fn rewrite_intra_doc_link() {
check_rewrite(
r#"
//- minicore: eq, derive
//- /main.rs crate:foo
//! $0[PartialEq]
fn main() {}
"#,
expect!["[PartialEq](https://doc.rust-lang.org/stable/core/cmp/trait.PartialEq.html)"],
);
}
#[test]
fn rewrite_intra_doc_link_with_anchor() {
check_rewrite(
r#"
//- minicore: eq, derive
//- /main.rs crate:foo
//! $0[PartialEq#derivable]
fn main() {}
"#,
expect!["[PartialEq#derivable](https://doc.rust-lang.org/stable/core/cmp/trait.PartialEq.html#derivable)"],
);
}