Coder Social home page Coder Social logo

toniwonkanobi / markdown-it-table-of-contents-with-heading Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cmaas/markdown-it-table-of-contents

0.0 2.0 0.0 20 KB

A table of contents plugin for Markdown-it

License: MIT License

JavaScript 100.00%
markdown-it markdown-it-plugin

markdown-it-table-of-contents-with-heading's Introduction

This is a fork that places a <h4>Table of Contents</h4> within in the <div class="table-of-contents">.


markdown-it-table-of-contents

A table of contents plugin for Markdown-it. Based on https://github.com/samchrisinger/markdown-it-toc but simpler, a bit more customizable and with a default slugifier that matches that of https://www.npmjs.com/package/markdown-it-anchor.

Usage

var MarkdownIt = require("markdown-it");
var md = new MarkdownIt();

md.use(require("markdown-it-anchor")); // Optional, but makes sense as you really want to link to something
md.use(require("markdown-it-table-of-contents"));

Then add [[toc]] where you want the table of contents to be added in your markdown.

Example markdown

This markdown:

# Heading

[[toc]]

## Sub heading 1
Some nice text

## Sub heading 2
Some even nicer text

... would render this HTML using the default options specified in "usage" above:

<h1 id="heading">Heading</h1>

<div class="table-of-contents">
  <ul>
    <li><a href="#heading">Heading</a>
      <ul>
        <li><a href="#sub-heading-1">Sub heading 1</a></li>
        <li><a href="#sub-heading-2">Sub heading 2</a></li>
      </ul>
    </li>
  </ul>
</div>

<h2 id="sub-heading-1">Sub heading 1</h2>
<p>Some nice text</p>

<h2 id="sub-heading-2">Sub heading 2</h2>
<p>Some even nicer text</p>

Options

You may specify options when useing the plugin. like so:

md.use(require("markdown-it-table-of-contents"), options);

These options are available:

Name Description Default
"includeLevel" Headings levels to use (2 for h2:s etc) [1, 2]
"containerClass" The class for the container DIV "table-of-contents"
"slugify" A custom slugification function string.js' slugify
"markerPattern" Regex pattern of the marker to be replaced with TOC /^\[\[toc\]\]/im
"listType" Type of list (ul for unordered, ol for ordered) ul
"format" A function for formatting headings (see below) undefined

format is an optional function for changing how the headings are displayed in the TOC.

function format(headingAsString) {
  // manipulate the headings as you like here.
  return manipulatedHeadingString;
}

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.