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

markdown-toc will generate parts of toc from the content of html tag, like <pre>

Open junyulah opened this issue 8 years ago • 5 comments

I have a markdown file which contain <pre> tag. The content of <pre> is another markdown string. The bug is fired at this situation.

junyulah avatar Mar 20 '17 15:03 junyulah

I use this in many different documents that have markdown inside of pre tags and I've never seen this happen. Please provide an example of what's causing the bug so that we can reproduce it.

jonschlinkert avatar Mar 20 '17 15:03 jonschlinkert

I write a simple test, like below, and find that if "## test2" and "## test3" are at the head of line, bug will happen.

it('toc', () => {
        console.log(toc(` 
<pre>

## test2

## test3
</pre>
            `).content);
    });

result is :

- [test2](#test2)
- [test3](#test3)

junyulah avatar Mar 20 '17 16:03 junyulah

command line or API?

edit: thanks for the detail

jonschlinkert avatar Mar 20 '17 17:03 jonschlinkert

API calling. "##test2" and "##test3" must be at the beginning of the line.

let toc = require('markdown-toc');

describe('index', () => {
    it('toc', () => {
        console.log(toc(`
<pre>
## test2
## test3
</pre>
`).content); // I got :  - [test2](#test2) \n - [test3](#test3)
    });
});

junyulah avatar Mar 21 '17 04:03 junyulah

After thinking about this, I think that's technically correct as it is. If you were to use gfm instead of <pre>, you would get the following (using an array because of the nested backticks):

let toc = require('markdown-toc');

console.log(toc([
'# One',
'# Two',
'````',
'## test2',
'## test3',
'````',
'# Three',
'```',
'## test4',
'## test5',
'```',
'# Four',
].join('\n')).content); 
// yields
// - [One](#one)
// - [Two](#two)
// - [Three](#three)
// - [Four](#four)

I'd be happy to take a pr to add support for HTML tags - I see the usefulness of it, but we would need to weigh it against the technical debt of the edge cases and code complexity it would add.

jonschlinkert avatar Mar 21 '17 05:03 jonschlinkert