vtcode_core/ui/
markdown.rs1use crate::config::loader::SyntaxHighlightingConfig;
2use crate::ui::theme::{self, ThemeStyles};
3use anstyle::Style;
4
5pub use vtcode_commons::ui_protocol::CodeBlockWeight;
6
7#[cfg(feature = "tui")]
9pub use vtcode_ui::tui::ui::markdown::{
10 HighlightedSegment, MarkdownLine, MarkdownSegment, RenderMarkdownOptions, highlight_code_to_ansi,
11 highlight_code_to_segments, highlight_line_for_diff,
12};
13
14#[cfg(not(feature = "tui"))]
16pub use vtcode_commons::ui_protocol::{HighlightedSegment, MarkdownLine, MarkdownSegment, RenderMarkdownOptions};
17
18#[cfg(not(feature = "tui"))]
20pub fn highlight_code_to_ansi(code: &str, _language: Option<&str>, _theme: &str) -> String {
21 code.to_string()
22}
23
24#[cfg(not(feature = "tui"))]
26pub fn highlight_code_to_segments(code: &str, _language: Option<&str>, _theme: &str) -> Vec<HighlightedSegment> {
27 vec![HighlightedSegment { style: Style::default(), text: code.to_string() }]
28}
29
30#[cfg(not(feature = "tui"))]
32pub fn highlight_line_for_diff(line: &str, _language: Option<&str>) -> Option<Vec<(Style, String)>> {
33 Some(vec![(Style::default(), line.to_string())])
34}
35
36pub fn render_markdown_to_lines(
40 source: &str,
41 base_style: Style,
42 theme_styles: &ThemeStyles,
43 highlight_config: Option<&SyntaxHighlightingConfig>,
44) -> Vec<MarkdownLine> {
45 render_markdown_to_lines_with_options(
46 source,
47 base_style,
48 theme_styles,
49 highlight_config,
50 RenderMarkdownOptions::default(),
51 )
52}
53
54#[cfg(feature = "tui")]
56pub fn render_markdown_to_lines_with_options(
57 source: &str,
58 base_style: Style,
59 theme_styles: &ThemeStyles,
60 highlight_config: Option<&SyntaxHighlightingConfig>,
61 render_options: RenderMarkdownOptions,
62) -> Vec<MarkdownLine> {
63 let tui_theme_styles = crate::ui::tui_compat::tui_theme_styles_from_core(theme_styles);
64 vtcode_ui::tui::ui::markdown::render_markdown_to_lines_with_options(
65 source,
66 base_style,
67 &tui_theme_styles,
68 highlight_config,
69 render_options,
70 )
71}
72
73#[cfg(not(feature = "tui"))]
75pub fn render_markdown_to_lines_with_options(
76 source: &str,
77 base_style: Style,
78 _theme_styles: &ThemeStyles,
79 _highlight_config: Option<&SyntaxHighlightingConfig>,
80 _render_options: RenderMarkdownOptions,
81) -> Vec<MarkdownLine> {
82 let mut lines: Vec<MarkdownLine> = source
83 .lines()
84 .map(|line| MarkdownLine {
85 segments: if line.is_empty() {
86 Vec::new()
87 } else {
88 vec![MarkdownSegment {
89 style: base_style,
90 text: line.to_string(),
91 link_target: None,
92 }]
93 },
94 line_background: None,
95 })
96 .collect();
97 if lines.is_empty() {
98 lines.push(MarkdownLine::default());
99 }
100 lines
101}
102
103pub fn render_markdown(source: &str) -> Vec<MarkdownLine> {
105 let styles = theme::active_styles();
106 render_markdown_to_lines(source, Style::default(), &styles, None)
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn facade_renders_markdown() {
115 let lines = render_markdown("# Heading");
116 assert!(!lines.is_empty());
117 }
118}