It depends if you want to add it as soon as a new topic is created as well. I think it could be done with Custom Wizard Plugin 🧙.
You could add a TOC to all existing topics via the Rails console, though we tend to suggest this kind of solution when no other is available, and for experienced users, as it can easily go wrong.