-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Render Markdown in search results #77686
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
Changes from all commits
5d4a712
e178030
07e9426
b903519
f0cf5a9
376507f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,6 @@ | |
//! // ... something using html | ||
//! ``` | ||
|
||
#![allow(non_camel_case_types)] | ||
|
||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_hir::HirId; | ||
|
@@ -1037,7 +1035,95 @@ impl MarkdownSummaryLine<'_> { | |
} | ||
} | ||
|
||
/// Renders a subset of Markdown in the first paragraph of the provided Markdown. | ||
/// | ||
/// - *Italics*, **bold**, and `inline code` styles **are** rendered. | ||
/// - Headings and links are stripped (though the text *is* rendered). | ||
/// - HTML, code blocks, and everything else are ignored. | ||
/// | ||
/// Returns a tuple of the rendered HTML string and whether the output was shortened | ||
/// due to the provided `length_limit`. | ||
fn markdown_summary_with_limit(md: &str, length_limit: usize) -> (String, bool) { | ||
if md.is_empty() { | ||
return (String::new(), false); | ||
} | ||
|
||
let mut s = String::with_capacity(md.len() * 3 / 2); | ||
let mut text_length = 0; | ||
let mut stopped_early = false; | ||
|
||
fn push(s: &mut String, text_length: &mut usize, text: &str) { | ||
s.push_str(text); | ||
*text_length += text.len(); | ||
}; | ||
|
||
'outer: for event in Parser::new_ext(md, Options::ENABLE_STRIKETHROUGH) { | ||
match &event { | ||
Event::Text(text) => { | ||
for word in text.split_inclusive(char::is_whitespace) { | ||
if text_length + word.len() >= length_limit { | ||
stopped_early = true; | ||
break 'outer; | ||
} | ||
|
||
push(&mut s, &mut text_length, word); | ||
} | ||
} | ||
Event::Code(code) => { | ||
if text_length + code.len() >= length_limit { | ||
stopped_early = true; | ||
break; | ||
} | ||
|
||
s.push_str("<code>"); | ||
push(&mut s, &mut text_length, code); | ||
s.push_str("</code>"); | ||
} | ||
Event::Start(tag) => match tag { | ||
Tag::Emphasis => s.push_str("<em>"), | ||
Tag::Strong => s.push_str("<strong>"), | ||
Tag::CodeBlock(..) => break, | ||
_ => {} | ||
}, | ||
Event::End(tag) => match tag { | ||
Tag::Emphasis => s.push_str("</em>"), | ||
Tag::Strong => s.push_str("</strong>"), | ||
Tag::Paragraph => break, | ||
_ => {} | ||
}, | ||
Event::HardBreak | Event::SoftBreak => { | ||
if text_length + 1 >= length_limit { | ||
stopped_early = true; | ||
break; | ||
} | ||
|
||
push(&mut s, &mut text_length, " "); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
(s, stopped_early) | ||
} | ||
|
||
/// Renders a shortened first paragraph of the given Markdown as a subset of Markdown, | ||
/// making it suitable for contexts like the search index. | ||
/// | ||
/// Will shorten to 59 or 60 characters, including an ellipsis (…) if it was shortened. | ||
/// | ||
/// See [`markdown_summary_with_limit`] for details about what is rendered and what is not. | ||
crate fn short_markdown_summary(markdown: &str) -> String { | ||
let (mut s, was_shortened) = markdown_summary_with_limit(markdown, 59); | ||
|
||
if was_shortened { | ||
s.push('…'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just thought about this but since Maybe you have an opinion too @jyn514 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think it makes sense to inline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think @camelid ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well I think it's better to keep them separate, for two reasons:
But I'm also really eager to get this done, so I'm willing to inline it if you disagree with my reasoning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a blocking point for me, more like an unnecessary complexity. So unless @jyn514 wants to wait for it to be inlined, we can always come back to it later on. |
||
} | ||
|
||
s | ||
} | ||
|
||
/// Renders the first paragraph of the provided markdown as plain text. | ||
/// Useful for alt-text. | ||
/// | ||
/// - Headings, links, and formatting are stripped. | ||
/// - Inline code is rendered as-is, surrounded by backticks. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
/// Foo | ||
/// Docs for Foo | ||
pub struct Foo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// ignore-tidy-linelength | ||
|
||
const QUERY = ['summaries', 'summaries::Sidebar', 'summaries::Sidebar2']; | ||
|
||
const EXPECTED = [ | ||
{ | ||
'others': [ | ||
{ 'path': '', 'name': 'summaries', 'desc': 'This <em>summary</em> has a link and <code>code</code>.' }, | ||
], | ||
}, | ||
{ | ||
'others': [ | ||
{ 'path': 'summaries', 'name': 'Sidebar', 'desc': 'This <code>code</code> will be rendered in a code tag.' }, | ||
], | ||
}, | ||
{ | ||
'others': [ | ||
{ 'path': 'summaries', 'name': 'Sidebar2', 'desc': '' }, | ||
], | ||
}, | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#![crate_type = "lib"] | ||
#![crate_name = "summaries"] | ||
|
||
//! This *summary* has a [link] and `code`. | ||
//! | ||
//! This is the second paragraph. | ||
//! | ||
//! [link]: https://example.com | ||
/// This `code` will be rendered in a code tag. | ||
/// | ||
/// This text should not be rendered. | ||
camelid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub struct Sidebar; | ||
|
||
/// ```text | ||
/// this block should not be rendered | ||
/// ``` | ||
pub struct Sidebar2; |
Uh oh!
There was an error while loading. Please reload this page.