markdown-toc icon indicating copy to clipboard operation
markdown-toc copied to clipboard

toc.insert throws error if <!-- toc --> present inside code block

Open dstaudt opened this issue 5 years ago • 2 comments

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.

dstaudt avatar May 09 '19 16:05 dstaudt

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>&lt;!-- toc --&gt;</code></pre>

bvfnbk avatar Nov 05 '21 08:11 bvfnbk

...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.

bvfnbk avatar Nov 05 '21 10:11 bvfnbk