@@ -36,6 +36,10 @@ static MORE_DIVIDER_RE: Lazy<Regex> = Lazy::new(|| {
3636 . unwrap ( )
3737} ) ;
3838
39+ static FOOTNOTES_RE : Lazy < Regex > = Lazy :: new ( || {
40+ Regex :: new ( r#"<sup class="footnote-reference"><a href=\s*.*?>\s*.*?</a></sup>"# ) . unwrap ( )
41+ } ) ;
42+
3943/// Although there exists [a list of registered URI schemes][uri-schemes], a link may use arbitrary,
4044/// private schemes. This regex checks if the given string starts with something that just looks
4145/// like a scheme, i.e., a case-insensitive identifier followed by a colon.
@@ -78,7 +82,7 @@ fn is_colocated_asset_link(link: &str) -> bool {
7882#[ derive( Debug ) ]
7983pub struct Rendered {
8084 pub body : String ,
81- pub summary_len : Option < usize > ,
85+ pub summary : Option < String > ,
8286 pub toc : Vec < Heading > ,
8387 /// Links to site-local pages: relative path plus optional anchor target.
8488 pub internal_links : Vec < ( String , Option < String > ) > ,
@@ -405,6 +409,7 @@ pub fn markdown_to_html(
405409 . map ( |x| x. as_object ( ) . unwrap ( ) . get ( "relative_path" ) . unwrap ( ) . as_str ( ) . unwrap ( ) ) ;
406410 // the rendered html
407411 let mut html = String :: with_capacity ( content. len ( ) ) ;
412+ let mut summary = None ;
408413 // Set while parsing
409414 let mut error = None ;
410415
@@ -679,17 +684,13 @@ pub fn markdown_to_html(
679684 event
680685 } ) ;
681686 }
682- Event :: Html ( text) => {
683- if !has_summary && MORE_DIVIDER_RE . is_match ( & text) {
684- has_summary = true ;
685- events. push ( Event :: Html ( CONTINUE_READING . into ( ) ) ) ;
686- continue ;
687- }
688- if !contains_shortcode ( text. as_ref ( ) ) {
689- events. push ( Event :: Html ( text) ) ;
690- continue ;
691- }
692-
687+ Event :: Html ( text) if !has_summary && MORE_DIVIDER_RE . is_match ( text. as_ref ( ) ) => {
688+ has_summary = true ;
689+ events. push ( Event :: Html ( CONTINUE_READING . into ( ) ) ) ;
690+ }
691+ Event :: Html ( text) | Event :: InlineHtml ( text)
692+ if contains_shortcode ( text. as_ref ( ) ) =>
693+ {
693694 render_shortcodes ! ( false , text, range) ;
694695 }
695696 _ => events. push ( event) ,
@@ -781,14 +782,31 @@ pub fn markdown_to_html(
781782 convert_footnotes_to_github_style ( & mut events) ;
782783 }
783784
784- cmark:: html:: push_html ( & mut html, events. into_iter ( ) ) ;
785+ let continue_reading = events
786+ . iter ( )
787+ . position ( |e| matches ! ( e, Event :: Html ( CowStr :: Borrowed ( CONTINUE_READING ) ) ) )
788+ . unwrap_or ( events. len ( ) ) ;
789+
790+ let mut events = events. into_iter ( ) ;
791+
792+ // emit everything up to summary
793+ cmark:: html:: push_html ( & mut html, events. by_ref ( ) . take ( continue_reading) ) ;
794+
795+ if has_summary {
796+ // remove footnotes
797+ let summary_html = FOOTNOTES_RE . replace_all ( & html, "" ) . into_owned ( ) ;
798+ summary = Some ( summary_html)
799+ }
800+
801+ // emit everything after summary
802+ cmark:: html:: push_html ( & mut html, events) ;
785803 }
786804
787805 if let Some ( e) = error {
788806 Err ( e)
789807 } else {
790808 Ok ( Rendered {
791- summary_len : if has_summary { html . find ( CONTINUE_READING ) } else { None } ,
809+ summary ,
792810 body : html,
793811 toc : make_table_of_contents ( headings) ,
794812 internal_links,
@@ -861,10 +879,10 @@ mod tests {
861879 for more in mores {
862880 let content = format ! ( "{top}\n \n {more}\n \n {bottom}" ) ;
863881 let rendered = markdown_to_html ( & content, & context, vec ! [ ] ) . unwrap ( ) ;
864- assert ! ( rendered. summary_len . is_some( ) , "no summary when splitting on {more}" ) ;
865- let summary_len = rendered. summary_len . unwrap ( ) ;
866- let summary = & rendered . body [ ..summary_len ] . trim ( ) ;
867- let body = & rendered. body [ summary_len ..] . trim ( ) ;
882+ assert ! ( rendered. summary . is_some( ) , "no summary when splitting on {more}" ) ;
883+ let summary = rendered. summary . unwrap ( ) ;
884+ let summary = summary . trim ( ) ;
885+ let body = rendered. body [ summary . len ( ) ..] . trim ( ) ;
868886 let continue_reading = & body[ ..CONTINUE_READING . len ( ) ] ;
869887 let body = & body[ CONTINUE_READING . len ( ) ..] . trim ( ) ;
870888 assert_eq ! ( summary, & top_rendered) ;
0 commit comments