anchor.diff:          e.map(i => {
             if (i.isIntersecting && i.intersectionRatio >= 1) {
               allTocLinks.forEach(link => link.classList.remove("toc-active"));
  -            document.querySelector(`#toc li a[href="#${i.target.id}"]`).classList.add('toc-active');
  +            toc_item = document.querySelector(`#toc li a[href="#${i.target.id}"]`)
  +            if(toc_item !== null) toc_item.classList.add('toc-active'); 
             }
           });
         };
  
positive.code:         document.querySelectorAll('.main h2[id], .main h3[id]').forEach((h) => {
            observer.unobserve(h);
          });
          var tocItems = $("#toc a");
          for (let i = 0; i < tocItems.length; i++) {
            tocItems[i].classList.remove("toc-active");
          }
          this.classList.add("toc-active");
        })
  
negative.code:   <!-- remove toc active class when page is loaded -->
    <script type="text/javascript">
      window.addEventListener('DOMContentLoaded', () => {
        toc_items = document.querySelectorAll('#toc a')
  
        for (let i = 0; i < toc_items.length; i++) {
          toc_items[i].classList.remove("toc-active");
        }
      })
    </script>
  