Automatic TOC generation is a difficult problem to solve. There’s more discussion about this in the aforementioned feature topic: