@@ -1506,6 +1506,7 @@ impl Context {
1506
1506
}
1507
1507
}
1508
1508
1509
+ /// Construct a map of items shown in the sidebar to a plain-text summary of their docs.
1509
1510
fn build_sidebar_items ( & self , m : & clean:: Module ) -> BTreeMap < String , Vec < NameDoc > > {
1510
1511
// BTreeMap instead of HashMap to get a sorted output
1511
1512
let mut map: BTreeMap < _ , Vec < _ > > = BTreeMap :: new ( ) ;
@@ -1522,7 +1523,7 @@ impl Context {
1522
1523
let short = short. to_string ( ) ;
1523
1524
map. entry ( short)
1524
1525
. or_default ( )
1525
- . push ( ( myname, Some ( plain_summary_line ( item. doc_value ( ) ) ) ) ) ;
1526
+ . push ( ( myname, Some ( plain_text_summary ( item. doc_value ( ) ) ) ) ) ;
1526
1527
}
1527
1528
1528
1529
if self . shared . sort_modules_alphabetically {
@@ -1728,22 +1729,15 @@ fn full_path(cx: &Context, item: &clean::Item) -> String {
1728
1729
s
1729
1730
}
1730
1731
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.
1731
1738
#[ 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 ( )
1747
1741
}
1748
1742
1749
1743
crate fn shorten ( s : String ) -> String {
@@ -1800,25 +1794,35 @@ fn render_markdown(
1800
1794
)
1801
1795
}
1802
1796
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.
1803
1799
fn document_short (
1804
1800
w : & mut Buffer ,
1805
- cx : & Context ,
1806
1801
item : & clean:: Item ,
1807
1802
link : AssocItemLink < ' _ > ,
1808
1803
prefix : & str ,
1809
1804
is_hidden : bool ,
1810
1805
) {
1811
1806
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
+ ) ;
1822
1826
} else if !prefix. is_empty ( ) {
1823
1827
write ! (
1824
1828
w,
@@ -3689,7 +3693,7 @@ fn render_impl(
3689
3693
} else if show_def_docs {
3690
3694
// In case the item isn't documented,
3691
3695
// provide short documentation from the trait.
3692
- document_short ( w, cx , it, link, "" , is_hidden) ;
3696
+ document_short ( w, it, link, "" , is_hidden) ;
3693
3697
}
3694
3698
}
3695
3699
} else {
@@ -3701,7 +3705,7 @@ fn render_impl(
3701
3705
} else {
3702
3706
document_stability ( w, cx, item, is_hidden) ;
3703
3707
if show_def_docs {
3704
- document_short ( w, cx , item, link, "" , is_hidden) ;
3708
+ document_short ( w, item, link, "" , is_hidden) ;
3705
3709
}
3706
3710
}
3707
3711
}
0 commit comments