All Projects → jonschlinkert → Markdown Toc

jonschlinkert / Markdown Toc

Licence: mit
Generate a markdown TOC (table of contents) for a README or any markdown files, using remarkable. Used by assemble, verb, and lots of other projects on GitHub. API and CLI.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Markdown Toc

Markdown Toc
🎄Generate toc for github markdown file.(为 markdown 文件生成 toc 目录)
Stars: ✭ 144 (-87.85%)
Mutual labels:  toc, markdown, md
Github Markdown Toc.go
Easy TOC creation for GitHub README.md (in go)
Stars: ✭ 387 (-67.34%)
Mutual labels:  readme, toc, markdown
Github Profile Readme Generator
🚀 Generate GitHub profile README easily with the latest add-ons like visitors count, GitHub stats, etc using minimal UI.
Stars: ✭ 7,812 (+559.24%)
Mutual labels:  readme, markdown
Assemble
Community
Stars: ✭ 3,995 (+237.13%)
Mutual labels:  markdown, md
Mditor
📝 [ M ] arkdown + E [ ditor ] = Mditor
Stars: ✭ 523 (-55.86%)
Mutual labels:  markdown, md
Markdown Preview Enhanced
One of the 'BEST' markdown preview extensions for Atom editor!
Stars: ✭ 3,478 (+193.5%)
Mutual labels:  toc, markdown
Md To Pdf
Hackable CLI tool for converting Markdown files to PDF using Node.js and headless Chrome.
Stars: ✭ 374 (-68.44%)
Mutual labels:  markdown, md
Markdownview Android
MarkdownView is an Android webview with the capability of loading Markdown text or file and display it as HTML, it extends Android webview.
Stars: ✭ 432 (-63.54%)
Mutual labels:  readme, md
markdown-index
Generate a global index for multiple markdown files recursively
Stars: ✭ 15 (-98.73%)
Mutual labels:  readme, toc
Readme
README文件语法解读,即Github Flavored Markdown语法介绍
Stars: ✭ 5,812 (+390.46%)
Mutual labels:  readme, markdown
Remarkable
Markdown parser, done right. Commonmark support, extensions, syntax plugins, high speed - all in one. Gulp and metalsmith plugins available. Used by Facebook, Docusaurus and many others! Use https://github.com/breakdance/breakdance for HTML-to-markdown conversion. Use https://github.com/jonschlinkert/markdown-toc to generate a table of contents.
Stars: ✭ 5,252 (+343.21%)
Mutual labels:  markdown, md
Ngx Markdown
Angular markdown component/directive/pipe/service to parse static, dynamic or remote content to HTML with syntax highlight
Stars: ✭ 687 (-42.03%)
Mutual labels:  markdown, md
Markserv
🏁 serve markdown as html (GitHub style), index directories, live-reload as you edit
Stars: ✭ 304 (-74.35%)
Mutual labels:  markdown, md
Format Readme
Формат файла README
Stars: ✭ 270 (-77.22%)
Mutual labels:  readme, markdown
Markdown-Templates
bgoonz.github.io/markdown-templates/
Stars: ✭ 16 (-98.65%)
Mutual labels:  readme, project
Vim Markdown Toc
A vim 7.4+ plugin to generate table of contents for Markdown files.
Stars: ✭ 427 (-63.97%)
Mutual labels:  toc, markdown
Readme styles
Minimal README.rst and README.md template for Github projects.
Stars: ✭ 60 (-94.94%)
Mutual labels:  readme, markdown
Markdown-Crash-Course
Markdown Crash Course. Learn Markdown language on a simple way.
Stars: ✭ 100 (-91.56%)
Mutual labels:  readme, md
peterthehan
My personal site and profile README.
Stars: ✭ 36 (-96.96%)
Mutual labels:  readme, project
Markdown Magic
💫 Automatically format markdown files, sync external docs/src code & make better docs
Stars: ✭ 551 (-53.5%)
Mutual labels:  readme, markdown

markdown-toc NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

Generate a markdown TOC (table of contents) with Remarkable.

Table of Contents

(TOC generated by verb using markdown-toc)

Install

Install with npm:

$ npm install --save markdown-toc

CLI

Usage: markdown-toc [options] <input>

  input:        The Markdown file to parse for table of contents,
                or "-" to read from stdin.

  -i:           Edit the <input> file directly, injecting the TOC at <!-- toc -->;
                (Without this flag, the default is to print the TOC to stdout.)

  --json:       Print the TOC in JSON format

  --append:     Append a string to the end of the TOC

  --bullets:    Bullets to use for items in the generated TOC
                (Supports multiple bullets: --bullets "*" --bullets "-" --bullets "+")
                (Default is "*".)

  --maxdepth:   Use headings whose depth is at most maxdepth
                (Default is 6.)

  --no-firsth1: Include the first h1-level heading in a file

  --no-stripHeadingTags: Do not strip extraneous HTML tags from heading
                         text before slugifying

Highlights

