repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
section-jump.tsTypeScript38 lines1.7 KiBraw download
// The section jump strip (SectionJump.astro): marks the section being read
// and keeps its link in view inside the strip's own sideways scroll.

export function initSectionJump(): void {
  const nav = document.querySelector<HTMLElement>('[data-sec-jump]');
  if (!nav || nav.dataset.bound || !('IntersectionObserver' in window)) return;
  nav.dataset.bound = '1';

  const track = nav.querySelector<HTMLElement>('.sec-jump__track');
  const links = [...nav.querySelectorAll<HTMLAnchorElement>('a[data-sf-jump]')];
  const byId = new Map(links.map((l) => [decodeURIComponent(l.hash.slice(1)), l]));

  const mark = (id: string): void => {
    const link = byId.get(id);
    if (!link || link.getAttribute('aria-current') === 'true') return;
    links.forEach((l) => l.removeAttribute('aria-current'));
    link.setAttribute('aria-current', 'true');
    // Scroll the strip, never the page: `scrollIntoView` would move both.
    if (track) {
      const left = link.offsetLeft - track.offsetLeft;
      const right = left + link.offsetWidth;
      if (left < track.scrollLeft) track.scrollLeft = left;
      else if (right > track.scrollLeft + track.clientWidth) track.scrollLeft = right - track.clientWidth;
    }
  };

  // A band just under the masthead and strip: whichever section crosses it
  // is the one being read.
  const observer = new window.IntersectionObserver(
    (entries) => entries.forEach((e) => { if (e.isIntersecting) mark((e.target as HTMLElement).id); }),
    { rootMargin: '-130px 0px -72% 0px', threshold: 0 },
  );
  byId.forEach((_, id) => {
    const section = document.getElementById(id);
    if (section) observer.observe(section);
  });
  document.addEventListener('astro:before-swap', () => observer.disconnect(), { once: true });
}