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
Toc.astroAstro47 lines1.8 KiBraw download
---
import { isSelfNumbered } from '../lib/rehype-heading-marks.mjs';

interface Heading {
  depth: number;
  slug: string;
  text: string;
}
interface Props {
  headings: Heading[];
  /** Number the sections, matching the numbers the article's own headings
   *  carry. Only the hand-authored sheets do (rehype-heading-marks); the
   *  mirrors keep their headings as written, so their rail stays unnumbered. */
  numbered?: boolean;
}
const { headings, numbered = false } = Astro.props;

/**
 * "On this page": the contents rail beside every article, and the same node
 * folded into a disclosure above it on a narrow screen. One element serves
 * both so the scroll-spy in app.ts (`initTOC`) has a single set of links.
 * Styling is in global.css (`.toc`) with the layout it sits in.
 */
const toc = headings.filter((h) => h.depth === 2 || h.depth === 3);
const sections = toc.filter((h) => h.depth === 2);
// The same test the heading pass applies, on the same titles, so a sheet
// that numbers its own sections is left alone in both places.
const showNumbers = numbered && sections.length > 0 && !isSelfNumbered(sections.map((h) => h.text));
let n = 0;
const items = toc.map((h) => ({ ...h, n: h.depth === 2 ? String(++n).padStart(2, '0') : '' }));
---
{toc.length > 1 && (
  <details class:list={['toc', { 'toc--numbered': showNumbers }]} open>
    <summary>
      <span>On this page</span>
      <span class="toc__count">{String(sections.length || toc.length).padStart(2, '0')}</span>
    </summary>
    <nav aria-label="On this page">
      {items.map((h) => (
        <a href={`#${h.slug}`} class={h.depth === 3 ? 'lvl-3' : 'lvl-2'}>
          {showNumbers && h.n && <span class="toc__n">{h.n}</span>}
          <span>{h.text}</span>
        </a>
      ))}
    </nav>
  </details>
)}