diff options
| author | Yuki Okushi <huyuumi.dev@gmail.com> | 2021-02-22 18:26:08 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-22 18:26:08 +0900 |
| commit | 1dba8ce8a54cc99ebde1afd0babf38f0dc99469c (patch) | |
| tree | 987887e714a9f5a990c86c948ff0bf30821c0e73 | |
| parent | 20c1fa17707e6cdd202a9a720f7dfe0000ecb74e (diff) | |
| parent | 575c75b324ee7557b776f6dce6f4f77f5f7887a1 (diff) | |
| download | rust-1dba8ce8a54cc99ebde1afd0babf38f0dc99469c.tar.gz rust-1dba8ce8a54cc99ebde1afd0babf38f0dc99469c.zip | |
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.
| -rw-r--r-- | src/librustdoc/html/markdown.rs | 1 | ||||
| -rw-r--r-- | src/librustdoc/html/markdown/tests.rs | 1 | ||||
| -rw-r--r-- | src/librustdoc/html/render/mod.rs | 5 | ||||
| -rw-r--r-- | src/test/rustdoc/description.rs | 24 | ||||
| -rw-r--r-- | src/test/rustdoc/description_default.rs | 14 |
5 files changed, 44 insertions, 1 deletions
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index a81fd55f6f1..b7854bbf82b 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -1124,6 +1124,7 @@ crate fn plain_text_summary(md: &str) -> String { Event::HardBreak | Event::SoftBreak => s.push(' '), Event::Start(Tag::CodeBlock(..)) => break, Event::End(Tag::Paragraph) => break, + Event::End(Tag::Heading(..)) => break, _ => (), } } diff --git a/src/librustdoc/html/markdown/tests.rs b/src/librustdoc/html/markdown/tests.rs index 9da3072ec28..994fe8206e8 100644 --- a/src/librustdoc/html/markdown/tests.rs +++ b/src/librustdoc/html/markdown/tests.rs @@ -230,6 +230,7 @@ fn test_plain_text_summary() { t("code `let x = i32;` ...", "code `let x = i32;` ..."); t("type `Type<'static>` ...", "type `Type<'static>` ..."); t("# top header", "top header"); + t("# top header\n\nfollowed by some text", "top header"); t("## header", "header"); t("first paragraph\n\nsecond paragraph", "first paragraph"); t("```\nfn main() {}\n```", ""); diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 7ca355ed11c..7f122bb8cb5 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1548,7 +1548,10 @@ impl Context<'_> { } title.push_str(" - Rust"); let tyname = it.type_(); - let desc = if it.is_crate() { + let desc = it.doc_value().as_ref().map(|doc| plain_text_summary(&doc)); + let desc = if let Some(desc) = desc { + desc + } else if it.is_crate() { format!("API documentation for the Rust `{}` crate.", self.shared.layout.krate) } else { format!( diff --git a/src/test/rustdoc/description.rs b/src/test/rustdoc/description.rs new file mode 100644 index 00000000000..05ec4282208 --- /dev/null +++ b/src/test/rustdoc/description.rs @@ -0,0 +1,24 @@ +#![crate_name = "foo"] +//! # Description test crate +//! +//! This is the contents of the test crate docstring. +//! It should not show up in the description. + +// @has 'foo/index.html' '//meta[@name="description"]/@content' \ +// 'Description test crate' +// @!has - '//meta[@name="description"]/@content' 'should not show up' + +// @has 'foo/foo_mod/index.html' '//meta[@name="description"]/@content' \ +// 'First paragraph description.' +// @!has - '//meta[@name="description"]/@content' 'Second paragraph' +/// First paragraph description. +/// +/// Second paragraph should not show up. +pub mod foo_mod { + pub struct __Thing {} +} + +// @has 'foo/fn.foo_fn.html' '//meta[@name="description"]/@content' \ +// 'Only paragraph.' +/// Only paragraph. +pub fn foo_fn() {} diff --git a/src/test/rustdoc/description_default.rs b/src/test/rustdoc/description_default.rs new file mode 100644 index 00000000000..21d8e04d3f9 --- /dev/null +++ b/src/test/rustdoc/description_default.rs @@ -0,0 +1,14 @@ +#![crate_name = "foo"] + +// @has 'foo/index.html' '//meta[@name="description"]/@content' \ +// 'API documentation for the Rust `foo` crate.' + +// @has 'foo/foo_mod/index.html' '//meta[@name="description"]/@content' \ +// 'API documentation for the Rust `foo_mod` mod in crate `foo`.' +pub mod foo_mod { + pub struct __Thing {} +} + +// @has 'foo/fn.foo_fn.html' '//meta[@name="description"]/@content' \ +// 'API documentation for the Rust `foo_fn` fn in crate `foo`.' +pub fn foo_fn() {} |
