Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the first paragraph, instead of cookie-cutter text, for rustdoc descriptions #82351

Merged
merged 8 commits into from
Feb 22, 2021
1 change: 1 addition & 0 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
@@ -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,
_ => (),
}
}
1 change: 1 addition & 0 deletions src/librustdoc/html/markdown/tests.rs
Original file line number Diff line number Diff line change
@@ -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```", "");
5 changes: 4 additions & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
@@ -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!(
19 changes: 19 additions & 0 deletions src/test/rustdoc/description.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![crate_name = "foo"]
//! # Description test crate
//!
//! This is the contents of the test crate docstring. It should not show up in the description.
// @matches 'foo/index.html' '//meta[@name="description"]/@content' 'Description test crate'

// @matches 'foo/foo_mod/index.html' '//meta[@name="description"]/@content' 'First paragraph description.'
/// First paragraph description.
///
/// Second paragraph should not show up.
pub mod foo_mod {
pub struct __Thing {}
}

// @matches 'foo/fn.foo_fn.html' '//meta[@name="description"]/@content' 'Only paragraph.'
/// Only paragraph.
pub fn foo_fn() {}

12 changes: 12 additions & 0 deletions src/test/rustdoc/description_default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![crate_name = "foo"]

// @matches 'foo/index.html' '//meta[@name="description"]/@content' 'API documentation for the Rust `foo` crate.'

// @matches '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 {}
}

// @matches 'foo/fn.foo_fn.html' '//meta[@name="description"]/@content' 'API documentation for the Rust `foo_fn` fn in crate `foo`.'
pub fn foo_fn() {}