{"id":15663634,"url":"https://github.com/jonschlinkert/html-toc","last_synced_at":"2025-04-23T22:42:15.652Z","repository":{"id":47338846,"uuid":"84571258","full_name":"jonschlinkert/html-toc","owner":"jonschlinkert","description":"Generate a HTML table of contents using cheerio.","archived":false,"fork":false,"pushed_at":"2021-09-02T15:01:20.000Z","size":18,"stargazers_count":17,"open_issues_count":3,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-30T04:12:40.028Z","etag":null,"topics":["cheerio","html","table-of-contents","toc"],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonschlinkert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/contributing.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-10T15:02:14.000Z","updated_at":"2022-09-14T03:28:53.000Z","dependencies_parsed_at":"2022-09-01T20:13:27.274Z","dependency_job_id":null,"html_url":"https://github.com/jonschlinkert/html-toc","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fhtml-toc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fhtml-toc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fhtml-toc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fhtml-toc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonschlinkert","download_url":"https://codeload.github.com/jonschlinkert/html-toc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250528681,"owners_count":21445511,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cheerio","html","table-of-contents","toc"],"created_at":"2024-10-03T13:38:58.191Z","updated_at":"2025-04-23T22:42:15.635Z","avatar_url":"https://github.com/jonschlinkert.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# html-toc [![NPM version](https://img.shields.io/npm/v/html-toc.svg?style=flat)](https://www.npmjs.com/package/html-toc) [![NPM monthly downloads](https://img.shields.io/npm/dm/html-toc.svg?style=flat)](https://npmjs.org/package/html-toc) [![NPM total downloads](https://img.shields.io/npm/dt/html-toc.svg?style=flat)](https://npmjs.org/package/html-toc) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/html-toc.svg?style=flat\u0026label=Travis)](https://travis-ci.org/jonschlinkert/html-toc)\n\n\u003e Generate a HTML table of contents using cheerio.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save html-toc\n```\n\n## Usage\n\n```js\nvar toc = require('html-toc');\nconsole.log(toc(htmlString[, options]));\n```\n\n## Options\n\n### options.id\n\n**Type**: `string`\n\n**Default**: `#toc` (usage: `\u003cdiv id=\"toc\"\u003e\u003c/div\u003e`)\n\nSpecify the id for where the table of contents should be injected.\n\n**Example**\n\n```js\ntoc(str, {id: '#navigation'});\n```\n\nUsage:\n\n```html\n\u003cdiv id=\"navigation\"\u003e\u003c/div\u003e\n```\n\n### options.anchors\n\n**Type**: `boolean`\n\n**Default**: `undefined`\n\nSet to `false` to disable anchors.\n\n**Example**\n\n```js\ntoc(str, {anchors: false});\n```\n\n### options.anchorTemplate\n\nCustomize the template for creating anchors.\n\n**Type**: `function`\n\n**Default**\n\n```html\n\u003ca href=\"#${id}\" name=\"${id}\" class=\"anchor\"\u003e\n  \u003cspan class=\"anchor-target\" id=\"${id}\"\u003e\u003c/span\u003e\n  \u003cspan class=\"glyphicon glyphicon-link\"\u003e\u003c/span\u003e\n\u003c/a\u003e\n```\n\n**Example**\n\n```js\ntoc(str, {\n  anchorTemplate: function(id) {\n    return `\u003ca class=\"anchor\" href=\"${id}\" id=\"${id}\"\u003e\u003c/a\u003e`;\n  }\n});\n```\n\n### options.selectors\n\nHeading selectors to use for generating the table of contents.\n\n**Type**: `string`\n\n**Default**: `h1,h2`\n\n**Example**\n\nGenerate a table of contents for all headings h1-h4.\n\n```js\ntoc(str, {selectors: 'h1,h2,h3,h4'});\n```\n\n### options.parentLink\n\nSet whether to generate `id` attribute based on parent heading\n\n**Type**: `boolean`\n\n**Default**: `true`\n\n**Example**\n\n```js\ntoc(str, {parentLink: false});\n```\n\n### options.slugger\n\nCustomize the slugger for generating `id` attribute.\n\n**Type**: `function`\n\n**Default**: [markdown-slug](https://www.npmjs.com/package/markdown-slug)\n\n**Example**\n\n```js\ntoc(str, {\n  slugger: function(text) {\n    const re = /[\\u2000-\\u206F\\u2E00-\\u2E7F\\\\'!\"#$%\u0026()*+,./:;\u003c=\u003e?@[\\]^`{|}~]/g;\n    return text.toLowerCase().trim().replace(re, '').replace(/\\s/g, '-');\n  }\n});\n```\n\n### options.header\n\nSpecify html to be injected before table of contents.\n\n**Type**: `string`\n\n**Default**: `''`\n\n**Example**\n\n```js\ntoc(str, {header: '\u003ch2\u003eContents\u003c/h2\u003e'});\n```\n\n### options.minLength\n\nSet minimum number of headings for injecting table of contents.\n\n**Type**: `number`\n\n**Default**: `0`\n\n**Example**\n\n```js\ntoc(str, {minLength: 2});\n```\n\n### options.addID\n\n**Type**: `boolean`\n\n**Default**: `false`\n\nSet to `true` to add `id` attribute to selected headings even when headings' number is smaller than `options.minLength`.\n\n**Example**\n\nAlways add `id` attribute.\n\n```js\ntoc(str, {addID: true});\n```\n\n## About\n\n### Related projects\n\n* [breakdance](https://www.npmjs.com/package/breakdance): Breakdance is a node.js library for converting HTML to markdown. Highly pluggable, flexible and easy… [more](http://breakdance.io) | [homepage](http://breakdance.io \"Breakdance is a node.js library for converting HTML to markdown. Highly pluggable, flexible and easy to use. It's time for your markup to get down.\")\n* [markdown-toc](https://www.npmjs.com/package/markdown-toc): Generate a markdown TOC (table of contents) with Remarkable. | [homepage](https://github.com/jonschlinkert/markdown-toc \"Generate a markdown TOC (table of contents) with Remarkable.\")\n* [remarkable](https://www.npmjs.com/package/remarkable): Markdown parser, done right. 100% Commonmark support, extensions, syntax plugins, high speed - all in… [more](https://github.com/jonschlinkert/remarkable) | [homepage](https://github.com/jonschlinkert/remarkable \"Markdown parser, done right. 100% Commonmark support, extensions, syntax plugins, high speed - all in one.\")\n\n### Contributing\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\nPlease read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.\n\n### Contributors\n\n| **Commits** | **Contributor** | \n| --- | --- |\n| 9 | [dgeibi](https://github.com/dgeibi) |\n| 2 | [jonschlinkert](https://github.com/jonschlinkert) |\n\n### Building docs\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n### Running tests\n\nRunning and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n### Author\n\n**Jon Schlinkert**\n\n* [github/jonschlinkert](https://github.com/jonschlinkert)\n* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)\n\n### License\n\nCopyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 14, 2017._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fhtml-toc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonschlinkert%2Fhtml-toc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fhtml-toc/lists"}