Skip to content

Commit

Permalink
update markdown highlighting to use separate heading themes
Browse files Browse the repository at this point in the history
  • Loading branch information
CptPotato committed Feb 3, 2022
1 parent d3221b0 commit a39c452
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
2 changes: 2 additions & 0 deletions book/src/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ We use a similar set of scopes as

- `markup`
- `heading`
- `marker`
- `1`, `2`, `3`, `4`, `5`, `6` - heading text for h1 through h6
- `list`
- `unnumbered`
- `numbered`
Expand Down
54 changes: 36 additions & 18 deletions helix-term/src/ui/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use tui::{

use std::sync::Arc;

use pulldown_cmark::{CodeBlockKind, CowStr, Event, Options, Parser, Tag};
use pulldown_cmark::{CodeBlockKind, CowStr, Event, HeadingLevel, Options, Parser, Tag};

use helix_core::{
syntax::{self, HighlightEvent, Syntax},
Rope,
};
use helix_view::{
graphics::{Margin, Rect},
graphics::{Margin, Rect, Style},
Theme,
};

Expand All @@ -24,7 +24,7 @@ pub struct Markdown {

text_style: String,
block_style: String,
heading_style: String,
heading_styles: [String; 6],
}

// TODO: pre-render and self reference via Pin
Expand All @@ -37,14 +37,23 @@ impl Markdown {
config_loader,
text_style: "markup.normal".into(),
block_style: "markup.raw.inline".into(),
heading_style: "markup.heading".into(),
heading_styles: [
"markup.heading.1".into(),
"markup.heading.2".into(),
"markup.heading.3".into(),
"markup.heading.4".into(),
"markup.heading.5".into(),
"markup.heading.6".into(),
],
}
}

pub fn style_group(mut self, suffix: &str) -> Self {
self.text_style = format!("markup.normal.{}", suffix);
self.block_style = format!("markup.raw.inline.{}", suffix);
self.heading_style = format!("markup.heading.{}", suffix);
for i in 0..self.heading_styles.len() {
self.heading_styles[i] = format!("markup.heading.{}.{}", i + 1, suffix);
}
self
}

Expand All @@ -70,17 +79,19 @@ impl Markdown {
})
}

macro_rules! get_theme {
($s1: expr) => {
theme
.map(|theme| theme.try_get($s1.as_str()))
.flatten()
.unwrap_or_default()
};
}
let text_style = get_theme!(self.text_style);
let code_style = get_theme!(self.block_style);
let heading_style = get_theme!(self.heading_style);
let get_theme = |key: &str| {
theme
.map(|theme| theme.try_get(key))
.flatten()
.unwrap_or_default()
};
let text_style = get_theme(&self.text_style);
let code_style = get_theme(&self.block_style);
let heading_styles: Vec<Style> = self
.heading_styles
.iter()
.map(|key| get_theme(key))
.collect();

for event in parser {
match event {
Expand Down Expand Up @@ -175,9 +186,16 @@ impl Markdown {
lines.push(Spans::from(span));
}
}
} else if let Some(Tag::Heading(_, _, _)) = tags.last() {
} else if let Some(Tag::Heading(level, _, _)) = tags.last() {
let mut span = to_span(text);
span.style = heading_style;
span.style = match level {
HeadingLevel::H1 => heading_styles[0],
HeadingLevel::H2 => heading_styles[1],
HeadingLevel::H3 => heading_styles[2],
HeadingLevel::H4 => heading_styles[3],
HeadingLevel::H5 => heading_styles[4],
HeadingLevel::H6 => heading_styles[5],
};
spans.push(span);
} else {
let mut span = to_span(text);
Expand Down
13 changes: 9 additions & 4 deletions runtime/queries/markdown/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
[
(atx_heading)
(setext_heading)
] @markup.heading
(setext_heading (heading_content) @markup.heading.1 (setext_h1_underline) @markup.heading.marker)
(setext_heading (heading_content) @markup.heading.2 (setext_h2_underline) @markup.heading.marker)

(atx_heading (atx_h1_marker) @markup.heading.marker (heading_content) @markup.heading.1)
(atx_heading (atx_h2_marker) @markup.heading.marker (heading_content) @markup.heading.2)
(atx_heading (atx_h3_marker) @markup.heading.marker (heading_content) @markup.heading.3)
(atx_heading (atx_h4_marker) @markup.heading.marker (heading_content) @markup.heading.4)
(atx_heading (atx_h5_marker) @markup.heading.marker (heading_content) @markup.heading.5)
(atx_heading (atx_h6_marker) @markup.heading.marker (heading_content) @markup.heading.6)

(code_fence_content) @none

Expand Down

0 comments on commit a39c452

Please sign in to comment.