Skip to content

Commit

Permalink
librustdoc: lazily format "read more" link in document_short
Browse files Browse the repository at this point in the history
  • Loading branch information
yotamofek committed Feb 14, 2025
1 parent ea15f6d commit 5cc64e8
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,18 +569,27 @@ fn document_short<'a, 'cx: 'a>(
let (mut summary_html, has_more_content) =
MarkdownSummaryLine(&s, &item.links(cx)).into_string_with_has_more_content();

if has_more_content {
let link =
format!(" <a{}>Read more</a>", assoc_href_attr(item, link, cx).maybe_display());
let link = if has_more_content {
let link = fmt::from_fn(|f| {
write!(
f,
" <a{}>Read more</a>",
assoc_href_attr(item, link, cx).maybe_display()
)
});

if let Some(idx) = summary_html.rfind("</p>") {
summary_html.insert_str(idx, &link);
summary_html.insert_str(idx, &link.to_string());
None
} else {
summary_html.push_str(&link);
Some(link)
}
} else {
None
}
.maybe_display();

write!(f, "<div class='docblock'>{summary_html}</div>")?;
write!(f, "<div class='docblock'>{summary_html}{link}</div>")?;
}
Ok(())
})
Expand Down

0 comments on commit 5cc64e8

Please sign in to comment.