$ show henriklovhaug/md-tui
MD-TUI is a Rust-based terminal markdown viewer (TUI) that renders `.md` files with full keyboard navigation, syntax highlighting via tree-sitter, inline images, foldable `<details>` blocks, mermaid diagrams, table rendering, search, and link following (internal/external/wiki). It can be used as a standalone binary (`mdt`) or as a library. The architecture is monolithic but modular: a custom PEG parser (`src/md.pest`) converts markdown into a component tree (`ComponentRoot`) composed of `TextComponent` and `ImageComponent` nodes; these are rendered to the terminal using `ratatui`. Configuration is loaded from `~/.config/mdt/config.toml` or environment variables. The repository demonstrates strong Rust craftsmanship with well-separated modules, optional feature flags (`tree-sitter`, `network`), thread-local caching for syntax highlighting, and a comprehensive test suite covering parsing edge cases, search, and rendering behavior.
The LLM identified 3 files worth highlighting for reuse:
Provides a `highlight_code()` function that uses tree-sitter to tokenize and highlight source code for 20+ languages (bash, C, Python, Rust, etc.). Results are cached per-language in a thread-local `RefCell<HashMap>`, avoiding repeated `HighlightConfiguration` construction. The module exports a static `HIGHLIGHT_NAMES` array and `DEFAULT_COLOR_MAP` that can be partially overridden via config. This is a self-contained, reusable syntax highlighting service that can be extracted into any Rust TUI or CLI project needing code highlighting.
Implements a lightweight file-position cache that saves and loads the last-viewed source line for a document, keyed by a stable 64-bit FNV-1a hash of the canonical path. Entries optionally expire after a configurable TTL (default 60 minutes). The cache is stored in the OS cache directory (`~/.cache/mdt/positions/`). This is a generic, reusable persistence utility for restoring scroll positions or bookmarks in any application.
Contains the `word_wrapping()` function, a pure Rust implementation that splits a sequence of `Word` tokens into lines respecting a maximum visible width (using `unicode-width`). It supports optional hyphenation with a trailing dash for long words, and handles words wider than the line width. Though part of the text rendering pipeline, the wrapping logic is isolated and could be reused in any project needing terminal-width-aware text layout.