cmaas / markdown-it-table-of-contents

A table of contents plugin for Markdown-it
MIT License
99 stars 33 forks source link

more specific header selection #63

Open SArpnt opened 1 week ago

SArpnt commented 1 week ago

in my document i want to only generate a table of contents for the headers inside the MAIN tag. outside this main tag is a header for the full page, a header for the table of contents, and another header for an acknowledgements section.

i think the most versatile way to allow this and other complex header management would be to allow css selectors for headers. for example, i could set the option to ["main h2", "main h3", "main h4"]. numbers can keep the same behavior as before for compatibility. it would also allow using any other elements for headers.

i might be able to write a pull request for this later.

cmaas commented 1 week ago

markdown-it and its plugins operate on the Markdown level, not on the HTML level. The output is HTML. Hence, while building the table of contents, the structure of the target HTML is not known yet. So there is no querySelectorAll for main h2 and so on.

The plugin basically counts all markdown-it node with token.type === 'heading_open' and takes the level from there. For easier understanding, the level is called h1, h2, etc. in the plugin configuration, but it has nothing to do with a DOM query selector.

What does your input Markdown file look like from which you want to select DOM nodes?

SArpnt commented 1 week ago

i started using this plugin after a discussion to possibly replace the snap reference manual with markdown (currently it's a word doc, converted to a pdf) i'm trying to keep it mostly matched up, and as one document so it can still be printed to a nice pdf afterwards. here's my current outline (condensed to the relevant parts):

<style>
    /* styling to improve the print layout */
</style>

<div style="...">
    <!-- various other elements for styling, not important -->
    <h1 style="...">Snap! Reference Manual</h1>
</div>

<aside><nav>

## Table of Contents

[[toc]]

</nav><aside>

<aside>

## Acknowledgements

...

</aside>

<main>

<hgroup>
    <h1>Reference Manual</h1>
    <p>Version 10.0</p>
<hgroup>

(about the program)

(many more nested headers, regular markdown past this point)

</main>

the title, table of contents, and acknowledgements shouldn't be part of the table of contents. only what's in \

is the actual manual.