diff --git a/CHANGELOG.md b/CHANGELOG.md index f3effc1..0207859 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,17 +2,17 @@ ## Unreleased -## Added +### Added - Definition lists -## Changed +### Changed - Updated pulldown-cmark to 0.12 - Newlines are no longer inserted before/after markdown ([#56](https://github.com/lampsitter/egui_commonmark/pull/56)) > For the old behaviour you can call `ui.label("");` before and and after -## Removed +### Removed - Experimental comrak backend ([#57](https://github.com/lampsitter/egui_commonmark/pull/57)) - Deprecated method `syntax_theme` diff --git a/egui_commonmark/examples/mixing.rs b/egui_commonmark/examples/mixing.rs index 3740942..bc44f9f 100644 --- a/egui_commonmark/examples/mixing.rs +++ b/egui_commonmark/examples/mixing.rs @@ -93,6 +93,10 @@ Term 2 : Definition 2 Paragraph 2 + +Term 3 + +: Definition 3 "#, ); diff --git a/egui_commonmark/src/parsers/pulldown.rs b/egui_commonmark/src/parsers/pulldown.rs index 5764973..a597096 100644 --- a/egui_commonmark/src/parsers/pulldown.rs +++ b/egui_commonmark/src/parsers/pulldown.rs @@ -58,6 +58,12 @@ impl Newline { } } +#[derive(Default)] +struct DefinitionList { + is_first_item: bool, + is_def_list_def: bool, +} + pub struct CommonMarkViewerInternal { source_id: Id, curr_table: usize, @@ -68,7 +74,7 @@ pub struct CommonMarkViewerInternal { line: Newline, fenced_code_block: Option, is_list_item: bool, - is_def_list_def: bool, + def_list: DefinitionList, is_table: bool, is_blockquote: bool, checkbox_events: Vec, @@ -90,7 +96,7 @@ impl CommonMarkViewerInternal { image: None, line: Newline::default(), is_list_item: false, - is_def_list_def: false, + def_list: Default::default(), fenced_code_block: None, is_table: false, is_blockquote: false, @@ -281,8 +287,8 @@ impl CommonMarkViewerInternal { options: &CommonMarkOptions, ui: &mut Ui, ) { - if self.is_def_list_def { - self.is_def_list_def = false; + if self.def_list.is_def_list_def { + self.def_list.is_def_list_def = false; let item_events = delayed_events(events, |tag| { matches!(tag, pulldown_cmark::TagEnd::DefinitionListDefinition) @@ -631,10 +637,19 @@ impl CommonMarkViewerInternal { pulldown_cmark::Tag::DefinitionList => { self.line.try_insert_start(ui); + self.def_list.is_first_item = true; + } + pulldown_cmark::Tag::DefinitionListTitle => { + // we disable newline as the first title should not insert a newline + // as we have already done that upon the DefinitionList Tag + if !self.def_list.is_first_item { + self.line.try_insert_start(ui) + } else { + self.def_list.is_first_item = false; + } } - pulldown_cmark::Tag::DefinitionListTitle => {} pulldown_cmark::Tag::DefinitionListDefinition => { - self.is_def_list_def = true; + self.def_list.is_def_list_def = true; } } } diff --git a/egui_commonmark_macros/src/generator.rs b/egui_commonmark_macros/src/generator.rs index bf0cdbb..dded0b5 100644 --- a/egui_commonmark_macros/src/generator.rs +++ b/egui_commonmark_macros/src/generator.rs @@ -158,6 +158,12 @@ pub struct StyledImage { pub alt_text: Vec, } +#[derive(Default)] +struct DefinitionList { + is_first_item: bool, + is_def_list_def: bool, +} + pub(crate) struct CommonMarkViewerInternal { source_id: Id, curr_table: usize, @@ -168,7 +174,7 @@ pub(crate) struct CommonMarkViewerInternal { line: Newline, fenced_code_block: Option, is_list_item: bool, - is_def_list_def: bool, + def_list: DefinitionList, is_table: bool, is_blockquote: bool, @@ -189,7 +195,7 @@ impl CommonMarkViewerInternal { image: None, line: Newline::default(), is_list_item: false, - is_def_list_def: false, + def_list: Default::default(), fenced_code_block: None, is_table: false, is_blockquote: false, @@ -279,8 +285,8 @@ impl CommonMarkViewerInternal { options: &CommonMarkOptions, ) -> TokenStream { let mut stream = TokenStream::new(); - if self.is_def_list_def { - self.is_def_list_def = false; + if self.def_list.is_def_list_def { + self.def_list.is_def_list_def = false; let item_events = delayed_events(events, |tag| { matches!(tag, pulldown_cmark::TagEnd::DefinitionListDefinition) @@ -677,10 +683,21 @@ impl CommonMarkViewerInternal { pulldown_cmark::Tag::HtmlBlock | pulldown_cmark::Tag::MetadataBlock(_) => { TokenStream::new() } - pulldown_cmark::Tag::DefinitionList => self.line.try_insert_start(), - pulldown_cmark::Tag::DefinitionListTitle => TokenStream::new(), + pulldown_cmark::Tag::DefinitionList => { + let s = self.line.try_insert_start(); + self.def_list.is_first_item = true; + s + } + pulldown_cmark::Tag::DefinitionListTitle => { + if !self.def_list.is_first_item { + self.line.try_insert_start() + } else { + self.def_list.is_first_item = false; + TokenStream::new() + } + } pulldown_cmark::Tag::DefinitionListDefinition => { - self.is_def_list_def = true; + self.def_list.is_def_list_def = true; TokenStream::new() } }