Skip to main content

vtcode_core/ui/
markdown.rs

1use crate::config::loader::SyntaxHighlightingConfig;
2use crate::ui::theme::{self, ThemeStyles};
3use anstyle::Style;
4
5pub use vtcode_commons::ui_protocol::CodeBlockWeight;
6
7// When TUI is enabled, use vtcode-ui's richer types (with internal methods).
8#[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// When headless, use the plain data types from commons.
15#[cfg(not(feature = "tui"))]
16pub use vtcode_commons::ui_protocol::{HighlightedSegment, MarkdownLine, MarkdownSegment, RenderMarkdownOptions};
17
18/// Highlight a code block to an ANSI-escaped string (headless passthrough).
19#[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/// Highlight a code block into styled segments (headless passthrough).
25#[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/// Highlight a single line for diff display (headless passthrough).
31#[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
36// ── Markdown rendering ──────────────────────────────────────────────────────
37
38/// Render markdown source into styled lines using default rendering options.
39pub 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/// Render markdown source into styled lines with custom rendering options (TUI build).
55#[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/// Render markdown source into styled lines (headless passthrough, no syntax highlighting).
74#[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        })
95        .collect();
96    if lines.is_empty() {
97        lines.push(MarkdownLine::default());
98    }
99    lines
100}
101
102/// Render markdown source using the currently active theme styles.
103pub fn render_markdown(source: &str) -> Vec<MarkdownLine> {
104    let styles = theme::active_styles();
105    render_markdown_to_lines(source, Style::default(), &styles, None)
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn facade_renders_markdown() {
114        let lines = render_markdown("# Heading");
115        assert!(!lines.is_empty());
116    }
117}