markdown2json
markdown2json is a small CLI that walks a markdown file or directory of markdown documents and emits a structured JSON index. It is useful for creating searchable corpora of notes, documentation, or blog posts without writing a custom crawler.
Installation
This project is built with Rust. To build and run it you need a recent Rust toolchain.
The compiled binary will be available at target/release/markdown2json.
Usage
From the repository root run the CLI with one or more markdown files or directories as positional arguments:
- Each input can be a
.md/.markdownfile or a directory containing markdown files. - The optional
--depth N/-d Nflag limits how deep directory traversal should recurse. When omitted, traversal is unbounded. - Flags must appear after all inputs; a leading
--depthor unknown flag results in an error. - Each path is validated before processing. Missing paths are listed and cause the command to exit with a non-zero status.
If any input path does not exist or an unknown flag is provided, the program prints an error message and exits with a non-zero status.
Output format
The command prints a JSON array to stdout. Each element represents a header section from one of the parsed markdown files:
file_path: Absolute or relative path to the source markdown file.header: The section title.text_blocks: An array of text paragraphs under the section.code_blocks: An array of code block objects extracted from the section. Each object has:value: code block text.start_line: 1-based line where that code block starts (inclusive).end_line: 1-based line where that code block ends (inclusive).
start_line: The 1-based line number where the section starts (inclusive).end_line: The 1-based line number where the section ends (inclusive).heading_line: The 1-based line number of the section heading. This isnullfor synthetic preamble sections created before the first heading.
Examples
Index a single file:
Index multiple inputs and emit a single combined JSON document:
Index a directory but only descend two levels deep (note the depth flag trails the inputs):
Redirect the JSON output to a file for later processing:
Library usage
The parser is also exposed as a library if you want to integrate it into your own Rust application. Add the crate to your Cargo.toml (use a path dependency when working from a local checkout, or swap in a published version if you depend on crates.io):
[]
= { = "https://github.com/jslambda/markdown-indexer" }
Then call index_markdown to receive the parsed sections and adapt them to your needs:
use index_markdown;
If you want the same JSON shape as the CLI, build your own JsonDocumentElement values from the returned sections (see src/main.rs for the conversion logic).
Development
The main CLI entrypoint lives in src/main.rs. The parser utilities are provided by the markdown2json crate dependencies declared in Cargo.toml. There are no additional runtime requirements.