tree2md-0.6.0 is not a library.
tree2md
Like the tree command, but outputs in Markdown.
Scans directories and prints a Markdown-formatted tree. Optionally embed file contents as syntax-highlighted code blocks.
Table of Contents
- tree2md
Quick Start
# Install
# Show directory tree (no file contents)
# Include file contents as syntax-highlighted code blocks
Clipboard helpers:
# macOS
|
# Linux
|
# Windows
|
Why tree2md vs. tree
| Capability | tree |
tree2md |
|---|---|---|
| Output Markdown | ✖︎ | ✔ |
| Embed file contents (code blocks) | ✖︎ | ✔ |
| Syntax highlighting hints | ✖︎ | ✔ |
Respect .gitignore |
◯* | ✔ |
| Filter by extension / glob | ◯ | ✔ |
| Drive via stdin (authoritative/merge) | △ | ✔ |
| Flat output for file collections | ✖︎ | ✔ |
| Truncate by bytes / lines | ✖︎ | ✔ |
Security boundary (--restrict-root) |
✖︎ | ✔ |
| Fast, single-binary (Rust) | — | ✔ |
* depending on platform/flags; tree2md provides explicit --respect-gitignore.
Features
- Markdown-formatted directory trees
- Optional file contents as fenced code blocks (with language hints)
- Extension filters and glob patterns
- Honors
.gitignore(opt-in) - Truncate large files by bytes or lines
- Hidden files/dirs support
- Read paths from stdin (newline or NUL-delimited)
- Flat output for discrete file sets
- Security guardrail with
--restrict-root - Fast & efficient (Rust)
Installation
From crates.io
From source
# Binary at: ./target/release/tree2md
Pre-built binaries
Download from the releases page.
Available for:
- Linux (x86_64)
- macOS (Apple Silicon)
- Windows (x86_64)
Usage
Common recipes
# 1) Quick overview without contents
# 2) Generate README-style docs with contents
# 3) Filter by extensions
# 4) Find with glob patterns (repeatable)
# 5) Include hidden files and respect .gitignore
# 6) Truncate embedded contents (lines or bytes)
# 7) Combine filters + contents + truncation
Git-friendly
# Only Git-tracked TypeScript files
|
# Recently changed files
|
All options (cheat sheet)
Basic
-c, --contents— include file contents as code blocks-t, --truncate <N>— truncate file content to first N bytes--max-lines <N>— limit file content to first N lines-e, --include-ext <EXTS>— comma-separated list (e.g..go,.py)-f, --find <PATTERN>— glob pattern (repeatable), e.g."src/**/*.rs"-a, --all— include hidden files/dirs--respect-gitignore— honor.gitignorerules-h, --help/-V, --version
Stdin mode
--stdin— read newline-delimited paths from stdin--stdin0— read NUL-delimited paths from stdin--stdin-mode <authoritative|merge>— default: authoritative--keep-order— preserve stdin order (default is alphabetical)--base <DIR>— base directory to resolve relative stdin paths--restrict-root <DIR>— ensure all paths stay within this directory--expand-dirs— expand directories received via stdin--flat— render a flat list (no tree)
Display & paths
--display-path <relative|absolute|input>— default: relative (relative is default in v0.4.0+)--display-root <DIR>— custom root for relative display--strip-prefix <PREFIX>— remove prefix from display paths (repeatable)--show-root— print the display root at the top--no-root— omit the root node in tree mode (default in stdin mode)--root-label <LABEL>— custom label for the root (e.g.".","PROJECT_ROOT")
Stdin mode (precise control)
Use stdin when you already have an exact file list (CI pipelines, git ls-files, ripgrep, find).
# Authoritative: only use files from stdin
|
# Merge: combine stdin with a directory scan of `src/`
|
# NUL-delimited (handles spaces/newlines in paths)
|
# Expand directories that appear in stdin
|
# Keep stdin order (for prioritized docs)
|
# Enforce a project boundary (security)
|
Flat mode is great for discrete file collections:
|
Display & path controls
# Show absolute paths
# Custom display root and show it at the top
# Strip a common prefix from printed paths
|
Example output
- - - - - -
```rust
fn main() {
}
```
```rust
pub fn add(a: i32, b: i32) -> i32 {
}
```
Supported languages
Language detection for fenced code blocks (non-exhaustive):
- Rust (.rs), Go (.go), Python (.py)
- JavaScript (.js), TypeScript (.ts, .tsx)
- HTML (.html), CSS (.css, .scss, .sass)
- SQL (.sql), Shell (.sh)
- TOML (.toml), YAML (.yaml, .yml)
- JSON (.json), Markdown (.md)
Performance & security
- Designed to be fast and memory-efficient.
- Use
--truncate/--max-linesfor very large files. - Prefer
--respect-gitignoreto avoid noise. - Use
--restrict-rootin scripts/CI to prevent path escapes.
Build from source
Requirements:
- Rust 1.70 or later
- Cargo
# Build release
# Run tests
# Install locally
Contributing
Issues and PRs are welcome. Please include a clear description and, if possible, tests.
License
MIT License