Code Monkey home page Code Monkey logo

md-toc's Introduction

md-toc

Generate a GitHub compatible table of contents from headings in a markdown file.

Table of contents

Motivation

I wanted something for myself that does exactly what I need it to do and exposes all the levers that I need. Also, I specifically wanted a CLI that can run in a pre-commit hook (via husky + lint-staged) before prettier --write for markdown files so that I never have another piece of documentation without a table of contents!

Installation

npm install -D @zeusdeux/md-toc

Usage

Usage with lint-staged

Add the following line to the "lint-staged" config

"lint-staged": {
  "*.md": "md-toc --write",
}

It is recommended that you run prettier after md-toc. For example โ€”

"lint-staged": {
  "*.md": ["md-toc --write", "prettier --write"],
}

Usage as a CLI

md-toc

Usage: $0 [options] <file or stdin>

Options:
  -d, --debug        Print debug logs to stderr                 [boolean] [default: false]
  -v, --version      Show cli version                                            [boolean]
  -h, --help         Show help                                                   [boolean]
  -w, --write        Write changes to the input file            [boolean] [default: false]
  -a, --insertUnder  Heading to insert the table of contents under. Defaults to table of c
                     ontents|toc|table-of-contents                  [string] [default: ""]

Examples:
  md-toc --write Readme.md           Generate a table of contents from headings in Readme.
                                     md, insert them under a heading name "Table of Conten
                                     ts", "toc" or "table-of-contents" (all case insensiti
                                     ve) in Readme.md and write the file to disk.
  md-toc Readme.md                   Same as the --write option but the output is written
                                     to stdout and Readme.md is left as is.
  md-toc -a "Contents" -w Readme.md  Same as --write but the table of contents is inserted
                                      under the first heading named "Contents".

Debugging

If you run into problems and want to open an issue here, please run the cli with --debug and put the output in the issue. Do redact PII from the debug info before pasting into the issue.

Contributing

The package manager used is pnpm so please use that and don't commit a package-lock.json. Also, before starting development, run nvm use in the folder where you clone this repository.

Other than that, it is a fairly tiny codebase. Feel free to clone, edit and open a PR.

To test your changes locally, run pnpm link --global and assuming you are using nvm, this should make md-toc available as a command in your shell.

md-toc's People

Contributors

zeusdeux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jmw992

md-toc's Issues

Add jump2header

You may consider adding a flag that also enables jump2header links

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.