Skip to content

Commit 6c9a580

Browse files
committed
rustdoc: Strip broken links in summaries
The primary reason to do this is because intra-doc links are treated as "broken" in the summary since the resolution context is not available. Previously, search results with intra-doc links would look like: > This method returns an \[`Ordering`\] but now they look like: > This method returns an `Ordering` as search results with regular links do. The one drawback I can see is if people are using `[` and `]` literally, but I think that case is much rarer than using intra-doc links, and they can and should escape the brackets with a backslash or use inline code. Plus, this is just for summaries.
1 parent d1206f9 commit 6c9a580

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/librustdoc/html/markdown.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,14 @@ fn markdown_summary_with_limit(md: &str, length_limit: usize) -> (String, bool)
10351035
*text_length += text.len();
10361036
}
10371037

1038-
'outer: for event in Parser::new_ext(md, summary_opts()) {
1038+
// NOTE: Make sure to update the same variable in `plain_text_summary`
1039+
// if/when you update this one. They have to be duplicated because of a typesystem thing.
1040+
let mut broken_link_callback =
1041+
|broken_link: BrokenLink<'_>| Some(("#".into(), broken_link.reference.to_owned().into()));
1042+
1043+
'outer: for event in
1044+
Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut broken_link_callback))
1045+
{
10391046
match &event {
10401047
Event::Text(text) => {
10411048
for word in text.split_inclusive(char::is_whitespace) {
@@ -1113,7 +1120,14 @@ crate fn plain_text_summary(md: &str) -> String {
11131120

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

1116-
for event in Parser::new_ext(md, summary_opts()) {
1123+
// NOTE: Make sure to update the same variable in `markdown_summary_with_limit`
1124+
// if/when you update this one. They have to be duplicated because of a typesystem thing.
1125+
let mut broken_link_callback =
1126+
|broken_link: BrokenLink<'_>| Some(("#".into(), broken_link.reference.to_owned().into()));
1127+
1128+
for event in
1129+
Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut broken_link_callback))
1130+
{
11171131
match &event {
11181132
Event::Text(text) => s.push_str(text),
11191133
Event::Code(code) => {

0 commit comments

Comments
 (0)