@@ -10,18 +10,22 @@ use tui::{
10use std::borrow::Cow;
10use std::borrow::Cow;
12use helix_core::Position;
12use helix_core::Position;
13−use helix_view::Editor;
13+use helix_view::{Editor, Theme};
15pub struct Markdown {
15pub struct Markdown {
16 contents: String,
16 contents: String,
19+// TODO: pre-render and self reference via Pin
20+// better yet, just use Tendril + subtendril for references
19impl Markdown {
22impl Markdown {
20 pub fn new(contents: String) -> Self {
23 pub fn new(contents: String) -> Self {
21 Self { contents }
24 Self { contents }
24−fn parse(contents: &str) -> tui::text::Text {
28+fn parse<'a>(contents: &'a str, theme: Option<&Theme>) -> tui::text::Text<'a> {
25 use pulldown_cmark::{CodeBlockKind, CowStr, Event, Options, Parser, Tag};
29 use pulldown_cmark::{CodeBlockKind, CowStr, Event, Options, Parser, Tag};
26 use tui::text::{Span, Spans, Text};
30 use tui::text::{Span, Spans, Text};
@@ -68,10 +72,73 @@ fn parse(contents: &str) -> tui::text::Text {
70 Event::Text(text) => {
74 Event::Text(text) => {
75+ // TODO: temp workaround
71− if let Some(Tag::CodeBlock(CodeBlockKind::Fenced(_))) = tags.last() {
76+ if let Some(Tag::CodeBlock(CodeBlockKind::Fenced(language))) = tags.last() {
72− for line in text.lines() {
77+ if let Some(theme) = theme {
78+ use helix_core::syntax::{self, HighlightEvent, Syntax};
79+ use helix_core::Rope;
81+ let rope = Rope::from(text.as_ref());
82+ let syntax = syntax::LOADER
83+ .language_config_for_scope(&format!("source.{}", language))
84+ .and_then(|config| config.highlight_config(theme.scopes()))
85+ .map(|config| Syntax::new(&rope, config));
87+ if let Some(mut syntax) = syntax {
88+ // if we have a syntax available, highlight_iter and generate spans
89+ let mut highlights = Vec::new();
91+ for event in syntax.highlight_iter(rope.slice(..), None, None, |_| None)
93+ match event.unwrap() {
94+ HighlightEvent::HighlightStart(span) => {
95+ highlights.push(span);
97+ HighlightEvent::HighlightEnd => {
100+ HighlightEvent::Source { start, end } => {
101+ let style = match highlights.first() {
103+ theme.get(theme.scopes()[span.0].as_str())
105+ None => Style::default().fg(Color::Rgb(164, 160, 232)), // lavender
108+ let mut slice = &text[start..end];
109+ while let Some(end) = slice.find('\n') {
110+ // emit span up to newline
111+ let text = &slice[..end];
112+ let span = Span::styled(text.to_owned(), style);
115+ // truncate slice to after newline
116+ slice = &slice[end + 1..];
119+ let spans = std::mem::replace(&mut spans, Vec::new());
120+ lines.push(Spans::from(spans));
123+ // if there's anything left, emit it too
124+ if !slice.is_empty() {
125+ let span = Span::styled(slice.to_owned(), style);
132+ for line in text.lines() {
73− let mut span = Span::styled(line.to_string(), code_style);
133+ let mut span = Span::styled(line.to_string(), code_style);
134+ lines.push(Spans::from(span));
138+ for line in text.lines() {
139+ let mut span = Span::styled(line.to_string(), code_style);
74− lines.push(Spans::from(span));
140+ lines.push(Spans::from(span));
76 } else if let Some(Tag::Heading(_)) = tags.last() {
143 } else if let Some(Tag::Heading(_)) = tags.last() {
77 let mut span = to_span(text);
144 let mut span = to_span(text);
@@ -113,7 +180,7 @@ impl Component for Markdown {
113 fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
180 fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
114 use tui::widgets::{Paragraph, Widget, Wrap};
181 use tui::widgets::{Paragraph, Widget, Wrap};
116− let text = parse(&self.contents);
183+ let text = parse(&self.contents, Some(&cx.editor.theme));
118 let par = Paragraph::new(text)
185 let par = Paragraph::new(text)
119 .wrap(Wrap { trim: false })
186 .wrap(Wrap { trim: false })
@@ -124,18 +191,10 @@ impl Component for Markdown {
126 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
193 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
127− let contents = parse(&self.contents);
194+ let contents = parse(&self.contents, None);
128 let padding = 2;
195 let padding = 2;
129 let width = std::cmp::min(contents.width() as u16 + padding, viewport.0);
196 let width = std::cmp::min(contents.width() as u16 + padding, viewport.0);
130 let height = std::cmp::min(contents.height() as u16 + padding, viewport.1);
197 let height = std::cmp::min(contents.height() as u16 + padding, viewport.1);
131 Some((width, height))
198 Some((width, height))