Giter Club home page Giter Club logo

table-of-contents-preprocessor's Introduction

Table of Contents Generator

About

Say you've got a README.md file with lots of information, and you want to create a table of contents with reference links

This script will help you to do so. It extracts information of titles in a given file and inserts a formatted table of contents in the position, specified by @@TOC@@ line.

Table of Contents

The table of contents for the README.md was generated with this utility, so you can treat it as a demo

Installation

Easy with npm

npm install -g md-toc-filter

Usage

First, make sure you've got @@TOC@@ token on a separate line in your markdown-file. This is the place the table of contents will be inserted to.

Then, preprocess the file:

md-toc-filter README.md > NEW_README.md

You can also specify the maximal heading depth to be included in the TOC by passing the -d argument:

md-toc-filter -d 2 README.md > NEW_README.md

This will include headings up to depth 2 (##).

Pass the -a argument to use an alternative link generation mode, e.g. convert the heading "3.1.1. Foo" to "3-1-1-foo" instead of "311-foo". This argument will also strip all the non-Latin and non-numeric characters. This is useful for some Markdown renderers like the GitLab Markdown renderer.

Example

# Foo Great Project

Hey, this is my project

## Contents
@@TOC@@

## About

Some info about it

## Authors

My picture here

Will be transformed to

# Foo Great Project

Hey, this is my project

## Contents
- [Foo Great Project](#foo-great-project)
    - [Contents](#contents)
    - [About](#about)
    - [Authors](#authors)

## About

Some info about it

## Authors

My picture here

After the preprocessing you're free to modify the result as you wish. For example, it makes sense to remove reference to table of contents from table of contents

Limitations

The script doesn't support underlined titles like this

My Title
========

Use sharps instead

# My Title

table-of-contents-preprocessor's People

Contributors

aslushnikov avatar cpetrov avatar erictune avatar ipetropolsky avatar woodp avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

table-of-contents-preprocessor's Issues

The script does not support links to anchors of non-english titles

For example, the following README.md cannot be correctly handled by the script:


#  目录

@@TOC@@

# 标题一

## 子标题一

## 子标题二

# 标题二

## 子标题一

## 子标题二

The script will generate the following markdown:

#  目录

- [目录](#)
- [标题一](#)
    - [子标题一](#)
    - [子标题二](#)
- [标题二](#)
    - [子标题一](#)
    - [子标题二](#)

# 标题一

## 子标题一

## 子标题二

# 标题二

## 子标题一

## 子标题二

Note that the links to the anchors are lost.

Ignore hashes in quoted code

I often write docs with code snippets in them, using triple backticks.
That code often has lines that start with a hash (e.g. bash comments, python comments, c pragmas).

this tool would be more useful if it ignored hash characters that occured between triple backtick pairs.

I'm willing to send a PR if you are likely to accept it.

I have been using this code via the fork at https://github.com/princed/table-of-contents-preprocessor.

Running on file removed placeholder but generated no TOC

Adding the placeholder of @@toc@@ to myFile.md, and running:

$ md-toc-filter myFile.md > tocMyFile.md

...simply removed the placeholder, and no TOC was created from the ### Headers.

Expected behaviour: placeholder replaced with TOC including all # Headers.

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.