@@ -1506,6 +1506,7 @@ impl Context {
15061506 }
15071507 }
15081508
1509+ /// Construct a map of items shown in the sidebar to a plain-text summary of their docs.
15091510 fn build_sidebar_items ( & self , m : & clean:: Module ) -> BTreeMap < String , Vec < NameDoc > > {
15101511 // BTreeMap instead of HashMap to get a sorted output
15111512 let mut map: BTreeMap < _ , Vec < _ > > = BTreeMap :: new ( ) ;
@@ -1522,7 +1523,7 @@ impl Context {
15221523 let short = short. to_string ( ) ;
15231524 map. entry ( short)
15241525 . or_default ( )
1525- . push ( ( myname, Some ( plain_summary_line ( item. doc_value ( ) ) ) ) ) ;
1526+ . push ( ( myname, Some ( plain_text_summary ( item. doc_value ( ) ) ) ) ) ;
15261527 }
15271528
15281529 if self . shared . sort_modules_alphabetically {
@@ -1728,22 +1729,15 @@ fn full_path(cx: &Context, item: &clean::Item) -> String {
17281729 s
17291730}
17301731
1732+ /// Renders the first paragraph of the given markdown as plain text, making it suitable for
1733+ /// contexts like alt-text or the search index.
1734+ ///
1735+ /// If no markdown is supplied, the empty string is returned.
1736+ ///
1737+ /// See [`markdown::plain_text_summary`] for further details.
17311738#[ inline]
1732- crate fn plain_summary_line ( s : Option < & str > ) -> String {
1733- let s = s. unwrap_or ( "" ) ;
1734- // This essentially gets the first paragraph of text in one line.
1735- let mut line = s
1736- . lines ( )
1737- . skip_while ( |line| line. chars ( ) . all ( |c| c. is_whitespace ( ) ) )
1738- . take_while ( |line| line. chars ( ) . any ( |c| !c. is_whitespace ( ) ) )
1739- . fold ( String :: new ( ) , |mut acc, line| {
1740- acc. push_str ( line) ;
1741- acc. push ( ' ' ) ;
1742- acc
1743- } ) ;
1744- // remove final whitespace
1745- line. pop ( ) ;
1746- markdown:: plain_summary_line ( & line[ ..] )
1739+ crate fn plain_text_summary ( s : Option < & str > ) -> String {
1740+ s. map ( markdown:: plain_text_summary) . unwrap_or_default ( )
17471741}
17481742
17491743crate fn shorten ( s : String ) -> String {
@@ -1800,25 +1794,35 @@ fn render_markdown(
18001794 )
18011795}
18021796
1797+ /// Writes a documentation block containing only the first paragraph of the documentation. If the
1798+ /// docs are longer, a "Read more" link is appended to the end.
18031799fn document_short (
18041800 w : & mut Buffer ,
1805- cx : & Context ,
18061801 item : & clean:: Item ,
18071802 link : AssocItemLink < ' _ > ,
18081803 prefix : & str ,
18091804 is_hidden : bool ,
18101805) {
18111806 if let Some ( s) = item. doc_value ( ) {
1812- let markdown = if s. contains ( '\n' ) {
1813- format ! (
1814- "{} [Read more]({})" ,
1815- & plain_summary_line( Some ( s) ) ,
1816- naive_assoc_href( item, link)
1817- )
1818- } else {
1819- plain_summary_line ( Some ( s) )
1820- } ;
1821- render_markdown ( w, cx, & markdown, item. links ( ) , prefix, is_hidden) ;
1807+ let mut summary_html = MarkdownSummaryLine ( s, & item. links ( ) ) . into_string ( ) ;
1808+
1809+ if s. contains ( '\n' ) {
1810+ let link = format ! ( r#" <a href="{}">Read more</a>"# , naive_assoc_href( item, link) ) ;
1811+
1812+ if let Some ( idx) = summary_html. rfind ( "</p>" ) {
1813+ summary_html. insert_str ( idx, & link) ;
1814+ } else {
1815+ summary_html. push_str ( & link) ;
1816+ }
1817+ }
1818+
1819+ write ! (
1820+ w,
1821+ "<div class='docblock{}'>{}{}</div>" ,
1822+ if is_hidden { " hidden" } else { "" } ,
1823+ prefix,
1824+ summary_html,
1825+ ) ;
18221826 } else if !prefix. is_empty ( ) {
18231827 write ! (
18241828 w,
@@ -3689,7 +3693,7 @@ fn render_impl(
36893693 } else if show_def_docs {
36903694 // In case the item isn't documented,
36913695 // provide short documentation from the trait.
3692- document_short ( w, cx , it, link, "" , is_hidden) ;
3696+ document_short ( w, it, link, "" , is_hidden) ;
36933697 }
36943698 }
36953699 } else {
@@ -3701,7 +3705,7 @@ fn render_impl(
37013705 } else {
37023706 document_stability ( w, cx, item, is_hidden) ;
37033707 if show_def_docs {
3704- document_short ( w, cx , item, link, "" , is_hidden) ;
3708+ document_short ( w, item, link, "" , is_hidden) ;
37053709 }
37063710 }
37073711 }
0 commit comments