Feat: UI / UX
This commit is contained in:
@@ -3,5 +3,5 @@ mod parser;
|
||||
mod theme;
|
||||
|
||||
pub use highlighter::{CodeHighlighter, NoHighlight};
|
||||
pub use parser::parse;
|
||||
pub use parser::{parse, CodeBlock, ParsedMarkdown};
|
||||
pub use theme::{DefaultTheme, MarkdownTheme};
|
||||
|
||||
@@ -5,17 +5,31 @@ use ratatui::text::{Line as RLine, Span};
|
||||
use crate::highlighter::CodeHighlighter;
|
||||
use crate::theme::MarkdownTheme;
|
||||
|
||||
pub struct CodeBlock {
|
||||
pub start_line: usize,
|
||||
pub end_line: usize,
|
||||
pub source: String,
|
||||
}
|
||||
|
||||
pub struct ParsedMarkdown {
|
||||
pub lines: Vec<RLine<'static>>,
|
||||
pub code_blocks: Vec<CodeBlock>,
|
||||
}
|
||||
|
||||
pub fn parse<T: MarkdownTheme, H: CodeHighlighter>(
|
||||
md: &str,
|
||||
theme: &T,
|
||||
highlighter: &H,
|
||||
) -> Vec<RLine<'static>> {
|
||||
) -> ParsedMarkdown {
|
||||
let processed = preprocess_markdown(md);
|
||||
let text = minimad::Text::from(processed.as_str());
|
||||
let mut lines = Vec::new();
|
||||
|
||||
let mut code_line_nr: usize = 0;
|
||||
let mut table_buffer: Vec<TableRow> = Vec::new();
|
||||
let mut code_blocks: Vec<CodeBlock> = Vec::new();
|
||||
let mut current_block_start: Option<usize> = None;
|
||||
let mut current_block_source: Vec<String> = Vec::new();
|
||||
|
||||
let flush_table = |buf: &mut Vec<TableRow>, out: &mut Vec<RLine<'static>>, theme: &T| {
|
||||
if buf.is_empty() {
|
||||
@@ -27,16 +41,43 @@ pub fn parse<T: MarkdownTheme, H: CodeHighlighter>(
|
||||
}
|
||||
};
|
||||
|
||||
let close_block = |start: Option<usize>,
|
||||
source: &mut Vec<String>,
|
||||
blocks: &mut Vec<CodeBlock>,
|
||||
lines: &Vec<RLine<'static>>| {
|
||||
if let Some(start) = start {
|
||||
blocks.push(CodeBlock {
|
||||
start_line: start,
|
||||
end_line: lines.len(),
|
||||
source: std::mem::take(source).join("\n"),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
for line in text.lines {
|
||||
let is_code = matches!(&line, Line::Normal(c) if c.style == CompositeStyle::Code);
|
||||
if !is_code {
|
||||
close_block(
|
||||
current_block_start.take(),
|
||||
&mut current_block_source,
|
||||
&mut code_blocks,
|
||||
&lines,
|
||||
);
|
||||
}
|
||||
|
||||
match line {
|
||||
Line::Normal(composite) if composite.style == CompositeStyle::Code => {
|
||||
flush_table(&mut table_buffer, &mut lines, theme);
|
||||
code_line_nr += 1;
|
||||
if current_block_start.is_none() {
|
||||
current_block_start = Some(lines.len());
|
||||
}
|
||||
let raw: String = composite
|
||||
.compounds
|
||||
.iter()
|
||||
.map(|c: &minimad::Compound| c.src)
|
||||
.collect();
|
||||
current_block_source.push(raw.clone());
|
||||
let mut spans = vec![
|
||||
Span::styled(format!(" {code_line_nr:>2} "), theme.code_border()),
|
||||
Span::styled("│ ", theme.code_border()),
|
||||
@@ -66,9 +107,15 @@ pub fn parse<T: MarkdownTheme, H: CodeHighlighter>(
|
||||
}
|
||||
}
|
||||
}
|
||||
close_block(
|
||||
current_block_start.take(),
|
||||
&mut current_block_source,
|
||||
&mut code_blocks,
|
||||
&lines,
|
||||
);
|
||||
flush_table(&mut table_buffer, &mut lines, theme);
|
||||
|
||||
lines
|
||||
ParsedMarkdown { lines, code_blocks }
|
||||
}
|
||||
|
||||
pub fn preprocess_markdown(md: &str) -> String {
|
||||
@@ -300,28 +347,39 @@ mod tests {
|
||||
#[test]
|
||||
fn test_parse_headings() {
|
||||
let md = "# H1\n## H2\n### H3";
|
||||
let lines = parse(md, &DefaultTheme, &NoHighlight);
|
||||
assert_eq!(lines.len(), 3);
|
||||
let parsed = parse(md, &DefaultTheme, &NoHighlight);
|
||||
assert_eq!(parsed.lines.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_code_block() {
|
||||
let md = "```\ncode line\n```";
|
||||
let lines = parse(md, &DefaultTheme, &NoHighlight);
|
||||
assert!(!lines.is_empty());
|
||||
let parsed = parse(md, &DefaultTheme, &NoHighlight);
|
||||
assert!(!parsed.lines.is_empty());
|
||||
assert_eq!(parsed.code_blocks.len(), 1);
|
||||
assert_eq!(parsed.code_blocks[0].source, "code line");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_table() {
|
||||
let md = "| A | B |\n|---|---|\n| 1 | 2 |";
|
||||
let lines = parse(md, &DefaultTheme, &NoHighlight);
|
||||
assert_eq!(lines.len(), 2);
|
||||
let parsed = parse(md, &DefaultTheme, &NoHighlight);
|
||||
assert_eq!(parsed.lines.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_theme_works() {
|
||||
let md = "Hello **world**";
|
||||
let lines = parse(md, &DefaultTheme, &NoHighlight);
|
||||
assert_eq!(lines.len(), 1);
|
||||
let parsed = parse(md, &DefaultTheme, &NoHighlight);
|
||||
assert_eq!(parsed.lines.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiple_code_blocks() {
|
||||
let md = "text\n```\nfirst\n```\nmore text\n```\nsecond line 1\nsecond line 2\n```";
|
||||
let parsed = parse(md, &DefaultTheme, &NoHighlight);
|
||||
assert_eq!(parsed.code_blocks.len(), 2);
|
||||
assert_eq!(parsed.code_blocks[0].source, "first");
|
||||
assert_eq!(parsed.code_blocks[1].source, "second line 1\nsecond line 2");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user