You cannot generate things automatically in HTML. You would have to do the generation using a preprocessor, or a server-side tool, or with client-side JavaScript.
The best you can do in HTML to allow browsers or their add-ons to generate a table of contents is to use heading elements h1
, h2
etc. consistently, according to the logical nesting level. This is what ToC generators such as the Firefox Table of Contents add-on use as data.