// Pure renderers shared by the catalog island (client) and catalog.astro // (server-rendered first page): chips, badges, one technique row. No DOM, no // side effects — the same function builds the SSR'd rows and the fetched ones, // which is what lets the island adopt the server's markup without a re-render. import { toolRoute, type Technique } from './techniques'; import { PLATFORM_META, CAPABILITY_META, SOURCE_META } from './taxonomy'; import { escapeHtml as esc, highlightCommand } from './highlight'; import { url } from './url'; import { FACET_KEYS, facetValues, searchText, type CatalogFilters, type FacetKey } from './techniques'; import { FACET_TITLE, facetLabel } from './insights'; /** Rows per page, server and client alike. */ export const PAGE = 40; /** * The fields the list view needs — exactly what public/data/index-.json * carries (scripts/build-index.mjs keeps an identical list; a test pins them * together). Everything the text query scans is here, so search behaves the * same as over the full record; detection, references, paths and the rest * live on the tool page the row links to. */ export const LIST_FIELDS = [ 'id', 'toolId', 'toolName', 'name', 'source', 'platform', 'capability', 'nativeCategory', 'command', 'description', 'usecase', 'mitre', 'privilege', 'added', 'context', 'requires', 'services', 'environment', 'aliases', 'availability', 'verification', 'reviewedAt', 'compatibility', 'expected', 'troubleshooting', 'sideEffects', 'restore', 'template', 'references', 'detection', ] as const; export type ListTechnique = Pick; export function toListTechnique(t: Technique): ListTechnique { const out: Partial> = {}; for (const k of LIST_FIELDS) if (t[k] !== undefined) out[k] = t[k]; return out as ListTechnique; } // ---- chips ----------------------------------------------------------------- export function chip(kind: string, value: string, label: string, accent: string, active: boolean, count: number): string { return ``; } /** Groups that start folded: rarely the first thing reached for, and the rail * is long enough without them. The island opens any group with a value on. */ const FOLDED: ReadonlySet = new Set(['environment', 'availability', 'verification']); export function facetGroup(kind: FacetKey, values: readonly string[], accent: string, active: string[], counts: Record): string { const chips = values.filter((v) => counts[v]).map((v) => chip(kind, v, facetLabel(kind, v), accent, active.includes(v), counts[v] || 0)).join(''); const open = active.length || !FOLDED.has(kind) ? ' open' : ''; return `
^:${esc(FACET_TITLE[kind])}${active.length}
${chips}
`; } export type FacetCounts = Partial>>; export function totalFacetCounts(rows: Technique[]): FacetCounts { return Object.fromEntries(FACET_KEYS.map(k => { const counts: Record = {}; for (const t of rows) for (const v of facetValues(t, k)) counts[v] = (counts[v] || 0) + 1; return [k, counts]; })); } /** The whole facet rail: one foldable group per facet, chip counts are dataset totals. */ export function renderFacets(active: Pick & Partial, counts: FacetCounts): string { return FACET_KEYS.map(k => facetGroup(k, Object.keys(counts[k] || {}).sort(), k === 'source' ? 'rose' : 'foam', active[k] || [], counts[k] || {})).join(''); } // ---- badges ---------------------------------------------------------------- export const badge = (label: string, accent: string, extra = '') => `${esc(label)}`; // ---- row ------------------------------------------------------------------- /** A DOM id for the detail panel so the expander can name what it controls. */ export const panelId = (id: string) => `td-${id.replace(/[^A-Za-z0-9_-]+/g, '-')}`; export function renderRow(t: ListTechnique, variants: ListTechnique[] = []): string { const caps = t.capability.map((c) => badge(c, CAPABILITY_META[c]?.accent || 'foam')).join(''); const plats = t.platform.map((p) => badge(p, PLATFORM_META[p]?.accent || 'foam', 'tbadge--soft')).join(''); const src = SOURCE_META[t.source]; const srcBadge = badge(src.label, src.accent, 'tbadge--src'); const newBadge = t.added && t.source !== 'DAEMON' ? `DÆMON` : ''; const label = t.name && t.name !== t.toolName ? `${esc(t.name)}` : ''; const route = url(toolRoute(t.toolId)); const pid = panelId(t.id); const mitre = t.mitre?.length ? `
MITRE${t.mitre.map((m) => `${esc(m)}`).join(' ')}
` : ''; const priv = t.privilege ? `
Context${esc(t.privilege)}
` : ''; const desc = t.description ? `

${esc(t.description)}

` : ''; const use = t.usecase ? `

Use ${esc(t.usecase)}

` : ''; const more = `Full details on the ${esc(t.toolName)} page — detection, references, paths →`; return `
${esc(t.toolName)}${label} ${srcBadge}${newBadge}${caps}${plats}
${variants.length > 1 ? `` : ''}
${highlightCommand(t.command)}
${esc(t.context || t.privilege || t.environment?.join(', ') || 'See prerequisites')}
`; } export const EMPTY_HTML = `

^:No matches

Nothing matches this filter. Loosen a facet or clear the search.

`; /** The count line and the "Show more" label, so server and client agree. */ export function countLabel(shown: number, total: number, activeFacets: number, addedOnly: boolean): string { return `${shown.toLocaleString()} of ${total.toLocaleString()} techniques${activeFacets ? ` · ${activeFacets} filter${activeFacets === 1 ? '' : 's'}` : ''}${addedOnly ? ' · DÆMON authored' : ''}`; } export function moreLabel(remaining: number): string { return `Show ${Math.min(remaining, PAGE)} more · ${remaining} remaining`; } /** * The same fields lib/techniques.ts#matchesQuery scans, lowercased once per * record at boot instead of once per record per keystroke. */ export function haystack(t: ListTechnique): string { return searchText(t as Technique); }