|
| 1 | +import Link from "next/link"; |
| 2 | +import { source } from "@/lib/source"; |
| 3 | + |
| 4 | +interface TagInfo { |
| 5 | + name: string; |
| 6 | + count: number; |
| 7 | + slug: string; |
| 8 | +} |
| 9 | + |
| 10 | +export default function TagsIndexPage() { |
| 11 | + // Collect all tags with their counts, normalizing case-insensitive duplicates |
| 12 | + const tagMap = new Map<string, { name: string; count: number }>(); |
| 13 | + |
| 14 | + source.getPages().forEach((page) => { |
| 15 | + const tags = (page.data as any).tags as string[] | undefined; |
| 16 | + tags?.forEach((tag) => { |
| 17 | + const slug = tag.toLowerCase().replace(/\s+/g, "-"); |
| 18 | + const existing = tagMap.get(slug); |
| 19 | + |
| 20 | + if (existing) { |
| 21 | + // Increment count, keep the first encountered name |
| 22 | + existing.count += 1; |
| 23 | + } else { |
| 24 | + tagMap.set(slug, { name: tag, count: 1 }); |
| 25 | + } |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + const tags: TagInfo[] = Array.from(tagMap.entries()) |
| 30 | + .map(([slug, { name, count }]) => ({ |
| 31 | + name, |
| 32 | + count, |
| 33 | + slug, |
| 34 | + })) |
| 35 | + .sort((a, b) => a.name.localeCompare(b.name)); |
| 36 | + |
| 37 | + // Group tags alphabetically by first letter |
| 38 | + const groupedTags = tags.reduce((acc, tag) => { |
| 39 | + const firstLetter = tag.name[0].toUpperCase(); |
| 40 | + if (!acc[firstLetter]) { |
| 41 | + acc[firstLetter] = []; |
| 42 | + } |
| 43 | + acc[firstLetter].push(tag); |
| 44 | + return acc; |
| 45 | + }, {} as Record<string, TagInfo[]>); |
| 46 | + |
| 47 | + const letters = Object.keys(groupedTags).sort(); |
| 48 | + |
| 49 | + return ( |
| 50 | + <div className="container relative mx-auto flex max-w-179 flex-1 shrink-1 flex-col gap-8 overflow-y-auto p-4 py-32"> |
| 51 | + <div> |
| 52 | + <h1 className="text-4xl font-bold mb-2">All Tags</h1> |
| 53 | + <p className="text-muted-foreground"> |
| 54 | + Browse documentation by tags |
| 55 | + </p> |
| 56 | + </div> |
| 57 | + |
| 58 | + <div className="space-y-8"> |
| 59 | + {letters.map((letter) => ( |
| 60 | + <div key={letter} className="space-y-4"> |
| 61 | + <h2 className="text-2xl font-semibold border-b pb-2">{letter}</h2> |
| 62 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"> |
| 63 | + {groupedTags[letter].map((tag) => ( |
| 64 | + <Link |
| 65 | + key={tag.slug} |
| 66 | + href={`/docs/tags/${tag.slug}`} |
| 67 | + className="group flex items-center justify-between rounded-lg border border-nc-border-grey-light bg-nc-background-default p-4 transition-colors hover:border-nc-border-grey hover:bg-nc-background-grey-light" |
| 68 | + > |
| 69 | + <span className="font-medium group-hover:text-nc-content-brand"> |
| 70 | + {tag.name} |
| 71 | + </span> |
| 72 | + <span className="text-sm text-muted-foreground"> |
| 73 | + {tag.count} |
| 74 | + </span> |
| 75 | + </Link> |
| 76 | + ))} |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + ))} |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +export async function generateMetadata() { |
| 86 | + return { |
| 87 | + title: "All Tags", |
| 88 | + description: "Browse all documentation tags", |
| 89 | + }; |
| 90 | +} |
0 commit comments