Skip to main content

Transform large markdown files into hierarchical folder structures for better navigation and AI-assisted editing

Project description

md-hierarchy

A CLI tool that splits markdown files into hierarchical folder structures based on heading levels, and can reconstruct the original markdown from the split pieces.

Features

  • Split markdown files into navigable folder hierarchies
  • Merge folder structures back into single markdown files
  • Preserves all markdown elements (code blocks, lists, tables, links, etc.)
  • Handles edge cases (duplicate headings, empty headings, skipped levels)
  • Round-trip compatible (split → merge produces equivalent content)
  • Dry-run mode to preview operations

Installation

# From PyPI
pip install md-hierarchy

# From source
pip install -e .

# With development dependencies
pip install -e ".[dev]"

Usage

Split Command

Split a markdown file into a hierarchical folder structure:

md-hierarchy split input.md output_dir --level 3

Options:

  • --level, -l: Heading level to extract as files (1-4, default: 3)
  • --overwrite: Overwrite output directory if it exists
  • --verbose, -v: Print detailed operation log
  • --dry-run: Show what would be done without writing files

Example:

# Split at level 3 (H3 headings become files)
md-hierarchy split proposal.md ./output --level 3

# Split with overwrite
md-hierarchy split proposal.md ./output --level 2 --overwrite

# Preview without creating files
md-hierarchy split proposal.md ./output --dry-run

Merge Command

Merge a folder structure back into a single markdown file:

md-hierarchy merge input_dir output.md

Options:

  • --verbose, -v: Print detailed operation log

Example:

# Merge folder structure
md-hierarchy merge ./output merged.md

# Merge with verbose output
md-hierarchy merge ./split-docs final.md --verbose

Output Structure

When splitting at level 3, the tool creates this structure:

output-dir/
├── 01-Introduction/
│   ├── README.md                    # Content directly under H1
│   ├── 01-Background/
│   │   ├── README.md                # Content directly under H2
│   │   ├── 01-Problem-Statement.md  # H3 section
│   │   └── 02-Research-Gap.md       # H3 section
│   └── 02-Objectives/
│       └── 01-Primary-Goals.md
└── 02-Methodology/
    └── README.md

File Naming Convention

  • Folders: NN-Sanitized-Title/ (e.g., 01-Introduction/)
  • Files: NN-Sanitized-Title.md or README.md
  • Numbers are zero-padded (01, 02, ..., 99)
  • Special characters (/ \ : * ? " < > |) are removed
  • Spaces are replaced with hyphens
  • Maximum length: 50 characters

Edge Cases Handled

  1. Empty headingsUntitled-Section-N
  2. Duplicate titles → Append -2, -3, etc.
  3. Skipped levels (H1 → H3) → Insert 00-Content/ folder
  4. Content before first heading00-Frontmatter/README.md
  5. Heading attributes (e.g., {#id .class}) → Preserved in content

Round-Trip Compatibility

The tool is designed for round-trip operations:

# Split
md-hierarchy split original.md ./split --level 3

# Merge
md-hierarchy merge ./split reconstructed.md

# Content should be equivalent
diff original.md reconstructed.md

Development

Setup

# Create virtual environment
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows

# Install in development mode
pip install -e ".[dev]"

Run Tests

pytest

Run Tests with Coverage

pytest --cov=md_hierarchy --cov-report=html

Requirements

  • Python 3.8+
  • Dependencies:
    • markdown-it-py - Markdown parsing
    • click - CLI framework

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

md_hierarchy-0.1.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

md_hierarchy-0.1.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file md_hierarchy-0.1.0.tar.gz.

File metadata

  • Download URL: md_hierarchy-0.1.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for md_hierarchy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 45e74b1aa35ee9b2947223c98fa886591a0dcbfd5ef9d2c691df3ae172e8ffac
MD5 e26c1142a02b533a15bf25be2e7e152f
BLAKE2b-256 93ec0012dac8211e6f06fd83b3d6d0f68374a533c5759f974bd1fe48df7914a3

See more details on using hashes here.

File details

Details for the file md_hierarchy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: md_hierarchy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for md_hierarchy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f57f29ce54ceab4feeee08b2e09c6ddd02cacbb1e02cb637450b7f6ca974371
MD5 591a6c15ee13faa65a8345939689fe2e
BLAKE2b-256 a94335cba82c853c67c249f818fced5b4651b8c924bce464e037d8376832f6cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page