Skip to main content

Crate ratatui_markdown

Crate ratatui_markdown 

Source
Expand description

§ratatui-markdown

A Rust library providing markdown rendering, collapsible JSON/TOML tree views, and a rich hybrid scroll system — all built on top of ratatui.

§Features

  • Markdown rendering — parse and render markdown to styled ratatui::text::Lines, with support for headings, lists, code blocks, blockquotes, tables, and inline formatting (bold, italic, inline code)
  • Collapsible trees — parse JSON or TOML into interactive collapsible trees with expand/collapse, styled keys, and keyboard navigation
  • Hybrid scroll system — dual-mode scrolling: free-scroll for exploring content, engaged mode for navigating focusable items
  • MarkdownPreview widget — unified widget combining markdown rendering, tree views, and action items into a single scrollable view
  • RichTextTheme — fully themeable via a trait: 15+ color slots for text, borders, JSON values, popups, and more
  • CJK-aware text wrapping — correct width calculation for CJK characters via unicode-width
  • TOML frontmatter support — optionally strip +++-delimited TOML frontmatter

§Feature Flags

All features are enabled by default. Disable default features to enable only what you need:

[dependencies]
ratatui-markdown = { version = "0.2", default-features = false, features = ["markdown"] }
FeatureRequiresDescription
markdownMarkdown parser and renderer
imageimage crateImage resolution via ImageResolver
scrollHybridScrollView, scrollable lists
treescroll, serde_json, tomlCollapsible JSON/TOML tree
previewmarkdown, scroll, treeMarkdownPreview unified widget
mermaidmarkdownMermaid diagram rendering
viewermarkdown, scrollMarkdownViewer widget

§Quick Start

use ratatui_markdown::preview::MarkdownPreview;

let mut preview = MarkdownPreview::new();
preview.set_content("# Hello, world!\n\nThis is a paragraph.");
// render and handle input in your ratatui app loop

§Markdown Rendering

use ratatui_markdown::markdown::MarkdownRenderer;

let renderer = MarkdownRenderer::new(80);
let blocks = renderer.parse("# Title\n\nParagraph with **bold** text.");
let lines = renderer.render(&blocks, &my_theme);

§Custom Rendering with Hooks

use ratatui_markdown::markdown::{MarkdownRenderer, RenderHooks};
use ratatui::text::Line;

struct MyHooks;

impl RenderHooks for MyHooks {
    fn heading1(&self, text: &str) -> Option<Line<'static>> {
        Some(Line::raw(format!(">>> {}", text)))
    }
}

let renderer = MarkdownRenderer::new(80)
    .with_render_hooks(Box::new(MyHooks));

§Collapsible Trees

use ratatui_markdown::tree::CollapsibleTree;

let mut tree = CollapsibleTree::from_json_str(
    r#"{"key": "value", "nested": {"a": 1}}"#
).unwrap();
let lines = tree.render_lines(80, &my_theme);
let items = tree.build_focusable_items();
tree.toggle("nested");

§Scroll System

use ratatui_markdown::scroll::HybridScrollView;

let mut scroll = HybridScrollView::new()
    .with_cursor_indicator(true);
// set content, handle input, render

§Theming

use ratatui::style::Color;
use ratatui_markdown::theme::{Generation, RichTextTheme};

struct MyTheme;

impl RichTextTheme for MyTheme {
    fn generation(&self) -> Generation { Generation(1) }
    fn get_text_color(&self) -> Color { Color::White }
    fn get_muted_text_color(&self) -> Color { Color::Gray }
    fn get_primary_color(&self) -> Color { Color::Cyan }
    fn get_secondary_color(&self) -> Color { Color::Blue }
    fn get_info_color(&self) -> Color { Color::LightBlue }
    fn get_background_color(&self) -> Color { Color::Black }
    fn get_border_color(&self) -> Color { Color::DarkGray }
    fn get_focused_border_color(&self) -> Color { Color::White }
    fn get_popup_selected_background(&self) -> Color { Color::DarkGray }
    fn get_popup_selected_text_color(&self) -> Color { Color::White }
    fn get_json_key_color(&self) -> Color { Color::LightCyan }
    fn get_json_string_color(&self) -> Color { Color::Green }
    fn get_json_number_color(&self) -> Color { Color::Yellow }
    fn get_json_bool_color(&self) -> Color { Color::Magenta }
    fn get_json_null_color(&self) -> Color { Color::DarkGray }
    fn get_accent_yellow(&self) -> Color { Color::Yellow }
}

§Modules

ModuleFeatureDescription
markdownmarkdownParse and render markdown text
scrollscrollHybrid scroll system with focusable items
treetreeCollapsible JSON/TOML tree view
previewpreviewUnified MarkdownPreview widget
mermaidmermaidMermaid diagram rendering
viewerviewerMarkdownViewer widget
themealwaysRichTextTheme trait for theming
constantsalwaysBox-drawing chars, tree connectors, arrows

Re-exports§

pub use theme::DefaultTheme;Deprecated
pub use theme::CodeColors;
pub use theme::RichTextTheme;
pub use theme::ThemeBuilder;
pub use theme::ThemeConfig;

Modules§

constants
markdown
mermaid
preview
scroll
text_input
theme
tree
viewer