Skip to content

Commit

Permalink
Rollup merge of rust-lang#107934 - notriddle:notriddle/intra-doc-link…
Browse files Browse the repository at this point in the history
…-meta-description, r=camelid,GuillaumeGomez

rustdoc: account for intra-doc links in `<meta name="description">`

Similar to rust-lang#86451, but for the SEO descriptions instead of the search descriptions.
  • Loading branch information
matthiaskrgr committed Feb 12, 2023
2 parents 76c47fb + 72b3f46 commit 9b50332
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,14 +1176,23 @@ pub(crate) fn short_markdown_summary(markdown: &str, link_names: &[RenderedLink]
/// - Headings, links, and formatting are stripped.
/// - Inline code is rendered as-is, surrounded by backticks.
/// - HTML and code blocks are ignored.
pub(crate) fn plain_text_summary(md: &str) -> String {
pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> String {
if md.is_empty() {
return String::new();
}

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

for event in Parser::new_ext(md, summary_opts()) {
let mut replacer = |broken_link: BrokenLink<'_>| {
link_names
.iter()
.find(|link| link.original_text.as_str() == &*broken_link.reference)
.map(|link| (link.href.as_str().into(), link.new_text.as_str().into()))
};

let p = Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut replacer));

for event in p {
match &event {
Event::Text(text) => s.push_str(text),
Event::Code(code) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/markdown/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn test_short_markdown_summary() {
#[test]
fn test_plain_text_summary() {
fn t(input: &str, expect: &str) {
let output = plain_text_summary(input);
let output = plain_text_summary(input, &[]);
assert_eq!(output, expect, "original: {}", input);
}

Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ impl<'tcx> Context<'tcx> {
};
title.push_str(" - Rust");
let tyname = it.type_();
let desc = it.doc_value().as_ref().map(|doc| plain_text_summary(doc));
let desc = it
.doc_value()
.as_ref()
.map(|doc| plain_text_summary(doc, &it.link_names(&self.cache())));
let desc = if let Some(desc) = desc {
desc
} else if it.is_crate() {
Expand Down
6 changes: 6 additions & 0 deletions tests/rustdoc/description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ pub mod foo_mod {
// 'Only paragraph.'
/// Only paragraph.
pub fn foo_fn() {}

// @has 'foo/fn.bar_fn.html' '//meta[@name="description"]/@content' \
// 'Description with intra-doc link to foo_fn and [nonexistent_item] and foo_fn.'
#[allow(rustdoc::broken_intra_doc_links)]
/// Description with intra-doc link to [foo_fn] and [nonexistent_item] and [foo_fn](self::foo_fn).
pub fn bar_fn() {}

0 comments on commit 9b50332

Please sign in to comment.