Factor out "Table of Contents" generation

This change removes code duplication and makes it easier to change
the "Table of Contents" across all pages.