Skip to content

Commit 72b3f46

Browse files
committed
rustdoc: account for intra-doc links in <meta name="description">
1 parent 8dabf5d commit 72b3f46

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
@@ -1180,14 +1180,23 @@ pub(crate) fn short_markdown_summary(markdown: &str, link_names: &[RenderedLink]
11801180
/// - Headings, links, and formatting are stripped.
11811181
/// - Inline code is rendered as-is, surrounded by backticks.
11821182
/// - HTML and code blocks are ignored.
1183-
pub(crate) fn plain_text_summary(md: &str) -> String {
1183+
pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> String {
11841184
if md.is_empty() {
11851185
return String::new();
11861186
}
11871187

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

1190-
for event in Parser::new_ext(md, summary_opts()) {
1190+
let mut replacer = |broken_link: BrokenLink<'_>| {
1191+
link_names
1192+
.iter()
1193+
.find(|link| link.original_text.as_str() == &*broken_link.reference)
1194+
.map(|link| (link.href.as_str().into(), link.new_text.as_str().into()))
1195+
};
1196+
1197+
let p = Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut replacer));
1198+
1199+
for event in p {
11911200
match &event {
11921201
Event::Text(text) => s.push_str(text),
11931202
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)