Skip to content

Commit 8848291

Browse files
committed
Inline code blocks
1 parent bba8ec6 commit 8848291

File tree

9 files changed

+95
-54
lines changed

9 files changed

+95
-54
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- Definition lists
8+
- Proper inline code block rendering
89

910
### Changed
1011

egui_commonmark/examples/macros.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,17 @@ impl eframe::App for App {
6161

6262
commonmark!(ui, &mut self.cache, "------------");
6363

64-
commonmark_str!(
65-
ui,
66-
&mut self.cache,
67-
"egui_commonmark/examples/markdown/tables.md"
68-
);
64+
// The table will end up with the same id as the table in the hello_world file.
65+
// Providing the id explicitly is annoying for all other widgets that are not tables
66+
// so push_id must be used in this case.
67+
ui.push_id("tables", |ui| {
68+
commonmark_str!(
69+
ui,
70+
&mut self.cache,
71+
"egui_commonmark/examples/markdown/tables.md"
72+
);
73+
});
74+
6975
commonmark!(ui, &mut self.cache, "------------");
7076

7177
commonmark_str!(

egui_commonmark/examples/markdown/code-blocks.md

+3
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ image = { version = "0.24", default-features = false, features = ["png"] }
2626
```
2727
- Code blocks can be in lists too :)
2828

29+
2930
More content...
31+
32+
Inline code blocks are supported if you for some reason need them

egui_commonmark/examples/mixing.rs

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ let x = 3;
5454
```
5555
"#,
5656
r#"
57+
let x = 3;
58+
"#,
59+
r#"
5760
A footnote [^F1]
5861
5962
[^F1]: The footnote"#,

egui_commonmark/examples/scroll.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct App {
1515
impl eframe::App for App {
1616
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
1717
let mut text = r#"# Commonmark Viewer Example
18-
This is a fairly large markdown file showcasing scroll.
18+
This is a fairly large markdown file showcasing scroll.
1919
"#
2020
.to_string();
2121

egui_commonmark/src/parsers/pulldown.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub struct CommonMarkViewerInternal {
7171
link: Option<Link>,
7272
image: Option<Image>,
7373
line: Newline,
74-
fenced_code_block: Option<FencedCodeBlock>,
74+
code_block: Option<CodeBlock>,
7575
is_list_item: bool,
7676
def_list: DefinitionList,
7777
is_table: bool,
@@ -95,7 +95,7 @@ impl CommonMarkViewerInternal {
9595
line: Newline::default(),
9696
is_list_item: false,
9797
def_list: Default::default(),
98-
fenced_code_block: None,
98+
code_block: None,
9999
is_table: false,
100100
is_blockquote: false,
101101
checkbox_events: Vec::new(),
@@ -542,7 +542,7 @@ impl CommonMarkViewerInternal {
542542
let rich_text = self.text_style.to_richtext(ui, &text);
543543
if let Some(image) = &mut self.image {
544544
image.alt_text.push(rich_text);
545-
} else if let Some(block) = &mut self.fenced_code_block {
545+
} else if let Some(block) = &mut self.code_block {
546546
block.content.push_str(&text);
547547
} else if let Some(link) = &mut self.link {
548548
link.text.push(rich_text);
@@ -576,17 +576,23 @@ impl CommonMarkViewerInternal {
576576
self.is_blockquote = true;
577577
}
578578
pulldown_cmark::Tag::CodeBlock(c) => {
579-
if let pulldown_cmark::CodeBlockKind::Fenced(lang) = c {
580-
self.fenced_code_block = Some(crate::FencedCodeBlock {
581-
lang: lang.to_string(),
582-
content: "".to_string(),
583-
});
584-
585-
self.line.try_insert_start(ui);
579+
match c {
580+
pulldown_cmark::CodeBlockKind::Fenced(lang) => {
581+
self.code_block = Some(crate::CodeBlock {
582+
lang: Some(lang.to_string()),
583+
content: "".to_string(),
584+
});
585+
}
586+
pulldown_cmark::CodeBlockKind::Indented => {
587+
self.code_block = Some(crate::CodeBlock {
588+
lang: None,
589+
content: "".to_string(),
590+
});
591+
}
586592
}
587-
588-
self.text_style.code = true;
593+
self.line.try_insert_start(ui);
589594
}
595+
590596
pulldown_cmark::Tag::List(point) => {
591597
if !self.list.is_inside_a_list() && self.line.can_insert_start() {
592598
newline(ui);
@@ -739,10 +745,9 @@ impl CommonMarkViewerInternal {
739745
options: &CommonMarkOptions,
740746
max_width: f32,
741747
) {
742-
if let Some(block) = self.fenced_code_block.take() {
748+
if let Some(block) = self.code_block.take() {
743749
block.end(ui, cache, options, max_width);
744750
self.line.try_insert_end(ui);
745751
}
746-
self.text_style.code = false;
747752
}
748753
}

egui_commonmark_backend/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use {
1717
alerts::{alert_ui, Alert, AlertBundle},
1818
// Pretty much every single element in this module is used by the proc macros
1919
elements::*,
20-
misc::{prepare_show, CommonMarkOptions, FencedCodeBlock, Image, Link},
20+
misc::{prepare_show, CodeBlock, CommonMarkOptions, Image, Link},
2121
};
2222

2323
// The only struct that is allowed to use directly. (If one does not need egui_commonmark)

egui_commonmark_backend/src/misc.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ impl Image {
229229
}
230230
}
231231

232-
pub struct FencedCodeBlock {
233-
pub lang: String,
232+
pub struct CodeBlock {
233+
pub lang: Option<String>,
234234
pub content: String,
235235
}
236236

237-
impl FencedCodeBlock {
237+
impl CodeBlock {
238238
pub fn end(
239239
&self,
240240
ui: &mut Ui,
@@ -246,7 +246,12 @@ impl FencedCodeBlock {
246246
Self::pre_syntax_highlighting(cache, options, ui);
247247

248248
let mut layout = |ui: &Ui, string: &str, wrap_width: f32| {
249-
let mut job = self.syntax_highlighting(cache, options, &self.lang, ui, string);
249+
let mut job = if let Some(lang) = &self.lang {
250+
self.syntax_highlighting(cache, options, lang, ui, string)
251+
} else {
252+
plain_highlighting(ui, string)
253+
};
254+
250255
job.wrap.max_width = wrap_width;
251256
ui.fonts(|f| f.layout_job(job))
252257
};
@@ -257,7 +262,7 @@ impl FencedCodeBlock {
257262
}
258263

259264
#[cfg(not(feature = "better_syntax_highlighting"))]
260-
impl FencedCodeBlock {
265+
impl CodeBlock {
261266
fn pre_syntax_highlighting(
262267
_cache: &mut CommonMarkCache,
263268
_options: &CommonMarkOptions,
@@ -274,12 +279,12 @@ impl FencedCodeBlock {
274279
ui: &Ui,
275280
text: &str,
276281
) -> egui::text::LayoutJob {
277-
plain_highlighting(ui, text, extension)
282+
simple_highlighting(ui, text, extension)
278283
}
279284
}
280285

281286
#[cfg(feature = "better_syntax_highlighting")]
282-
impl FencedCodeBlock {
287+
impl CodeBlock {
283288
fn pre_syntax_highlighting(
284289
cache: &mut CommonMarkCache,
285290
options: &CommonMarkOptions,
@@ -328,12 +333,12 @@ impl FencedCodeBlock {
328333

329334
job
330335
} else {
331-
plain_highlighting(ui, text, extension)
336+
simple_highlighting(ui, text, extension)
332337
}
333338
}
334339
}
335340

336-
fn plain_highlighting(ui: &Ui, text: &str, extension: &str) -> egui::text::LayoutJob {
341+
fn simple_highlighting(ui: &Ui, text: &str, extension: &str) -> egui::text::LayoutJob {
337342
egui_extras::syntax_highlighting::highlight(
338343
ui.ctx(),
339344
&egui_extras::syntax_highlighting::CodeTheme::from_style(ui.style()),
@@ -342,6 +347,19 @@ fn plain_highlighting(ui: &Ui, text: &str, extension: &str) -> egui::text::Layou
342347
)
343348
}
344349

350+
fn plain_highlighting(ui: &Ui, text: &str) -> egui::text::LayoutJob {
351+
let mut job = egui::text::LayoutJob::default();
352+
job.append(
353+
text,
354+
0.0,
355+
egui::TextFormat::simple(
356+
TextStyle::Monospace.resolve(ui.style()),
357+
ui.style().visuals.text_color(),
358+
),
359+
);
360+
job
361+
}
362+
345363
#[cfg(feature = "better_syntax_highlighting")]
346364
fn syntect_color_to_egui(color: syntect::highlighting::Color) -> egui::Color32 {
347365
egui::Color32::from_rgb(color.r, color.g, color.b)

egui_commonmark_macros/src/generator.rs

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::iter::Peekable;
22

33
use egui_commonmark_backend::{
4-
alerts::Alert, misc::Style, pulldown::*, CommonMarkOptions, FencedCodeBlock, Image,
4+
alerts::Alert, misc::Style, pulldown::*, CodeBlock, CommonMarkOptions, Image,
55
};
66

77
use proc_macro2::TokenStream;
@@ -169,7 +169,7 @@ pub(crate) struct CommonMarkViewerInternal {
169169
link: Option<StyledLink>,
170170
image: Option<StyledImage>,
171171
line: Newline,
172-
fenced_code_block: Option<FencedCodeBlock>,
172+
code_block: Option<CodeBlock>,
173173
is_list_item: bool,
174174
def_list: DefinitionList,
175175
is_table: bool,
@@ -192,7 +192,7 @@ impl CommonMarkViewerInternal {
192192
line: Newline::default(),
193193
is_list_item: false,
194194
def_list: Default::default(),
195-
fenced_code_block: None,
195+
code_block: None,
196196
is_table: false,
197197
is_blockquote: false,
198198
dumps_heading: false,
@@ -559,7 +559,7 @@ impl CommonMarkViewerInternal {
559559
image
560560
.alt_text
561561
.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 {
563563
block.content.push_str(&text);
564564
} else if let Some(link) = &mut self.link {
565565
link.text
@@ -595,18 +595,22 @@ impl CommonMarkViewerInternal {
595595
TokenStream::new()
596596
}
597597
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+
}
609611
}
612+
613+
self.line.try_insert_start()
610614
}
611615

612616
pulldown_cmark::Tag::List(point) => {
@@ -807,21 +811,22 @@ impl CommonMarkViewerInternal {
807811

808812
fn end_code_block(&mut self, cache: &Expr) -> TokenStream {
809813
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() {
812815
let content = block.content;
813816

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+
});
819826

820827
stream.extend(self.line.try_insert_end());
821828
}
822829

823-
self.text_style.code = false;
824-
825830
stream
826831
}
827832

0 commit comments

Comments
 (0)