Features

  • Can optionally be used as a remarkable plugin
  • Returns an object with the rendered TOC (on content), as well as a json property with the raw TOC object, so you can generate your own TOC using templates or however you want
  • Works with repeated headings
  • Uses sane defaults, so no customization is necessary, but you can if you need to.
  • filter out headings you don't want
  • Improve the headings you do want
  • Use a custom slugify function to change how links are created

Safe!

  • Won't mangle markdown in code examples in gfm code blocks that other TOC generators mistake as being actual headings (this happens when markdown headings are show in examples, meaning they arent' actually headings that should be in the toc. Also happens with yaml and coffee-script comments, or any comments that use #)
  • Won't mangle front-matter, or mistake front-matter properties for headings like other TOC generators

Usage

var toc = require('markdown-toc');

toc('# One\n\n# Two').content;
// Results in:
// - [One](#one)
// - [Two](#two)

To allow customization of the output, an object is returned with the following properties:

  • content {String}: The generated table of contents. Unless you want to customize rendering, this is all you need.
  • highest {Number}: The highest level heading found. This is used to adjust indentation.
  • tokens {Array}: Headings tokens that can be used for custom rendering

API

toc.plugin

Use as a remarkable plugin.

var Remarkable = require('remarkable');
var toc = require('markdown-toc');

function render(str, options) {
  return new Remarkable()
    .use(toc.plugin(options)) // <= register the plugin
    .render(str);
}

Usage example

var results = render('# AAA\n# BBB\n# CCC\nfoo\nbar\nbaz');

Results in:

- [AAA](#aaa)
- [BBB](#bbb)
- [CCC](#ccc)

toc.json

Object for creating a custom TOC.

toc('# AAA\n## BBB\n### CCC\nfoo').json;

// results in
[ { content: 'AAA', slug: 'aaa', lvl: 1 },
  { content: 'BBB', slug: 'bbb', lvl: 2 },
  { content: 'CCC', slug: 'ccc', lvl: 3 } ]

toc.insert

Insert a table of contents immediately after an opening <!-- toc --> code comment, or replace an existing TOC if both an opening comment and a closing comment (<!-- tocstop -->) are found.

(This strategy works well since code comments in markdown are hidden when viewed as HTML, like when viewing a README on GitHub README for example).

Example

<!-- toc -->
- old toc 1
- old toc 2
- old toc 3
<!-- tocstop -->

## abc
This is a b c.

## xyz
This is x y z.

Would result in something like:

<!-- toc -->
- [abc](#abc)
- [xyz](#xyz)
<!-- tocstop -->

## abc
This is a b c.

## xyz
This is x y z.

Utility functions

As a convenience to folks who wants to create a custom TOC, markdown-toc's internal utility methods are exposed:

var toc = require('markdown-toc');
  • toc.bullets(): render a bullet list from an array of tokens
  • toc.linkify(): linking a heading content string
  • toc.slugify(): slugify a heading content string
  • toc.strip(): strip words or characters from a heading content string

Example

var result = toc('# AAA\n## BBB\n### CCC\nfoo');
var str = '';

result.json.forEach(function(heading) {
  str += toc.linkify(heading.content);
});

Options

options.append

Append a string to the end of the TOC.

toc(str, {append: '\n_(TOC generated by Verb)_'});

options.filter

Type: Function

Default: undefined

Params:

  • str {String} the actual heading string
  • ele {Objecct} object of heading tokens
  • arr {Array} all of the headings objects

Example

From time to time, we might get junk like this in our TOC.

[.aaa([foo], ...) another bad heading](#-aaa--foo--------another-bad-heading)

Unless you like that kind of thing, you might want to filter these bad headings out.

function removeJunk(str, ele, arr) {
  return str.indexOf('...') === -1;
}

var result = toc(str, {filter: removeJunk});
//=> beautiful TOC

options.slugify

Type: Function

Default: Basic non-word character replacement.

Example

var str = toc('# Some Article', {slugify: require('uslug')});

options.bullets

Type: String|Array

Default: *

The bullet to use for each item in the generated TOC. If passed as an array (['*', '-', '+']), the bullet point strings will be used based on the header depth.

options.maxdepth

Type: Number

Default: 6

Use headings whose depth is at most maxdepth.

options.firsth1

Type: Boolean

Default: true

Exclude the first h1-level heading in a file. For example, this prevents the first heading in a README from showing up in the TOC.

options.stripHeadingTags

Type: Boolean

Default: true

Strip extraneous HTML tags from heading text before slugifying. This is similar to GitHub markdown behavior.

About

Related projects

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

Commits Contributor
196 jonschlinkert
4 stefanwalther
3 Marsup
2 dvcrn
2 maxogden
2 twang2218
2 angrykoala
2 zeke
1 Vortex375
1 owzim
1 chendaniely
1 Daniel-Mietchen
1 Feder1co5oave
1 garygreen
1 TehShrike
1 citizenmatt
1 rafaelsteil
1 RichardBradley
1 sethvincent
1 lu22do

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on September 19, 2017.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].