|
| 1 | +// Base type for all TOC items with core properties |
1 | 2 | export type BaseTocItem = { |
2 | 3 | fullPath: string |
3 | 4 | title: string |
4 | 5 | intro?: string |
5 | 6 | } |
6 | 7 |
|
| 8 | +// Valid octicon types that match the CookBookArticleCard component |
| 9 | +export type ValidOcticon = |
| 10 | + | 'code' |
| 11 | + | 'log' |
| 12 | + | 'terminal' |
| 13 | + | 'bug' |
| 14 | + | 'lightbulb' |
| 15 | + | 'gear' |
| 16 | + | 'rocket' |
| 17 | + | 'beaker' |
| 18 | + | 'copilot' |
| 19 | + | 'hubot' |
| 20 | + | 'book' |
| 21 | + | 'shield-lock' |
| 22 | + | 'lock' |
| 23 | + |
| 24 | +// Extended type for child TOC items with additional metadata |
7 | 25 | export type ChildTocItem = BaseTocItem & { |
8 | | - octicon?: |
9 | | - | 'code' |
10 | | - | 'log' |
11 | | - | 'terminal' |
12 | | - | 'bug' |
13 | | - | 'lightbulb' |
14 | | - | 'gear' |
15 | | - | 'rocket' |
16 | | - | 'beaker' |
17 | | - | 'copilot' |
18 | | - | 'hubot' |
19 | | - | 'book' |
| 26 | + octicon?: ValidOcticon |
20 | 27 | category?: string[] |
21 | 28 | complexity?: string[] |
22 | 29 | industry?: string[] |
23 | 30 | } |
24 | 31 |
|
| 32 | +// Main TOC item type that can contain children |
25 | 33 | export type TocItem = BaseTocItem & { |
26 | 34 | childTocItems?: ChildTocItem[] |
| 35 | + octicon?: ValidOcticon |
| 36 | + category?: string[] |
| 37 | + complexity?: string[] |
| 38 | + industry?: string[] |
27 | 39 | } |
28 | 40 |
|
| 41 | +// Type alias for article card components |
29 | 42 | export type ArticleCardItems = ChildTocItem[] |
| 43 | + |
| 44 | +// Raw TOC type that matches the actual data structure from getTocItems() |
| 45 | +// This includes all properties that may be present in the source data |
| 46 | +export type RawTocItem = { |
| 47 | + title: string |
| 48 | + fullPath: string |
| 49 | + intro: string | null |
| 50 | + octicon: string | null |
| 51 | + category: string[] | null |
| 52 | + complexity: string[] | null |
| 53 | + industry: string[] | null |
| 54 | + childTocItems: RawTocItem[] |
| 55 | +} |
| 56 | + |
| 57 | +// Helper function to validate and cast octicon values |
| 58 | +export function isValidOcticon(octicon: string | null): octicon is ValidOcticon { |
| 59 | + const validOcticons: ValidOcticon[] = [ |
| 60 | + 'code', |
| 61 | + 'log', |
| 62 | + 'terminal', |
| 63 | + 'bug', |
| 64 | + 'lightbulb', |
| 65 | + 'gear', |
| 66 | + 'rocket', |
| 67 | + 'beaker', |
| 68 | + 'copilot', |
| 69 | + 'hubot', |
| 70 | + 'book', |
| 71 | + 'shield-lock', |
| 72 | + 'lock', |
| 73 | + ] |
| 74 | + return octicon !== null && validOcticons.includes(octicon as ValidOcticon) |
| 75 | +} |
| 76 | + |
| 77 | +// Simplified TOC item type for basic landing pages that don't need extended metadata |
| 78 | +export type SimpleTocItem = { |
| 79 | + fullPath: string |
| 80 | + title: string |
| 81 | + intro?: string |
| 82 | + childTocItems?: Array<{ |
| 83 | + fullPath: string |
| 84 | + title: string |
| 85 | + }> |
| 86 | +} |
| 87 | + |
| 88 | +// Reusable mapper function to convert RawTocItem to TocItem with full metadata |
| 89 | +export function mapRawTocItemToTocItem(raw: RawTocItem): TocItem { |
| 90 | + return { |
| 91 | + fullPath: raw.fullPath, |
| 92 | + title: raw.title, |
| 93 | + intro: raw.intro || undefined, |
| 94 | + octicon: isValidOcticon(raw.octicon) ? raw.octicon : undefined, |
| 95 | + category: raw.category || undefined, |
| 96 | + complexity: raw.complexity || undefined, |
| 97 | + industry: raw.industry || undefined, |
| 98 | + childTocItems: raw.childTocItems?.map(mapRawTocItemToTocItem), |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Reusable mapper function to convert RawTocItem to SimpleTocItem |
| 103 | +export function mapRawTocItemToSimpleTocItem(raw: RawTocItem): SimpleTocItem { |
| 104 | + return { |
| 105 | + fullPath: raw.fullPath, |
| 106 | + title: raw.title, |
| 107 | + intro: raw.intro || undefined, |
| 108 | + childTocItems: raw.childTocItems?.map((child) => ({ |
| 109 | + fullPath: child.fullPath, |
| 110 | + title: child.title, |
| 111 | + })), |
| 112 | + } |
| 113 | +} |
0 commit comments