1.3.0 • Published 3 years ago

markdown-it-toc-desc v1.3.0

Weekly downloads
3
License
MIT
Repository
-
Last release
3 years ago

markdown-it-toc-desc

It's a markdown-it plugin which can get the descriptor of the table of content without any changes and markup.

install

yarn add markdown-it-toc-desc

Usage

import MarkdownItTocDesc, { Heading, MarkdownTocDescOption } from "markdown-it-toc-desc";
import Markdown from "markdown-it";

const md = new Markdown();
const article = `
# h1.1
## h2.1.1
# h1.2
## h2.2.1
## h2.2.2
# h1.3
#### h3.4.1
 `;
let headings: Heading[] = [];
const options: MarkdownTocDescOption = {
    includeLevel: [1, 2, 3, 4, 5, 6],
    slugify: (s) => s,
    getTocTree: (tree) => (headings = tree),
};
md.use(MarkdownItTocDesc, options);
md.render(article);
// then you will get the toc desc in headings
console.log(headings);

Heading

Type Heading is the descriptor of toc we got.

here the properties:

keytype
linkstring
contentstring
childrenArray of Heading

MarkdownTocDescOption

keytypedesc
includeLevelArray of numberthe layer we need
slugify(s: string) => stringcontent format function
getTocTree( tree:Heading[]) => voidget the heading tree function

Other declaration

Only adjacent layer can be obtained in this plugin

e.g. we could not get h3 under h1 from # h1 ### h3