Rollup merge of #107934 - notriddle:notriddle/intra-doc-link-meta-description, r=camelid,GuillaumeGomez
rustdoc: account for intra-doc links in `<meta name="description">` Similar to #86451, but for the SEO descriptions instead of the search descriptions.
This commit is contained in:
commit
9b503325b4
@ -1176,14 +1176,23 @@ pub(crate) fn short_markdown_summary(markdown: &str, link_names: &[RenderedLink]
|
|||||||
/// - Headings, links, and formatting are stripped.
|
/// - Headings, links, and formatting are stripped.
|
||||||
/// - Inline code is rendered as-is, surrounded by backticks.
|
/// - Inline code is rendered as-is, surrounded by backticks.
|
||||||
/// - HTML and code blocks are ignored.
|
/// - 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() {
|
if md.is_empty() {
|
||||||
return String::new();
|
return String::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut s = String::with_capacity(md.len() * 3 / 2);
|
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 {
|
match &event {
|
||||||
Event::Text(text) => s.push_str(text),
|
Event::Text(text) => s.push_str(text),
|
||||||
Event::Code(code) => {
|
Event::Code(code) => {
|
||||||
|
@ -249,7 +249,7 @@ fn test_short_markdown_summary() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_plain_text_summary() {
|
fn test_plain_text_summary() {
|
||||||
fn t(input: &str, expect: &str) {
|
fn t(input: &str, expect: &str) {
|
||||||
let output = plain_text_summary(input);
|
let output = plain_text_summary(input, &[]);
|
||||||
assert_eq!(output, expect, "original: {}", input);
|
assert_eq!(output, expect, "original: {}", input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,7 +182,10 @@ impl<'tcx> Context<'tcx> {
|
|||||||
};
|
};
|
||||||
title.push_str(" - Rust");
|
title.push_str(" - Rust");
|
||||||
let tyname = it.type_();
|
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 {
|
let desc = if let Some(desc) = desc {
|
||||||
desc
|
desc
|
||||||
} else if it.is_crate() {
|
} else if it.is_crate() {
|
||||||
|
@ -22,3 +22,9 @@ pub mod foo_mod {
|
|||||||
// 'Only paragraph.'
|
// 'Only paragraph.'
|
||||||
/// Only paragraph.
|
/// Only paragraph.
|
||||||
pub fn foo_fn() {}
|
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() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user