showdown-toc-export
v1.0.3
Published
A showdown extension to output toc info.
Downloads
19
Maintainers
Readme
showdown-toc
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
...
]
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