Creating a table of contents with Markdown nested inside HTML

Whenever I have markdown inside HTML tags, it is not generated with the table of contents option. For example,

#Test

##Test2

Hello there!

#Test3

##Test4

<div>

#hello

</div>

      

Outputs:

<nav id="TOC">
<ul>
<li><a href="#test">Test</a><ul>
<li><a href="#test2">Test2</a></li>
</ul></li>
<li><a href="#test3">Test3</a><ul>
<li><a href="#test4">Test4</a></li>
</ul></li>
</ul>
</nav>
<h1 id="test">Test</h1>
<h2 id="test2">Test2</h2>
<p>Hello there!</p>
<h1 id="test3">Test3</h1>
<h2 id="test4">Test4</h2>
<div>
<h1>hello</h1>
</div>

      

I am using the following command:

pandoc -f markdown -t html5 --toc -s test.md

      

Is it possible to have a table of contents including markdown nested in HTML?

+3


source to share


1 answer


Since I just needed to solve the same problem, I thought I'd share it.

The Pandoc author referenced in this issue explains this behavior.

It also defines workarounds, in fact. Just replace <div>

with <article>

. In your case, it would be:



#Test

##Test2

Hello there!

#Test3

##Test4

<article>

#hello

</article>

      

with your command line, it gives me

<nav id="TOC">
<ul>
<li><a href="#test">Test</a><ul>
<li><a href="#test2">Test2</a></li>
</ul></li>
<li><a href="#test3">Test3</a><ul>
<li><a href="#test4">Test4</a></li>
</ul></li>
<li><a href="#hello">hello</a></li>
</ul>
</nav>
<h1 id="test">Test</h1>
<h2 id="test2">Test2</h2>
<p>Hello there!</p>
<h1 id="test3">Test3</h1>
<h2 id="test4">Test4</h2>
<article>
<h1 id="hello">hello</h1>
</article>

      

+1


source







All Articles