Skip to content

Commit 0ae5ef9

Browse files
authored
Rollup merge of rust-lang#86451 - notriddle:notriddle/rustdoc-intra-doc-link-summary, r=CraftSpider
Resolve intra-doc links in summary desc Before: ![rustdoc-intra-doc-link-summary-before](https://user-images.githubusercontent.com/1593513/122623069-9d995e80-d04f-11eb-8d46-ec2ec126bb5e.png) After: ![rustdoc-intra-doc-link-summary](https://user-images.githubusercontent.com/1593513/122623076-a4c06c80-d04f-11eb-967a-f5916871c34b.png)
2 parents 16a12c2 + f67585d commit 0ae5ef9

File tree

7 files changed

+69
-15
lines changed

7 files changed

+69
-15
lines changed

src/librustdoc/clean/types.rs

+27
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,33 @@ impl Item {
523523
.collect()
524524
}
525525

526+
/// Find a list of all link names, without finding their href.
527+
///
528+
/// This is used for generating summary text, which does not include
529+
/// the link text, but does need to know which `[]`-bracketed names
530+
/// are actually links.
531+
crate fn link_names(&self, cache: &Cache) -> Vec<RenderedLink> {
532+
cache
533+
.intra_doc_links
534+
.get(&self.def_id)
535+
.map_or(&[][..], |v| v.as_slice())
536+
.iter()
537+
.filter_map(|ItemLink { link: s, link_text, did, fragment }| {
538+
// FIXME(83083): using fragments as a side-channel for
539+
// primitive names is very unfortunate
540+
if did.is_some() || fragment.is_some() {
541+
Some(RenderedLink {
542+
original_text: s.clone(),
543+
new_text: link_text.clone(),
544+
href: String::new(),
545+
})
546+
} else {
547+
None
548+
}
549+
})
550+
.collect()
551+
}
552+
526553
crate fn is_crate(&self) -> bool {
527554
self.is_mod() && self.def_id.as_real().map_or(false, |did| did.index == CRATE_DEF_INDEX)
528555
}

src/librustdoc/formats/cache.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,14 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
292292
// which should not be indexed. The crate-item itself is
293293
// inserted later on when serializing the search-index.
294294
if item.def_id.index().map_or(false, |idx| idx != CRATE_DEF_INDEX) {
295+
let desc = item.doc_value().map_or_else(String::new, |x| {
296+
short_markdown_summary(&x.as_str(), &item.link_names(&self.cache))
297+
});
295298
self.cache.search_index.push(IndexItem {
296299
ty: item.type_(),
297300
name: s.to_string(),
298301
path: path.join("::"),
299-
desc: item
300-
.doc_value()
301-
.map_or_else(String::new, |x| short_markdown_summary(&x.as_str())),
302+
desc,
302303
parent,
303304
parent_idx: None,
304305
search_type: get_index_search_type(&item, &self.empty_cache, self.tcx),

src/librustdoc/html/markdown.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,11 @@ impl MarkdownSummaryLine<'_> {
10511051
///
10521052
/// Returns a tuple of the rendered HTML string and whether the output was shortened
10531053
/// due to the provided `length_limit`.
1054-
fn markdown_summary_with_limit(md: &str, length_limit: usize) -> (String, bool) {
1054+
fn markdown_summary_with_limit(
1055+
md: &str,
1056+
link_names: &[RenderedLink],
1057+
length_limit: usize,
1058+
) -> (String, bool) {
10551059
if md.is_empty() {
10561060
return (String::new(), false);
10571061
}
@@ -1065,7 +1069,20 @@ fn markdown_summary_with_limit(md: &str, length_limit: usize) -> (String, bool)
10651069
*text_length += text.len();
10661070
}
10671071

1068-
'outer: for event in Parser::new_ext(md, summary_opts()) {
1072+
let mut replacer = |broken_link: BrokenLink<'_>| {
1073+
if let Some(link) =
1074+
link_names.iter().find(|link| &*link.original_text == broken_link.reference)
1075+
{
1076+
Some((link.href.as_str().into(), link.new_text.as_str().into()))
1077+
} else {
1078+
None
1079+
}
1080+
};
1081+
1082+
let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut replacer));
1083+
let p = LinkReplacer::new(p, link_names);
1084+
1085+
'outer: for event in p {
10691086
match &event {
10701087
Event::Text(text) => {
10711088
for word in text.split_inclusive(char::is_whitespace) {
@@ -1121,8 +1138,8 @@ fn markdown_summary_with_limit(md: &str, length_limit: usize) -> (String, bool)
11211138
/// Will shorten to 59 or 60 characters, including an ellipsis (…) if it was shortened.
11221139
///
11231140
/// See [`markdown_summary_with_limit`] for details about what is rendered and what is not.
1124-
crate fn short_markdown_summary(markdown: &str) -> String {
1125-
let (mut s, was_shortened) = markdown_summary_with_limit(markdown, 59);
1141+
crate fn short_markdown_summary(markdown: &str, link_names: &[RenderedLink]) -> String {
1142+
let (mut s, was_shortened) = markdown_summary_with_limit(markdown, link_names, 59);
11261143

11271144
if was_shortened {
11281145
s.push('…');

src/librustdoc/html/markdown/tests.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn test_header_ids_multiple_blocks() {
221221
#[test]
222222
fn test_short_markdown_summary() {
223223
fn t(input: &str, expect: &str) {
224-
let output = short_markdown_summary(input);
224+
let output = short_markdown_summary(input, &[][..]);
225225
assert_eq!(output, expect, "original: {}", input);
226226
}
227227

@@ -232,6 +232,7 @@ fn test_short_markdown_summary() {
232232
t("Hard-break \nsummary", "Hard-break summary");
233233
t("hello [Rust] :)\n\n[Rust]: https://www.rust-lang.org", "hello Rust :)");
234234
t("hello [Rust](https://www.rust-lang.org \"Rust\") :)", "hello Rust :)");
235+
t("dud [link]", "dud [link]");
235236
t("code `let x = i32;` ...", "code <code>let x = i32;</code> …");
236237
t("type `Type<'static>` ...", "type <code>Type<'static></code> …");
237238
t("# top header", "top header");
@@ -259,6 +260,7 @@ fn test_plain_text_summary() {
259260
t("Hard-break \nsummary", "Hard-break summary");
260261
t("hello [Rust] :)\n\n[Rust]: https://www.rust-lang.org", "hello Rust :)");
261262
t("hello [Rust](https://www.rust-lang.org \"Rust\") :)", "hello Rust :)");
263+
t("dud [link]", "dud [link]");
262264
t("code `let x = i32;` ...", "code `let x = i32;` …");
263265
t("type `Type<'static>` ...", "type `Type<'static>` …");
264266
t("# top header", "top header");

src/librustdoc/html/render/cache.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
3434
// has since been learned.
3535
for &(did, ref item) in &cache.orphan_impl_items {
3636
if let Some(&(ref fqp, _)) = cache.paths.get(&did) {
37+
let desc = item
38+
.doc_value()
39+
.map_or_else(String::new, |s| short_markdown_summary(&s, &item.link_names(&cache)));
3740
cache.search_index.push(IndexItem {
3841
ty: item.type_(),
3942
name: item.name.unwrap().to_string(),
4043
path: fqp[..fqp.len() - 1].join("::"),
41-
desc: item.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s)),
44+
desc,
4245
parent: Some(did.into()),
4346
parent_idx: None,
4447
search_type: get_index_search_type(&item, cache, tcx),
@@ -47,6 +50,11 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
4750
}
4851
}
4952

53+
let crate_doc = krate
54+
.module
55+
.doc_value()
56+
.map_or_else(String::new, |s| short_markdown_summary(&s, &krate.module.link_names(&cache)));
57+
5058
let Cache { ref mut search_index, ref paths, .. } = *cache;
5159

5260
// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
@@ -100,9 +108,6 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
100108
crate_items.push(&*item);
101109
}
102110

103-
let crate_doc =
104-
krate.module.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s));
105-
106111
struct CrateData<'a> {
107112
doc: String,
108113
items: Vec<&'a IndexItem>,

src/test/rustdoc-js/summaries.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const QUERY = ['summaries', 'summaries::Sidebar', 'summaries::Sidebar2'];
55
const EXPECTED = [
66
{
77
'others': [
8-
{ 'path': '', 'name': 'summaries', 'desc': 'This <em>summary</em> has a link and <code>code</code>.' },
8+
{ 'path': '', 'name': 'summaries', 'desc': 'This <em>summary</em> has a link, [<code>code</code>], and <code>Sidebar2</code> intra-doc.' },
99
],
1010
},
1111
{

src/test/rustdoc-js/summaries.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#![crate_type = "lib"]
22
#![crate_name = "summaries"]
33

4-
//! This *summary* has a [link] and `code`.
4+
//! This *summary* has a [link], [`code`], and [`Sidebar2`] intra-doc.
55
//!
6-
//! This is the second paragraph.
6+
//! This is the second paragraph. It should not be rendered.
7+
//! To test that intra-doc links are resolved properly, [`code`] should render
8+
//! the square brackets, and [`Sidebar2`] should not.
79
//!
810
//! [link]: https://example.com
911

0 commit comments

Comments
 (0)