Repository navigation
Expand file tree
/
Copy pathcatalog.ts
More file actions
68 lines (62 loc) · 2.2 KB
/
Copy pathcatalog.ts
File metadata and controls
68 lines (62 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/** Shared pure helpers for the templates/extensions catalog pages. */
export interface CatalogItemLike {
name: string;
slug: string;
description: string;
labels: string[];
}
export interface BreadcrumbSegment {
href: string;
label: string;
isLast: boolean;
}
/** Labels for known docs path segments; unknown segments fall back to the raw slug. */
export const DOCS_SEGMENT_LABELS: Record<string, string> = {
docs: 'Docs',
'agents-md': 'AGENTS.md',
installation: 'Installation',
templates: 'Templates',
customization: 'Customization',
extensions: 'Extensions',
contributing: 'Contributing',
advanced: 'Advanced',
usage: 'Usage',
};
/**
* Case-insensitive substring match of a free-text query against an item's
* name, slug, description, and labels. A blank query matches everything.
*/
export function matchesCatalogQuery(item: CatalogItemLike, query: string): boolean {
const normalized = query.trim().toLowerCase();
if (!normalized) return true;
return (
item.name.toLowerCase().includes(normalized) ||
item.slug.toLowerCase().includes(normalized) ||
item.description.toLowerCase().includes(normalized) ||
item.labels.some((label) => label.toLowerCase().includes(normalized))
);
}
/**
* Filter catalog items by category (exact match, empty = all) and free-text
* query. Pure — the templates/extensions client pages delegate to this.
*/
export function filterCatalogItems<T extends CatalogItemLike & { category: string }>(
items: T[],
options: { category?: string; query?: string } = {},
): T[] {
const { category = '', query = '' } = options;
return items.filter((item) => (!category || item.category === category) && matchesCatalogQuery(item, query));
}
/**
* Build the Home → … → Page breadcrumb trail for a docs pathname.
* Returns [] for top-level routes (no trail needed).
*/
export function buildDocsBreadcrumbs(pathname: string): BreadcrumbSegment[] {
const segments = pathname.split('/').filter(Boolean);
if (segments.length <= 1) return [];
return segments.map((segment, index) => ({
href: `/${segments.slice(0, index + 1).join('/')}`,
label: DOCS_SEGMENT_LABELS[segment] ?? segment,
isLast: index === segments.length - 1,
}));
}