Giter Site home page Giter Site logo

showdown-toc's Introduction

showdown-toc

NPM version build status Test coverage npm download

A markdown-toc extension for showdown.
Features:

  • export table of contents info through closure
  • output table of contents into your html string

Install

esm

$ npm i showdown-toc --save

umd

<script src="showdown.js"></script>
<script src="//unpkg.com/showdown-toc/dist/index.umd.min.js"></script> 

Usage

1. export toc info through closure

import Showdown from 'showdown';
import showdownToc from 'showdown-toc';

const content = 'your markdown content';
const toc = [];
const showdown = new Showdown.Converter({ extensions: [showdownToc({ toc })] });
const result = showdown.makeHtml(content);
return result;

The toc array you pass in showdownToc will be:

[
    { anchor: 'header-1', level: 1, text: 'header 1' }, // # header 1
    { anchor: 'header-4', level: 4, text: 'header 4' }, // #### header 4
    ...
]

The table of contents will output by default as an ordered list <ol>. You can change this to an unordered list <ul> by passing in a second parameter of options:

const content = 'your markdown content';
const toc = [];
const opts = { listType: 'ul' };
const showdown = new Showdown.Converter({ extensions: [showdownToc({ toc, opts })] });
const result = showdown.makeHtml(content);
return result;

2. output table of contents into your html string

In your markdown just put a [toc] where you want a Table of Contents to appear. This extension will look for the first header after the [toc] and use whatever it finds first as the element for the rest of the TOC.

You can have multiple [toc] in a file, each one will show a Table of Contents for headers after it (and before the next [toc]).

If you move up a level from the headers being used for a [toc], the Table of Contents will stop (the assumption being you're "outside" of that section).

example

Markdown Input

# Main Page Heading
This is the intro to the main page.

## Section 1
A story.

[toc]

### Part 1
It was a nice day.

### Part 2
There were stormy clouds on the horizon.

#### Part 2A
They were very dark.

### Part 3
Then it rained.

## Section 2
Notice the section 2 header above is not included in the TOC of section 1? That's 
because each toc tag assumes it should stay in it's own section.

[toc]

### Part 1

### Part 2

#### Part 2A
Notice this heading isn't in the contents above. We only index the top level 
headings in each section, to keep things tidy. You may or may not like this, but 
that's the way it is. If you want to create a pull request with an option, 
you're welcome to! :)

### Part 3

The End.

HTML Output

Main Page Heading

This is the intro to the main page.

Section 1

A story.

  1. Part 1
  2. Part 2
  3. Part 3

Part 1

It was a nice day.

Part 2

There were stormy clouds on the horizon.

Part 2A

They were very dark.

Part 3

Then it rained.

Section 2

Notice the section 2 header above is not included in the TOC of section 1? That's because each toc tag assumes it should stay in it's own section.

  1. Part 1
  2. Part 2
  3. Part 3

Part 1

Part 2

Part 2A

Notice this heading isn't in the contents above. We only index the top level headings in each section, to keep things tidy. You may or may not like this, but that's the way it is. If you want to create a pull request with an option, you're welcome to! :)

Part 3

The End.

License

MIT

showdown-toc's People

Contributors

ahungrynoob avatar szul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

indiana-ruby szul

showdown-toc's Issues

How to show multi-level headers?

How to show multi-level headers?

[toc]

like:

image

I see you show the example "use [toc] in each section" in "output table of contents into your html string" section.

However, I did not see any example in your Readme to show how to use the multi-level headers.

You mensioned:

The toc array you pass in showdownToc will be:

[
    { anchor: 'header-1', level: 1, text: 'header 1' }, // # header 1
    { anchor: 'header-4', level: 4, text: 'header 4' }, // #### header 4
    ...
]

Whether you want to say if I want to use multi-level headers, I need to modify to:

The toc array you pass in showdownToc will be:

[
    { anchor: '#', level: 1, text: 'header 1' }, 
    { anchor: '##', level: 2, text: 'header 2' }, 
    ...
]

or sth. others.

Sorry, I cannot understand how to use to show multi-level table-of-contents list: e.g, 1, 1.1, 1.2 ; 2, 2.1, 2.2...

Thanks,

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.