markdown2json 0.1.1

Reads a markdown file or directory of markdown documents and emits a structured JSON
Documentation

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.

cargo build --release

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:

cargo run -- <input1> [input2 ...] [--depth N]
  • Each input can be a .md/.markdown file or a directory containing markdown files.
  • The optional --depth N/-d N flag limits how deep directory traversal should recurse. When omitted, traversal is unbounded.
  • Flags must appear after all inputs; a leading --depth or 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 is null for synthetic preamble sections created before the first heading.

Examples

Index a single file:

cargo run -- README.md

Index multiple inputs and emit a single combined JSON document:

cargo run -- docs notes/guide.md blog_posts

Index a directory but only descend two levels deep (note the depth flag trails the inputs):

cargo run -- docs --depth 2

Redirect the JSON output to a file for later processing:

cargo run -- notes > index.json

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):

[dependencies]
markdown2json = { git = "https://github.com/jslambda/markdown-indexer" }

Then call index_markdown to receive the parsed sections and adapt them to your needs:

use markdown2json::index_markdown;

fn main() -> Result<(), markdown::message::Message> {
    let src = "# Title\n\nSome text with `inline` code.";
    let sections = index_markdown(src)?;

    for section in sections {
        println!("{} (level {})", section.title, section.level);
        println!("Paragraphs: {}", section.body_text.len());
        println!(
            "Code blocks: {}",
            section.code_blocks.iter().map(|cb| &cb.value).count()
        );
    }

    Ok(())
}

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.