Skip to content

Commit 9b50332

Browse files
authored
Rollup merge of #107934 - notriddle:notriddle/intra-doc-link-meta-description, r=camelid,GuillaumeGomez
rustdoc: account for intra-doc links in `<meta name="description">` Similar to #86451, but for the SEO descriptions instead of the search descriptions.
2 parents 76c47fb + 72b3f46 commit 9b50332

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/librustdoc/html/markdown.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1176,14 +1176,23 @@ pub(crate) fn short_markdown_summary(markdown: &str, link_names: &[RenderedLink]
11761176
/// - Headings, links, and formatting are stripped.
11771177
/// - Inline code is rendered as-is, surrounded by backticks.
11781178
/// - HTML and code blocks are ignored.
1179-
pub(crate) fn plain_text_summary(md: &str) -> String {
1179+
pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> String {
11801180
if md.is_empty() {
11811181
return String::new();
11821182
}
11831183

11841184
let mut s = String::with_capacity(md.len() * 3 / 2);
11851185

1186-
for event in Parser::new_ext(md, summary_opts()) {
1186+
let mut replacer = |broken_link: BrokenLink<'_>| {
1187+
link_names
1188+
.iter()
1189+
.find(|link| link.original_text.as_str() == &*broken_link.reference)
1190+
.map(|link| (link.href.as_str().into(), link.new_text.as_str().into()))
1191+
};
1192+
1193+
let p = Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut replacer));
1194+
1195+
for event in p {
11871196
match &event {
11881197
Event::Text(text) => s.push_str(text),
11891198
Event::Code(code) => {

src/librustdoc/html/markdown/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ fn test_short_markdown_summary() {
249249
#[test]
250250
fn test_plain_text_summary() {
251251
fn t(input: &str, expect: &str) {
252-
let output = plain_text_summary(input);
252+
let output = plain_text_summary(input, &[]);
253253
assert_eq!(output, expect, "original: {}", input);
254254
}
255255

src/librustdoc/html/render/context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ impl<'tcx> Context<'tcx> {
182182
};
183183
title.push_str(" - Rust");
184184
let tyname = it.type_();
185-
let desc = it.doc_value().as_ref().map(|doc| plain_text_summary(doc));
185+
let desc = it
186+
.doc_value()
187+
.as_ref()
188+
.map(|doc| plain_text_summary(doc, &it.link_names(&self.cache())));
186189
let desc = if let Some(desc) = desc {
187190
desc
188191
} else if it.is_crate() {

tests/rustdoc/description.rs

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ pub mod foo_mod {
2222
// 'Only paragraph.'
2323
/// Only paragraph.
2424
pub fn foo_fn() {}
25+
26+
// @has 'foo/fn.bar_fn.html' '//meta[@name="description"]/@content' \
27+
// 'Description with intra-doc link to foo_fn and [nonexistent_item] and foo_fn.'
28+
#[allow(rustdoc::broken_intra_doc_links)]
29+
/// Description with intra-doc link to [foo_fn] and [nonexistent_item] and [foo_fn](self::foo_fn).
30+
pub fn bar_fn() {}

0 commit comments

Comments
 (0)