Skip to content

grantwinney/table-of-contents-for-html-page

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Table of Contents script

I wrote this to generate a table of contents for my blog posts (using the Ghost platform), but it should work for any HTML page.

Just pass the entire document (DOM) to it, and it'll parse out the header tags and generate markup for a table of contents. Call it from wherever you'd like, and then write out the results to the page.

Usage

For example, here's what I used in sidebar.hbs in Ghost.

<script type="text/javascript">
        document.write(getTocMarkup(document));
</script>

Notes

There's a few comments in the code, where you can make adjustments to suit your needs.

If you want to read more, or see what it should look like when rendered, I wrote about it here.

Releases

No releases published

Packages

No packages published