Module markdown_widget

Module markdown_widget 

Source
Expand description

Markdown rendering widget for ratatui applications.

Provides a feature-rich markdown viewer with optional extensions:

  • Table of contents (TOC)
  • Syntax highlighting
  • Text selection
  • Click-to-highlight line selection

§Mouse Capture Requirement

For mouse interactions (click, drag, hover) to work, you must enable mouse capture with crossterm:

use crossterm::event::{EnableMouseCapture, DisableMouseCapture};

// On startup:
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;

// On cleanup:
execute!(stdout, LeaveAlternateScreen, DisableMouseCapture)?;

Without EnableMouseCapture, scroll wheel may still work (terminal-dependent), but click events will not be received.

use ratatui_toolkit::{MarkdownWidget, MarkdownState};

// Create unified state
let mut state = MarkdownState::default();
state.source.set_content("# Hello World\n\nWelcome!");

// Create widget from state
let content = state.content().to_string();
let widget = MarkdownWidget::from_state(&content, &mut state)
    .show_toc(true)
    .show_statusline(true)
    .show_scrollbar(true);

§Example (Individual State Modules)

use ratatui_toolkit::markdown_widget::{MarkdownWidget, state::*};

// Create state modules
let mut scroll = ScrollState::default();
let mut source = SourceState::default();
let mut cache = CacheState::default();
let display = DisplaySettings::default();
let mut collapse = CollapseState::default();
let mut expandable = ExpandableState::default();
let mut git_stats = GitStatsState::default();
let mut vim = VimState::default();
let mut selection = SelectionState::default();
let mut double_click = DoubleClickState::default();

// Create widget with individual state modules
let widget = MarkdownWidget::new(
    content,
    &mut scroll,
    &mut source,
    &mut cache,
    &display,
    &mut collapse,
    &mut expandable,
    &mut git_stats,
    &mut vim,
    &mut selection,
    &mut double_click,
)
.show_toc(true)
.show_statusline(true)
.show_scrollbar(true);

Re-exports§

pub use foundation::elements::CheckboxState;
pub use foundation::elements::CodeBlockBorderKind;
pub use foundation::elements::CodeBlockColors;
pub use foundation::elements::CodeBlockTheme;
pub use foundation::elements::ColumnAlignment;
pub use foundation::elements::ElementKind;
pub use foundation::elements::MarkdownElement;
pub use foundation::elements::TableBorderKind;
pub use foundation::elements::TextSegment;
pub use foundation::elements::BLOCKQUOTE_MARKER;
pub use foundation::elements::BULLET_MARKERS;
pub use foundation::elements::CHECKBOX_CHECKED;
pub use foundation::elements::CHECKBOX_TODO;
pub use foundation::elements::CHECKBOX_UNCHECKED;
pub use foundation::elements::HEADING_ICONS;
pub use foundation::elements::HORIZONTAL_RULE_CHAR;
pub use foundation::elements::methods::render as render_element;
pub use foundation::elements::methods::render as render_element;
pub use foundation::elements::methods::render_with_options as render_element_with_options;
pub use foundation::elements::methods::RenderOptions;
pub use foundation::parser::render_markdown_to_elements;
pub use foundation::source::MarkdownSource;
pub use foundation::events::MarkdownDoubleClickEvent;
pub use foundation::events::MarkdownEvent;
pub use foundation::types::GitStats;
pub use foundation::types::SelectionPos;
pub use foundation::functions::render_markdown;
pub use foundation::functions::render_markdown_with_style;
pub use widget::enums::MarkdownWidgetMode;
pub use widget::MarkdownWidget;
pub use state::CacheState;
pub use state::CollapseState;
pub use state::DisplaySettings;
pub use state::DoubleClickState;
pub use state::ExpandableEntry;
pub use state::ExpandableState;
pub use state::GitStatsState;
pub use state::MarkdownState;
pub use state::ParsedCache;
pub use state::RenderCache;
pub use state::ScrollState;
pub use state::SelectionState;
pub use state::SourceState;
pub use state::TocEntry;
pub use state::TocState;
pub use state::VimState;
pub use extensions::toc::Toc;
pub use extensions::toc::TocConfig;
pub use extensions::theme::get_effective_theme_variant;
pub use extensions::theme::load_theme_from_json;
pub use extensions::theme::palettes;
pub use extensions::theme::ColorMapping;
pub use extensions::theme::ColorPalette;
pub use extensions::theme::MarkdownStyle;
pub use extensions::theme::MarkdownTheme;
pub use extensions::theme::SyntaxHighlighter;
pub use extensions::theme::SyntaxThemeVariant;
pub use extensions::theme::ThemeVariant;
pub use extensions::selection::handlers::handle_mouse_event;
pub use extensions::selection::handlers::handle_mouse_event_with_double_click;
pub use extensions::selection::helpers::handle_click;
pub use extensions::selection::helpers::should_render_line;

Modules§

extensions
Extensions for the markdown widget.
foundation
Foundation module for markdown widget.
state
State management for markdown widget.
widget
A scrollable, interactive markdown widget.