Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/leisn/markdown-it-toc

Markdown-it plugin to make table of contents, default parse tag [toc],
https://github.com/leisn/markdown-it-toc

markdown markdown-it markdown-it-plugin markdown-it-toc

Last synced: about 1 month ago
JSON representation

Markdown-it plugin to make table of contents, default parse tag [toc],

Awesome Lists containing this project

README

        

# @leisn/markdown-it-toc

![GitHub](https://img.shields.io/github/license/leisn/markdown-it-toc) ![npm (scoped)](https://img.shields.io/npm/v/@leisn/markdown-it-toc)

Markdown-it plugin to make table of contents, default parse tag [toc].

## Installaton

```bash
$ npm install @leisn/markdown-it-toc
or
$ yarn add @leisn/markdown-it-toc
```

## E.g. default

### Source markdown

```markdown
[toc]
# H1-1
## H2-1
### H3-1
# H1-2
```

### Result document

```html



1
H1-1
#



1.1
H2-1
#



1.1.1
H3-1
#



2
H1-2
#


```

## Usage

```javascript
const md = require('markdown-it')(mkOpts) .use(require('@leisn/markdown-it-toc'),options);
console.log(md.render('[toc]\n# h1\n## h2-1\n## h2-2'));
```

## Options

* **tocRegexp?: RegExp** Regular expression to detect TOC (default `/\[toc\]/im`).

* **tocTag?: string** Tag for toc root element ( `div` by default ).

* **tocAttrs?: string** Attributes for toc root element( `id="toc" class="toc-wrapper"` by default ).

* **headingAttrs?: string** Attributes for headings `h1,h2,h3...` ( `class="toc-heading"` by default )

```html
--------------------------
# h1 title
--------------------------


h1 title


```

* **headingContentAttrs?: string** Attributes for headings content `span` ( `class="toc-heading-content"` by default )

```html
--------------------------
# headingContent
--------------------------


headingContent


```

* **getToc?(toc: string): boolean** Get toc result, return false if you don't want it show in the result html document, else return true(default `null`). `toc` : the result html document

* **getPathId?(info): string** Get id content for forwarding in page, (e.g. for `info.paths=[1,2]` default return `toc-no.1-2` )

* **getTocItemContent?(info): string** Content inside toc item `a` (html, include item heading text)

* **getHeadingPrefix?(info): string** Content inside heading (html, before heading text)

```html
--------------------------
# h1 title
--------------------------


{getHeadingPrefix}
h1 title


```

* **getHeadingSuffix?(info): string** Content inside heading (html, after heading text)

```html
--------------------------
# h1 title
--------------------------


h1 title
{getHeadingSuffix}


```

## Test

```bash
$ npm run test
or
$ yarn test
```

## LICENSE

MIT