vsch / flexmark-java

CommonMark/Markdown Java parser with source level AST. CommonMark 0.28, emulation of: pegdown, kramdown, markdown.pl, MultiMarkdown. With HTML to MD, MD to PDF, MD to DOCX conversion modules.
BSD 2-Clause "Simplified" License
2.21k stars 260 forks source link

Cannot wrap ToC into div #459

Open arctrong opened 3 years ago

arctrong commented 3 years ago

To Reproduce

   private static String generateHtml(String mdText) {

        MutableDataSet options = new MutableDataSet();
        options.set(Parser.EXTENSIONS, Arrays.asList(TocExtension.create()));
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();
        Node document = parser.parse(mdText);
        return renderer.render(document);
    }

Give it the following input:

[TOC]

# Level 1

## Level 2

### Level 3

Expected behavior

<div class="toc">
<ul>
<li><a href="#level-1">Level 1</a><ul>
<li><a href="#level-2">Level 2</a><ul>
<li><a href="#level-3">Level 3</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<h1 id="level-1">Level 1</h1>
<h2 id="level-2">Level 2</h2>
<h3 id="level-3">Level 3</h3>

Resulting Output

<ul>
<li><a href="#level-1">Level 1</a><ul>
<li><a href="#level-2">Level 2</a><ul>
<li><a href="#level-3">Level 3</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h1 id="level-1">Level 1</h1>
<h2 id="level-2">Level 2</h2>
<h3 id="level-3">Level 3</h3>

Additional comments

If this is already implemented, please make it clear how to obtain this result. This page doesn't help. Thanks.

ghost commented 2 years ago

[TOC] isn't part of the Markdown standard. I contend that such a concept doesn't belong in Markdown.

Instead, flexmark-java could have an option to export a ToC as part of its configuration, rather than as a processing instruction inside a document.