Skip to content

Commit 1dba8ce

Browse files
authored
Rollup merge of #82351 - notriddle:docs-meta-description, r=jyn514
Use the first paragraph, instead of cookie-cutter text, for rustdoc descriptions Partially addresses #82283.
2 parents 20c1fa1 + 575c75b commit 1dba8ce

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

src/librustdoc/html/markdown.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,7 @@ crate fn plain_text_summary(md: &str) -> String {
11241124
Event::HardBreak | Event::SoftBreak => s.push(' '),
11251125
Event::Start(Tag::CodeBlock(..)) => break,
11261126
Event::End(Tag::Paragraph) => break,
1127+
Event::End(Tag::Heading(..)) => break,
11271128
_ => (),
11281129
}
11291130
}

src/librustdoc/html/markdown/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ fn test_plain_text_summary() {
230230
t("code `let x = i32;` ...", "code `let x = i32;` ...");
231231
t("type `Type<'static>` ...", "type `Type<'static>` ...");
232232
t("# top header", "top header");
233+
t("# top header\n\nfollowed by some text", "top header");
233234
t("## header", "header");
234235
t("first paragraph\n\nsecond paragraph", "first paragraph");
235236
t("```\nfn main() {}\n```", "");

src/librustdoc/html/render/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,10 @@ impl Context<'_> {
15481548
}
15491549
title.push_str(" - Rust");
15501550
let tyname = it.type_();
1551-
let desc = if it.is_crate() {
1551+
let desc = it.doc_value().as_ref().map(|doc| plain_text_summary(&doc));
1552+
let desc = if let Some(desc) = desc {
1553+
desc
1554+
} else if it.is_crate() {
15521555
format!("API documentation for the Rust `{}` crate.", self.shared.layout.krate)
15531556
} else {
15541557
format!(

src/test/rustdoc/description.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![crate_name = "foo"]
2+
//! # Description test crate
3+
//!
4+
//! This is the contents of the test crate docstring.
5+
//! It should not show up in the description.
6+
7+
// @has 'foo/index.html' '//meta[@name="description"]/@content' \
8+
// 'Description test crate'
9+
// @!has - '//meta[@name="description"]/@content' 'should not show up'
10+
11+
// @has 'foo/foo_mod/index.html' '//meta[@name="description"]/@content' \
12+
// 'First paragraph description.'
13+
// @!has - '//meta[@name="description"]/@content' 'Second paragraph'
14+
/// First paragraph description.
15+
///
16+
/// Second paragraph should not show up.
17+
pub mod foo_mod {
18+
pub struct __Thing {}
19+
}
20+
21+
// @has 'foo/fn.foo_fn.html' '//meta[@name="description"]/@content' \
22+
// 'Only paragraph.'
23+
/// Only paragraph.
24+
pub fn foo_fn() {}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![crate_name = "foo"]
2+
3+
// @has 'foo/index.html' '//meta[@name="description"]/@content' \
4+
// 'API documentation for the Rust `foo` crate.'
5+
6+
// @has 'foo/foo_mod/index.html' '//meta[@name="description"]/@content' \
7+
// 'API documentation for the Rust `foo_mod` mod in crate `foo`.'
8+
pub mod foo_mod {
9+
pub struct __Thing {}
10+
}
11+
12+
// @has 'foo/fn.foo_fn.html' '//meta[@name="description"]/@content' \
13+
// 'API documentation for the Rust `foo_fn` fn in crate `foo`.'
14+
pub fn foo_fn() {}

0 commit comments

Comments
 (0)