Skip to content

Commit 20915d4

Browse files
committed
1 parent 312b894 commit 20915d4

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
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/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>,

0 commit comments

Comments
 (0)