Making a table of content (TOC)

I have a folderish content type (book), with 'pages' (chapter).

If I want to make a table of content that links to all headings ( h1 to h4) in ALL chapters (and probably put this TOC in a viewlet or a portlet), how can this be done ?

Have a look at plone.app.layout.navigation.navtree.buildFolderTree.

Not sure if I understand you right here (or if my explanation is not good).

I want to have a folder (book) with
Chapters (content type) – That has (many) h2, h3, h4

Then I want to have a TOC for 'everything in the folder', something like this

Title of Chapter 1
   First h2 of chapter 1
       First h3 of chapter 2
  Secon h3 of chapter 2
Title of Chapter 2

etc.

This sounds like you could look at the code for the sitemap view or maybe the navigation portlet as a start.