@@ -36,6 +36,10 @@ static MORE_DIVIDER_RE: Lazy<Regex> = Lazy::new(|| {
36
36
. unwrap ( )
37
37
} ) ;
38
38
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
+
39
43
/// Although there exists [a list of registered URI schemes][uri-schemes], a link may use arbitrary,
40
44
/// private schemes. This regex checks if the given string starts with something that just looks
41
45
/// 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 {
78
82
#[ derive( Debug ) ]
79
83
pub struct Rendered {
80
84
pub body : String ,
81
- pub summary_len : Option < usize > ,
85
+ pub summary : Option < String > ,
82
86
pub toc : Vec < Heading > ,
83
87
/// Links to site-local pages: relative path plus optional anchor target.
84
88
pub internal_links : Vec < ( String , Option < String > ) > ,
@@ -405,6 +409,7 @@ pub fn markdown_to_html(
405
409
. map ( |x| x. as_object ( ) . unwrap ( ) . get ( "relative_path" ) . unwrap ( ) . as_str ( ) . unwrap ( ) ) ;
406
410
// the rendered html
407
411
let mut html = String :: with_capacity ( content. len ( ) ) ;
412
+ let mut summary = None ;
408
413
// Set while parsing
409
414
let mut error = None ;
410
415
@@ -679,17 +684,13 @@ pub fn markdown_to_html(
679
684
event
680
685
} ) ;
681
686
}
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
+ {
693
694
render_shortcodes ! ( false , text, range) ;
694
695
}
695
696
_ => events. push ( event) ,
@@ -781,14 +782,31 @@ pub fn markdown_to_html(
781
782
convert_footnotes_to_github_style ( & mut events) ;
782
783
}
783
784
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) ;
785
803
}
786
804
787
805
if let Some ( e) = error {
788
806
Err ( e)
789
807
} else {
790
808
Ok ( Rendered {
791
- summary_len : if has_summary { html . find ( CONTINUE_READING ) } else { None } ,
809
+ summary ,
792
810
body : html,
793
811
toc : make_table_of_contents ( headings) ,
794
812
internal_links,
@@ -861,10 +879,10 @@ mod tests {
861
879
for more in mores {
862
880
let content = format ! ( "{top}\n \n {more}\n \n {bottom}" ) ;
863
881
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 ( ) ;
868
886
let continue_reading = & body[ ..CONTINUE_READING . len ( ) ] ;
869
887
let body = & body[ CONTINUE_READING . len ( ) ..] . trim ( ) ;
870
888
assert_eq ! ( summary, & top_rendered) ;
0 commit comments