1
1
use std:: iter:: Peekable ;
2
2
3
3
use egui_commonmark_backend:: {
4
- alerts:: Alert , misc:: Style , pulldown:: * , CommonMarkOptions , FencedCodeBlock , Image ,
4
+ alerts:: Alert , misc:: Style , pulldown:: * , CodeBlock , CommonMarkOptions , Image ,
5
5
} ;
6
6
7
7
use proc_macro2:: TokenStream ;
@@ -169,7 +169,7 @@ pub(crate) struct CommonMarkViewerInternal {
169
169
link : Option < StyledLink > ,
170
170
image : Option < StyledImage > ,
171
171
line : Newline ,
172
- fenced_code_block : Option < FencedCodeBlock > ,
172
+ code_block : Option < CodeBlock > ,
173
173
is_list_item : bool ,
174
174
def_list : DefinitionList ,
175
175
is_table : bool ,
@@ -192,7 +192,7 @@ impl CommonMarkViewerInternal {
192
192
line : Newline :: default ( ) ,
193
193
is_list_item : false ,
194
194
def_list : Default :: default ( ) ,
195
- fenced_code_block : None ,
195
+ code_block : None ,
196
196
is_table : false ,
197
197
is_blockquote : false ,
198
198
dumps_heading : false ,
@@ -559,7 +559,7 @@ impl CommonMarkViewerInternal {
559
559
image
560
560
. alt_text
561
561
. push ( StyledText :: new ( self . text_style . clone ( ) , text. to_string ( ) ) ) ;
562
- } else if let Some ( block) = & mut self . fenced_code_block {
562
+ } else if let Some ( block) = & mut self . code_block {
563
563
block. content . push_str ( & text) ;
564
564
} else if let Some ( link) = & mut self . link {
565
565
link. text
@@ -595,18 +595,22 @@ impl CommonMarkViewerInternal {
595
595
TokenStream :: new ( )
596
596
}
597
597
pulldown_cmark:: Tag :: CodeBlock ( c) => {
598
- self . text_style . code = true ;
599
-
600
- if let pulldown_cmark:: CodeBlockKind :: Fenced ( lang) = c {
601
- self . fenced_code_block = Some ( FencedCodeBlock {
602
- lang : lang. to_string ( ) ,
603
- content : "" . to_string ( ) ,
604
- } ) ;
605
-
606
- self . line . try_insert_start ( )
607
- } else {
608
- TokenStream :: new ( )
598
+ match c {
599
+ pulldown_cmark:: CodeBlockKind :: Fenced ( lang) => {
600
+ self . code_block = Some ( CodeBlock {
601
+ lang : Some ( lang. to_string ( ) ) ,
602
+ content : "" . to_string ( ) ,
603
+ } ) ;
604
+ }
605
+ pulldown_cmark:: CodeBlockKind :: Indented => {
606
+ self . code_block = Some ( CodeBlock {
607
+ lang : None ,
608
+ content : "" . to_string ( ) ,
609
+ } ) ;
610
+ }
609
611
}
612
+
613
+ self . line . try_insert_start ( )
610
614
}
611
615
612
616
pulldown_cmark:: Tag :: List ( point) => {
@@ -807,21 +811,22 @@ impl CommonMarkViewerInternal {
807
811
808
812
fn end_code_block ( & mut self , cache : & Expr ) -> TokenStream {
809
813
let mut stream = TokenStream :: new ( ) ;
810
- if let Some ( block) = self . fenced_code_block . take ( ) {
811
- let lang = block. lang ;
814
+ if let Some ( block) = self . code_block . take ( ) {
812
815
let content = block. content ;
813
816
814
- stream. extend (
815
- quote ! (
816
- egui_commonmark_backend:: FencedCodeBlock { lang: #lang. to_owned( ) , content: #content. to_owned( ) }
817
- . end( ui, #cache, & options, max_width) ;
818
- ) ) ;
817
+ stream. extend ( if let Some ( lang) = block. lang {
818
+ quote ! ( egui_commonmark_backend:: CodeBlock {
819
+ lang: Some ( #lang. to_owned( ) ) , content: #content. to_owned( ) }
820
+ . end( ui, #cache, & options, max_width) ; )
821
+ } else {
822
+ quote ! ( egui_commonmark_backend:: CodeBlock {
823
+ lang: None , content: #content. to_owned( ) }
824
+ . end( ui, #cache, & options, max_width) ; )
825
+ } ) ;
819
826
820
827
stream. extend ( self . line . try_insert_end ( ) ) ;
821
828
}
822
829
823
- self . text_style . code = false ;
824
-
825
830
stream
826
831
}
827
832
0 commit comments