Skip to content

Commit

Permalink
Add space between definition list titles
Browse files Browse the repository at this point in the history
  • Loading branch information
lampsitter committed Aug 24, 2024
1 parent 6b86fe9 commit 6c6938f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 4 additions & 0 deletions egui_commonmark/examples/mixing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ Term 2
: Definition 2
Paragraph 2
Term 3
: Definition 3
"#,
);

Expand Down
27 changes: 21 additions & 6 deletions egui_commonmark/src/parsers/pulldown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -68,7 +74,7 @@ pub struct CommonMarkViewerInternal {
line: Newline,
fenced_code_block: Option<FencedCodeBlock>,
is_list_item: bool,
is_def_list_def: bool,
def_list: DefinitionList,
is_table: bool,
is_blockquote: bool,
checkbox_events: Vec<CheckboxClickEvent>,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
31 changes: 24 additions & 7 deletions egui_commonmark_macros/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ pub struct StyledImage {
pub alt_text: Vec<StyledText>,
}

#[derive(Default)]
struct DefinitionList {
is_first_item: bool,
is_def_list_def: bool,
}

pub(crate) struct CommonMarkViewerInternal {
source_id: Id,
curr_table: usize,
Expand All @@ -168,7 +174,7 @@ pub(crate) struct CommonMarkViewerInternal {
line: Newline,
fenced_code_block: Option<FencedCodeBlock>,
is_list_item: bool,
is_def_list_def: bool,
def_list: DefinitionList,
is_table: bool,
is_blockquote: bool,

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
}
Expand Down

0 comments on commit 6c6938f

Please sign in to comment.