jonschlinkert / markdown-toc

API and CLI for generating a markdown TOC (table of contents) for a README or any markdown files. Uses Remarkable to parse markdown. Used by NASA/openmct, Prisma, Joi, Mocha, Sass, Prettier, Orbit DB, FormatJS, Raneto, hapijs/code, webpack-flow, docusaurus, release-it, ts-loader, json-server, reactfire, bunyan, husky, react-easy-state, react-snap, chakra-ui, carbon, alfresco, repolinter, Assemble, Verb, and thousands of other projects.
https://github.com/jonschlinkert
MIT License
1.62k stars 702 forks source link

--no-firsth1 option causes "undefined" words to appear in the toc #168

Open trusktr opened 3 years ago

trusktr commented 3 years ago

Example without the option:

$ npx markdown-toc README.md
- [my-project](#my-project)
- [Getting started](#getting-started)
- [Usage](#usage)
  * [Example](#example)
- [API](#api)
  * [The `` element](#the--element)
    + [Attributes/Properties](#attributesproperties)
    + [Methods](#methods)
    + [Events](#events)
- [More Examples](#more-examples)
  * [Usage with [`@lume/element`](https://github.com/lume/element)](#usage-with-lumeelementhttpsgithubcomlumeelement)
  * [Usage with React](#usage-with-react)
  * [Usage with React and TypeScript](#usage-with-react-and-typescript)
  * [Usage with other view libs/frameworks](#usage-with-other-view-libsframeworks)
- [Development](#development)
  * [Test it in the examples](#test-it-in-the-examples)
  * [Test it in another project](#test-it-in-another-project)

Example with the option:

$ npx markdown-toc --no-firsth1 README.md
undefined [Getting started](#getting-started)
undefined [Usage](#usage)
- [Example](#example)
undefined [API](#api)
- [The `` element](#the--element)
  * [Attributes/Properties](#attributesproperties)
  * [Methods](#methods)
  * [Events](#events)
undefined [More Examples](#more-examples)
- [Usage with [`@lume/element`](https://github.com/lume/element)](#usage-with-lumeelementhttpsgithubcomlumeelement)
- [Usage with React](#usage-with-react)
- [Usage with React and TypeScript](#usage-with-react-and-typescript)
- [Usage with other view libs/frameworks](#usage-with-other-view-libsframeworks)
undefined [Development](#development)
- [Test it in the examples](#test-it-in-the-examples)
- [Test it in another project](#test-it-in-another-project)
doowb commented 3 years ago

@trusktr thank you for the issue and the example. Will you also include the source README.md to help determine where the bug is occurring?

nicobo commented 3 years ago

It happens only when the file contains <!-- toc -->. Sample file : README.md.txt.

Without <!-- toc --> :

mycomputer$ markdown-toc --no-firsth1 README.md 
- [About](#about)
- [Requirements & installation](#requirements--installation)
  * [Python package installation](#python-package-installation)
  * [Installation from source](#installation-from-source)
  * [Docker usage](#docker-usage)
...

With <!-- toc --> :

mycomputer$ markdown-toc --no-firsth1 README.md 
undefined [Requirements & installation](#requirements--installation)
- [Python package installation](#python-package-installation)
- [Installation from source](#installation-from-source)
- [Docker usage](#docker-usage)
...
nicobo commented 3 years ago

Also it's probably a duplicate of #42 and #131