npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

tocgenie

v0.1.1

Published

A CLI tool that generates a table of contents for markdown files in a git repository

Readme

tocgenie

A fast CLI tool that generates a table of contents for markdown files — with anchor links compatible with GitHub, GitLab, Bitbucket, or any custom platform. Works out of the box with any git repository.

Generated with tocgenie


Installation

Clone and build:

git clone https://github.com/smarkstrife/tocgenie.git
cd tocgenie
npm install
npm run build

Install globally (run tocgenie from anywhere):

npm install -g .

Use as a dev dependency in another project:

npm install --save-dev /path/to/tocgenie

Quick Start

# Generate TOC for a single file
tocgenie README.md

# Insert TOC directly into a file (add markers first — see below)
tocgenie --insert README.md

# Generate TOC for all tracked .md files in a git repo
tocgenie

Real World Examples

Single File

Generate a TOC and print to stdout:

tocgenie README.md

Output:

- [Installation](#installation)
- [Usage](#usage)
  - [Options](#options)
  - [Examples](#examples)
- [Contributing](#contributing)

Save the TOC to a separate file:

tocgenie -O toc.md README.md

Real file example — given a file story.md with this structure:

# The Last Compiler
## Prologue
## Chapter 1: The Bug
### The Discovery
### The Aftermath
## Chapter 2: The Fix
## Epilogue

Running:

tocgenie --no-git story.md

Produces:

- [The Last Compiler](#the-last-compiler)
  - [Prologue](#prologue)
  - [Chapter 1: The Bug](#chapter-1-the-bug)
    - [The Discovery](#the-discovery)
    - [The Aftermath](#the-aftermath)
  - [Chapter 2: The Fix](#chapter-2-the-fix)
  - [Epilogue](#epilogue)

Skip the top-level heading and use an ordered list:

tocgenie --no-git --min-level 2 --ordered story.md

Output:

1. [Prologue](#prologue)
1. [Chapter 1: The Bug](#chapter-1-the-bug)
   1. [The Discovery](#the-discovery)
   1. [The Aftermath](#the-aftermath)
1. [Chapter 2: The Fix](#chapter-2-the-fix)
1. [Epilogue](#epilogue)

Entire Repository

Scan all git-tracked .md files in the current repo:

tocgenie

Include files not yet committed to git:

tocgenie --include-untracked

Not in a git repo? Walk the filesystem instead:

tocgenie --no-git docs/

In-Place Insertion

This is the most common workflow. Add two marker comments anywhere in your markdown file:

## Table of Contents

<!-- toc -->
<!-- /toc -->

## Introduction

Then run:

tocgenie --insert README.md

tocgenie replaces everything between the markers with the generated TOC. Re-running the command is safe — it always replaces the existing content, never duplicates it.

Custom marker:

tocgenie --insert --marker "<!-- contents -->" README.md

CI / Automation

Keep your TOC up to date automatically. Add tocgenie as a dev dependency and a script in package.json:

{
  "scripts": {
    "toc": "tocgenie --insert README.md"
  },
  "devDependencies": {
    "tocgenie": "file:../tocgenie"
  }
}

Then run it as part of your workflow:

npm run toc

Or in a GitHub Actions workflow:

- name: Update TOC
  run: npx tocgenie --insert README.md

Using in Another Project

Option 1 — Global install (recommended for personal use):

npm install -g /path/to/tocgenie
tocgenie --insert README.md   # works in any directory

Option 2 — Local dev dependency:

npm install --save-dev /path/to/tocgenie

Add to package.json:

"scripts": {
  "toc": "tocgenie --insert README.md"
}

Option 3 — One-off via node directly:

node /path/to/tocgenie/dist/cli.js --no-git README.md

Options

| Flag | Short | Default | Description | |---|---|---|---| | --flavor <flavor> | -f | github | Anchor flavor: github, gitlab, bitbucket, generic | | --min-level <n> | | 1 | Minimum heading level to include | | --max-level <n> | | 6 | Maximum heading level to include | | --ordered | -o | false | Use ordered (1.) list markers | | --no-link | | false | Omit anchor links (plain text outline) | | --indent <n> | | 2 | Spaces per indent level | | --output <file> | -O | stdout | Write TOC to a file | | --insert | -i | false | Insert/replace TOC in-place between markers | | --marker <marker> | | <!-- toc --> | Marker used for in-place insertion | | --include-untracked | | false | Include untracked (but not gitignored) files | | --no-git | | false | Walk the filesystem instead of using git | | --anchor-prefix <prefix> | | "" | Custom prefix for generic flavor | | --anchor-strip-regex <regex> | | none | Custom strip regex for generic flavor | | [files...] | | all .md | Explicit files or globs to process |


Platform Flavors

Different platforms generate anchor slugs differently. Use --flavor to match your target platform.

| Flavor | Example heading | Generated anchor | |---|---|---| | github (default) | ## My Heading | #my-heading | | gitlab | ## My Heading | #my-heading | | bitbucket | ## My Heading | #markdown-header-my-heading | | generic | configurable via --anchor-prefix / --anchor-strip-regex | custom |

Examples:

tocgenie -f gitlab README.md
tocgenie -f bitbucket README.md
tocgenie -f generic --anchor-prefix "section-" README.md

Development

npm test              # run all tests
npm run test:watch    # watch mode
npm run build         # compile TypeScript
npm run dev -- --no-git README.md   # run without building

Project Structure

src/
├── cli.ts        Entry point and argument parsing
├── parser.ts     Markdown heading extraction
├── anchor.ts     Anchor/slug generation per platform flavor
├── toc.ts        TOC assembly and in-place insertion
├── git.ts        Git-aware and filesystem file discovery
└── types.ts      Shared type definitions
tests/
├── parser.test.ts
├── anchor.test.ts
├── toc.test.ts
└── fixtures/     Sample .md files used in tests

Author: Shikhar Srivastava