I wrote a custom bb code which does all with javascript. You don't need to use special bb codes for heading, the headings are determined by font-size.
<?php
namespace XF\Toc;
use XF\BbCode\Renderer\AbstractRenderer;
class FontSizeToc {
public static function...