A CLI utility to flatten directory structures, with perfect GitHub Flavored Markdown compatibility including explicit HTML anchors for maximum portability.
Watch the YouTube video for an example of why you might need to flatten project files into a single document for AI discussions and code reviews.
Requires Bun runtime (v1.1+).
npm install -g flatten-toolYou can run flatten-tool directly without installing:
npx flatten-tool [args]
bunx flatten-tool [args]Clone the repository and install dependencies:
git clone https://github.com/MBanucu/flatten-tool.git
cd flatten-tool
bun installRun directly with Bun:
bun run index.ts [args]By default, the tool merges all file contents into a single Markdown file, starting with a project file tree for navigation, followed by each file's content under a header with its full relative path, in a code block with appropriate language highlighting based on the file extension. The tree includes clickable links to file sections using GitHub-compatible anchors. Ignores and filters are applied as usual.
The <source> argument is optional and defaults to the current directory (.). The <target> argument is also optional and defaults to flattened.md (or flattened/ when using --directory).
--directory,-d: Flatten to individual files in a directory instead of merging to Markdown.--move,-m: Move files instead of copying (original files will be deleted).--overwrite,-o: Overwrite existing target files.--gitignore,-g: Respect.gitignorefiles (default: true). Use--no-gitignoreto disable.--help,-h: Show help.--version,-v: Show version.
Flatten current directory to flattened.md:
flatten-toolFlatten a specific directory to flattened.md:
flatten-tool /path/to/sourceFlatten current directory to a custom Markdown file:
flatten-tool output.mdFlatten a specific directory to a custom Markdown file:
flatten-tool /path/to/source output.mdFlatten to individual files in a directory:
flatten-tool --directoryThis creates a flattened/ directory with flattened files.
Flatten a specific directory to a custom output directory:
flatten-tool /path/to/source output-dir --directoryMove files instead of copying:
flatten-tool --moveOverwrite existing files:
flatten-tool --overwriteCombine options:
flatten-tool /path/to/source output.md --move --overwriteRun all tests:
bun testRun tests in watch mode:
bun test --watchRun a specific test:
bun test -t "flattens a simple nested directory"This project uses Bun for runtime, TypeScript for type safety, and follows the guidelines in AGENTS.md for coding standards.
- Added explicit HTML anchors (
<a id="..."></a>) before all headings for guaranteed compatibility across platforms that don't auto-generate heading IDs, maximum control over anchor names, and stable links.
- Enhanced safety: Automatically exclude common binary file extensions (PNG, JPEG, PDF, archives, executables, etc.) when merging to Markdown to prevent corruption.
- Added
--ignoreCLI option: Allow additional glob patterns to ignore (e.g.,*.log,temp/**). - Minor clean-ups: Improved variable naming and code consistency.
- Added YouTube video link in README demonstrating use case for AI discussions.
- Updated AGENTS.md with revised coding guidelines.
- Added '..' links in subdirectory file trees for navigation to parent directories.
- Added instructions for running flatten-tool directly with npx and bunx.
- Perfect GitHub compatibility: anchors now exactly match GitHub Flavored Markdown auto-generation using github-slugger.
- Cleaner directory headers: removed trailing
/for better readability. - Precomputed anchors: ensures no mismatches even with slug collisions.
- Removed unused treeify dependency.
- Improved navigation: project file tree is now a clickable nested Markdown list with links to each file's content section using standard markdown anchors.
- Simplified file headers: removed custom anchors from section headers.
- Added project file tree to the beginning of merged Markdown output for better navigation.
- Fixed GIF exclusion pattern to work recursively in subdirectories.
- Excluded GIF files by default when merging to Markdown to prevent binary content corruption.
- Added demo.gif to docs/ and linked in README.md.
- Added
setMaxListeners(0)on WriteStream in Markdown merging to silence listener warnings when processing many files.
- Fixed memory leak warnings in Markdown merging by refactoring to use
pipelineandfinishedfromnode:stream/promises.
- Implemented streaming for Markdown merging to improve memory efficiency for large files/directories.
- Updated documentation and coding guidelines.
- Made source argument optional (defaults to current directory).
- Improved CLI defaults and options.
- Initial release with Markdown merging and directory flattening capabilities.
This project was created using bun init in bun v1.3.8. Bun is a fast all-in-one JavaScript runtime.

