2.0.0 • Published 8 months ago

@serferdinand2/table-of-contents-generator v2.0.0

Weekly downloads
-
License
GNU
Repository
github
Last release
8 months ago

TOC Generator for HTML and Markdown

Installation

You can install the TOC generator using npm:

npm i @serferdinand2/table-of-contents-generator

Usage

You should be able to use this module in both Node.js and the browser as it supports both CommonJS and ES6 modules.

To use the TOC generator, import the function from the toc-generator module and call it with your input source and type. The function returns a table of contents as an HTML list.

This function only works as long as the first header is the top level header. If you have a header above the top level header down the line in your HTML, it will not work.

Example

import parseHeaders from '@serferdinand2/table-of-contents-generator';

const toc = parseHeaders(html, 'html');

Output

[
	{
		headerText: 'Header 1',
		id: 'header-1',
		children: [
			{
				headerText: 'Header 2',
				id: 'header-2',
				children: [
					{
						headerText: 'Header 3',
						id: 'header-3',
						children: [],
					},
					{
						headerText: 'Header 3',
						id: 'header-3',
						children: [],
					},
					{
						headerText: 'Header 3',
						id: 'header-3',
						children: [],
					},
				],
			},
			{ headerText: 'Header 2', id: 'header-2', children: [] },
			{
				headerText: 'Header 2',
				id: 'header-2',
				children: [
					{
						headerText: 'Header 3',
						id: 'header-3',
						children: [],
					},
					{
						headerText: 'Header 3',
						id: 'header-3',
						children: [],
					},
					{
						headerText: 'Header 3',
						id: 'header-3',
						children: [],
					},
				],
			},
			{ headerText: 'Header 2', id: 'header-2', children: [] },
		],
	},
];

or

const toc = parseHeaders(markdown, 'markdown');
2.0.0

8 months ago

1.0.3

8 months ago

1.0.0-alpha.5

8 months ago

1.0.0-alpha.4

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago

1.0.0-alpha.3

8 months ago

1.0.0-alpha.2

8 months ago

1.0.0-alpha.1

8 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago