markdown-toc
markdown-toc copied to clipboard
toc.insert throws error if <!-- toc --> present inside code block
let toc = require('markdown-toc');
sample = `<!-- toc -->
- old toc 1
- old toc 2
- old toc 3
<!-- tocstop -->
## abc
This is a b c.
\`\`\`\`
<!-- toc -->
<!-- tocstop -->
\`\`\`\`
## xyz
This is x y z.
`
console.log(toc.insert(sample).json);
Throws Error: markdown-toc only supports one Table of Contents per file.
Expectation is that the fenced code block would act as an escape.
Hi,
I had the same problem when writing technical documentation ("how to insert a toc"). The following is only a workaround and may not work for everyone:
<pre><code><!-- toc --></code></pre>
...and it does not throw the error if applied to a markdown file containing only a single TOC start comment and another one somewhere in a fenced code block.
In that case the insert()
function will expect the second TOC start comment to be the TOC stop and replace everything between these comments with the generated TOC.