Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add markdown heading h2 support #1148

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w


## Unreleased
* Added markdown heading h2 support

### Added ⭐
* `Context::load_texture` to convert an image into a texture which can be displayed using e.g. `ui.image(texture, size)` ([#1110](https://github.com/emilk/egui/pull/1110)).
Expand Down
6 changes: 6 additions & 0 deletions egui/src/widget_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ impl RichText {
self.text_style(TextStyle::Heading)
}

/// Use [`TextStyle::Heading2`].
#[inline]
pub fn heading2(self) -> Self {
self.text_style(TextStyle::Heading2)
}

/// Use [`TextStyle::Monospace`].
#[inline]
pub fn monospace(self) -> Self {
Expand Down
1 change: 1 addition & 0 deletions egui_demo_lib/src/easy_mark/easy_mark_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ and is also missing some features.
----------------

# At a glance
## The Syntax
- inline text:
- normal, `code`, *strong*, ~strikethrough~, _underline_, /italics/, ^raised^, $small$
- `\` escapes the next character
Expand Down
7 changes: 6 additions & 1 deletion egui_demo_lib/src/easy_mark/easy_mark_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
} else if start_of_line && text.starts_with("# ") {
style.heading = true;
skip = 2;
} else if start_of_line && text.starts_with("## ") {
style.heading2 = true;
skip = 3;
} else if start_of_line && text.starts_with("> ") {
style.quoted = true;
skip = 2;
Expand Down Expand Up @@ -134,7 +137,7 @@ fn format_from_style(
) -> egui::text::TextFormat {
use egui::{Align, Color32, Stroke, TextStyle};

let color = if emark_style.strong || emark_style.heading {
let color = if emark_style.strong || emark_style.heading || emark_style.heading2 {
visuals.strong_text_color()
} else if emark_style.quoted {
visuals.weak_text_color()
Expand All @@ -144,6 +147,8 @@ fn format_from_style(

let text_style = if emark_style.heading {
TextStyle::Heading
} else if emark_style.heading2 {
TextStyle::Heading2
} else if emark_style.code {
TextStyle::Monospace
} else if emark_style.small | emark_style.raised {
Expand Down
10 changes: 10 additions & 0 deletions egui_demo_lib/src/easy_mark/easy_mark_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum Item<'a> {
pub struct Style {
/// # heading (large text)
pub heading: bool,
/// # heading2 (large text)
pub heading2: bool,
/// > quoted (slightly dimmer color or other font style)
pub quoted: bool,
/// `code` (monospace, some other color)
Expand Down Expand Up @@ -234,6 +236,14 @@ impl<'a> Iterator for Parser<'a> {
continue;
}

// # Heading2
if let Some(after) = self.s.strip_prefix("## ") {
self.s = after;
self.start_of_line = false;
self.style.heading2 = true;
continue;
}

// > quote
if let Some(after) = self.s.strip_prefix("> ") {
self.s = after;
Expand Down
7 changes: 7 additions & 0 deletions egui_demo_lib/src/easy_mark/easy_mark_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub fn item_ui(ui: &mut Ui, item: easy_mark::Item<'_>) {
fn rich_text_from_style(text: &str, style: &easy_mark::Style) -> RichText {
let easy_mark::Style {
heading,
heading2,
quoted,
code,
strong,
Expand All @@ -110,6 +111,12 @@ fn rich_text_from_style(text: &str, style: &easy_mark::Style) -> RichText {
if small && !heading {
rich_text = rich_text.small();
}
if heading2 && !small {
rich_text = rich_text.heading2().strong();
}
if small && !heading2 {
rich_text = rich_text.small();
}
if code {
rich_text = rich_text.code();
}
Expand Down
4 changes: 4 additions & 0 deletions epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum TextStyle {
Button,
/// Heading. Probably larger than `Body`.
Heading,
/// Heading. Probably larger than `Body` and smaller than `Heading`.
Heading2,
/// Same size as `Body`, but used when monospace is important (for aligning number, code snippets, etc).
Monospace,
}
Expand All @@ -34,6 +36,7 @@ impl TextStyle {
TextStyle::Body,
TextStyle::Button,
TextStyle::Heading,
TextStyle::Heading2,
TextStyle::Monospace,
]
.iter()
Expand Down Expand Up @@ -215,6 +218,7 @@ impl Default for FontDefinitions {
family_and_size.insert(TextStyle::Body, (FontFamily::Proportional, 14.0));
family_and_size.insert(TextStyle::Button, (FontFamily::Proportional, 14.0));
family_and_size.insert(TextStyle::Heading, (FontFamily::Proportional, 20.0));
family_and_size.insert(TextStyle::Heading2, (FontFamily::Proportional, 18.0));
family_and_size.insert(TextStyle::Monospace, (FontFamily::Monospace, 14.0));

Self {
Expand Down