(function () {
    const toc = document.querySelector('[data-doc-toc]');
    const content = document.querySelector('.pp-doc-content');

    if (!toc || !content) return;

    const headings = content.querySelectorAll('h2, h3');

    headings.forEach((heading) => {
        if (!heading.id) {
            heading.id = heading.textContent
                .toLowerCase()
                .replace(/[^a-z0-9]+/g, '-')
                .replace(/^-|-$/g, '');
        }

        const link = document.createElement('a');
        link.href = '#' + heading.id;
        link.textContent = heading.textContent;
        link.className = heading.tagName === 'H3' ? 'toc-child' : '';

        toc.appendChild(link);
    });
})();