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 🙏

© 2024 – Pkg Stats / Ryan Hefner

md-toc-cli

v3.1.1

Published

Automatically insert or update a clickable table of contents (TOC) into your Markdown documents based on its headings using CLI or JavaScript module.

Downloads

72

Readme

Table of contents generator for Markdown

npm version npm downloads GitHub licence

Introduction

Automatically insert or update a clickable table of contents (TOC) into your Markdown documents based on its headings using CLI or JavaScript module with the perfect support for README.md files on GitHub.

md-toc-cli creates table of contents from level 2-6 headings (e.g., ## Heading, ### Heading etc.) and inserts it after level 1 heading or at the beginning of the file. Zero or one level 1 heading is expected (e.g., # Heading).

HTML anchor elements are added to level 1-6 headings to make table of content items clickable, e.g. <a id="..."></a>.

Anchor at level 1 heading allows creating Back to top links as [Back to top](#0).

md-toc-cli usage

Usage

md-toc-cli is available as npm package and Docker image.

Docker

When running md-toc-cli using Docker, mount the directory containing the Markdown files as a volume.

  1. Insert table of contents to the README.md file in the current directory

    docker run --rm -v .:/markdown eugenekhyst/md-toc-cli -i README.md
  2. Read the manual

    docker run --rm -v .:/markdown eugenekhyst/md-toc-cli --help
    md-toc-cli [file]
    
    Automatically insert or update a clickable table of contents (TOC) into your
    Markdown documents based on its headings (levels 2-6).
    
    Positionals:
      file  Markdown file for inserting or updating table of contents in
                                                     [string] [default: "README.md"]
    
    Options:
          --version           Show version number                          [boolean]
          --help              Show help                                    [boolean]
      -i, --in-place          Edit file in place          [boolean] [default: false]
      -s, --suffix            The extension of a backup copy. If no extension is
                              supplied, the original file is overwritten without
                              making a backup. This option implies -i.      [string]
      -t, --tab-width         The number of spaces per indentation-level
                                                               [number] [default: 2]
      -l, --list-item-symbol  Symbol used in front of line items to create an
                              unordered list
                                    [string] [choices: "-", "*", "+"] [default: "-"]
      -n, --no-attribution    Do not add an attribution "Table of contents is made
                              with ..."                   [boolean] [default: false]

npm

When running md-toc-cli using Node.js, install the package globally for convenience.

  1. Make sure Node.js 18.x LTS or newer is installed.

  2. Install md-toc-cli as a global package

    npm i -g md-toc-cli
  3. Insert table of contents to the README.md file in the current directory

    md-toc-cli -i README.md
  4. Read the manual

    $ md-toc-cli --help

Programmatic

md-toc-cli can be used as a library in JavaScript and TypeScript projects.

  1. Make sure Node.js 18 or newer is installed.
  2. Install md-toc-cli
    npm i md-toc-cli
  3. Import md-toc-cli functions
    import { insertOrUpdateToc, insertOrUpdateTocInFile } from 'md-toc-cli';
  4. Programmatically insert or update the table of contents in a Markdown string or file
    insertOrUpdateToc(markdownContent, {
      tabWidth: 2,
      listItemSymbol: '-',
      noAttribution: false,
    });
    await insertOrUpdateTocInFile('README.md', {
      inPlace: false,
      suffix: 'orig',
      tabWidth: 2,
      listItemSymbol: '-',
      noAttribution: false,
    });

Example

  1. Create file test.md as follows

    # Heading 1
    
    ## Heading 2a
    
    ### Heading 3aa
    
    #### Heading 4a
    
    ##### Heading 5a
    
    ###### Heading 6a
    
    ### Heading 3ab
    
    ## Heading 2b
    
    ### Heading 3b
    
    #### Heading 4b
    
    ## Heading 2c
    
    ### Heading 3c
  2. Insert table of contents to test.md and backup the original file

    md-toc-cli test.md -i -s 'orig'
  3. A backup test.md.orig is created for original file test.md.

  4. A clickable table of contents is inserted into test.md

    # <a id="0"></a>Heading 1
    
    <!-- Table of contents is made with https://github.com/eugene-khyst/md-toc-cli -->
    
    ## <a id="1"></a>Heading 2a
    
    ### <a id="1-1"></a>Heading 3aa
    
    #### <a id="1-1-1"></a>Heading 4a
    
    ##### <a id="1-1-1-1"></a>Heading 5a
    
    ###### <a id="1-1-1-1-1"></a>Heading 6a
    
    ### <a id="1-2"></a>Heading 3ab
    
    ## <a id="2"></a>Heading 2b
    
    ### <a id="2-1"></a>Heading 3b
    
    #### <a id="2-1-1"></a>Heading 4b
    
    ## <a id="3"></a>Heading 2c
    
    ### <a id="3-1"></a>Heading 3c
  5. Make any change to the level 2-6 headings (e.g. delete level 5-6 headings and rename level 3 headings).

  6. Update the table of contents in test.md

    md-toc-cli -i test.md
  7. The table of contents in test.md is updated according to the level 2-6 headings.