πŸ“¦ zanieb / astro-blue

πŸ“„ FUTURE-CLOUDFLARE-LEARNINGS.md Β· 812 lines
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812# Suggested Improvements Based on Cloudflare Docs Learnings

## Executive Summary

After reviewing the comprehensive ASTRO_THEME_LEARNINGS.md from Cloudflare docs (95+ product sections, enterprise-scale documentation), this plan suggests enhancements to astro-blue that would benefit from their proven patterns. The suggestions are prioritized by impact and align with your current goal of matching Mintlify/PYX styling while building a robust documentation foundation.

## Current State Analysis

**What's Working:**

- βœ… CSS customization matching Mintlify/PYX visual design
- βœ… Basic Starlight component overrides (PageTitle, SiteTitle, ThemeSelect, Callout)
- βœ… Clean color system with theme variables
- βœ… Good visual design patterns (TOC, sidebar, code blocks, pagination)

**Gaps Compared to Cloudflare Scale:**

- ❌ Manual sidebar configuration (Cloudflare uses auto-generation)
- ❌ No content reuse system (Cloudflare has partials)
- ❌ No extended frontmatter schema (Cloudflare uses Zod for type safety)
- ❌ No build-time validation (images, links, content)
- ❌ No search integration (Cloudflare uses Algolia)
- ❌ No custom markdown plugins
- ❌ No LLM-ready content endpoint
- ❌ Basic content collections (just docs)

---

## Priority 1: High-Impact, Low-Effort Wins

### 1. Auto-Generated Sidebar

**Impact:** πŸ”₯πŸ”₯πŸ”₯ High - Eliminates manual sidebar maintenance forever
**Effort:** ⚑ Low - 30-minute implementation

**Current Problem:**

```javascript
// astro.config.mjs - 82 lines of manual sidebar config
sidebar: [
  {
    label: 'Getting Started',
    items: [
      { label: 'Installation', slug: 'getting-started/installation' },
      // ... manual maintenance required
    ],
  },
];
```

**Cloudflare Pattern:**

```javascript
// Auto-generate from directory structure
async function autogenSections() {
  const sections = (await readdir('./src/content/docs/'))
    .filter((x) => x.isDirectory())
    .map((x) => ({
      label: x,
      autogenerate: { directory: x, collapsed: false },
    }));
}
```

**Implementation:**

- Create `src/util/sidebar.ts` with auto-generation logic
- Import and use in `astro.config.mjs`
- Add frontmatter overrides: `sidebar.order`, `sidebar.label`, `sidebar.badge`
- Directory structure drives navigation

**Files to modify:**

- `astro.config.mjs` - Replace manual sidebar with autogen
- `src/util/sidebar.ts` (new) - Auto-generation logic

---

### 2. Extended Frontmatter Schema with Zod

**Impact:** πŸ”₯πŸ”₯ Medium - Type safety, better content organization
**Effort:** ⚑ Low - 20-minute implementation

**Benefits:**

- Build-time validation of frontmatter
- Auto-completion in editors
- Support for: tags, content_type, summary, difficulty, reviewed_date, external_link

**Cloudflare Pattern:**

```typescript
// src/schemas/base.ts
export const baseSchema = ({ image }: SchemaContext) =>
  z.object({
    preview_image: image().optional(),
    pcx_content_type: z
      .union([
        z.literal('concept'),
        z.literal('how-to'),
        z.literal('tutorial'),
        z.literal('reference'),
      ])
      .optional(),
    tags: z.string().array().optional(),
    difficulty: z
      .union([z.literal('Beginner'), z.literal('Intermediate'), z.literal('Advanced')])
      .optional(),
    summary: z.string().optional(),
  });
```

**Implementation:**

- Create `src/schemas/base.ts`
- Extend in `src/content/config.ts`
- Add useful fields: `content_type`, `tags`, `summary`, `difficulty`

**Files to modify:**

- `src/schemas/base.ts` (new)
- `src/content/config.ts` - Extend docsSchema

---

### 3. LLM-Ready Content Endpoint

**Impact:** πŸ”₯πŸ”₯ Medium - Enables AI assistants to reference docs
**Effort:** ⚑ Very Low - 10-minute implementation

**Cloudflare Pattern:**

```typescript
// src/pages/llms.txt.ts
export const GET: APIRoute = async () => {
  const docs = await getCollection('docs');
  const markdown = docs
    .map(
      (doc) => `
# ${doc.data.title}
URL: ${baseUrl}${doc.slug}

${doc.body}
  `
    )
    .join('\n\n---\n\n');

  return new Response(markdown, {
    headers: { 'content-type': 'text/markdown' },
  });
};
```

**Implementation:**

- Create `src/pages/llms.txt.ts`
- Serve all docs as structured markdown
- Add to sitemap

**Files to create:**

- `src/pages/llms.txt.ts` (new)

---

## Priority 2: Medium-Impact Improvements

### 4. Partials System for Content Reuse

**Impact:** πŸ”₯πŸ”₯ Medium - Reduces duplication, easier updates
**Effort:** ⚑⚑ Medium - 1-hour implementation

**Use Case:** Installation instructions, common warnings, version-specific notes

**Cloudflare Pattern:**

```
src/content/partials/
β”œβ”€β”€ installation/
β”‚   β”œβ”€β”€ windows.mdx
β”‚   β”œβ”€β”€ macos.mdx
β”‚   └── linux.mdx
└── common/
    └── prerequisites.mdx
```

Usage in MDX:

```mdx
import { Render } from '~/components';

<Render file="windows" product="installation" />
```

**Implementation:**

- Create partials content collection
- Create `<Render>` component
- Move reusable content to partials

**Files to modify/create:**

- `src/content/config.ts` - Add partials collection
- `src/components/Render.astro` (new)
- `src/content/partials/**/*.mdx` (new directory)

---

### 5. Build-Time Image Validation

**Impact:** πŸ”₯πŸ”₯ Medium - Catch broken images before deployment
**Effort:** ⚑⚑ Medium - 30-minute implementation

**Cloudflare Pattern:**

```typescript
// src/plugins/remark/validate-images.ts
export default function validateImages() {
  return (tree: Node, file: VFile) => {
    visit(tree, 'image', (node: ImageNode) => {
      if (!existsSync(fullPath)) {
        throw new Error(`Image not found: "${url}"`);
      }
    });
  };
}
```

**Implementation:**

- Create remark plugin for image validation
- Add to markdown config in astro.config.mjs

**Files to create:**

- `src/plugins/remark/validate-images.ts` (new)
- Update `astro.config.mjs` - Add to markdown.remarkPlugins

---

### 6. Path Aliases for Cleaner Imports

**Impact:** πŸ”₯ Low - Better DX, cleaner imports
**Effort:** ⚑ Very Low - 5-minute implementation

**Current:**

```typescript
import { Callout } from '../../../components/Callout.astro';
```

**With aliases:**

```typescript
import { Callout } from '~/components/Callout.astro';
```

**Implementation:**

- Create `tsconfig.json` with path mapping
- All Cloudflare docs use `~/*` pattern

**Files to create:**

- `tsconfig.json` (new)

---

## Priority 3: Advanced Features (Future Consideration)

### 7. Algolia DocSearch Integration

**Impact:** πŸ”₯πŸ”₯πŸ”₯ High - Professional search experience
**Effort:** ⚑⚑⚑ High - Requires Algolia setup + API key
**When:** After content is more mature

**Cloudflare Pattern:**

- Header search widget (Cmd+K)
- Full search page with facets
- Meta tags for faceted search

**Prerequisites:**

- Algolia account
- DocSearch crawler setup
- Meta tag implementation

---

### 8. Multiple Content Collections

**Impact:** πŸ”₯ Low - Better organization for different content types
**Effort:** ⚑⚑ Medium
**When:** When you have distinct content types beyond docs

**Examples from Cloudflare:**

```typescript
export const collections = {
  docs: defineCollection({ schema: docsSchema }),
  guides: defineCollection({ schema: guideSchema }),
  changelog: defineCollection({ schema: changelogSchema }),
  glossary: defineCollection({ schema: glossarySchema }),
};
```

---

### 9. Custom Markdown Plugins

**Impact:** πŸ”₯ Low-Medium - Enhanced markdown capabilities
**Effort:** ⚑⚑⚑ High
**When:** When you need special markdown features

**Cloudflare Examples:**

- External links plugin (adds β†—)
- Mermaid diagram support
- Custom heading IDs `# foo {/*bar*/}`
- Image validation

---

### 10. Component Barrel Exports

**Impact:** πŸ”₯ Low - Cleaner imports
**Effort:** ⚑ Low

**Cloudflare Pattern:**

```typescript
// src/components/index.ts
export * from '@astrojs/starlight/components';
export { default as Callout } from './Callout.astro';
export { default as Render } from './Render.astro';
// ... 80+ component exports
```

Usage:

```astro
---
import { Callout, Render, Card } from '~/components';
---
```

---

## Recommended Implementation Order

### Week 1: Foundation Improvements

1. βœ… Auto-generated sidebar (30 min)
2. βœ… Path aliases / tsconfig.json (5 min)
3. βœ… LLM-ready content endpoint (10 min)
4. βœ… Extended frontmatter schema (20 min)

**Total Time:** ~1 hour
**Value:** Eliminates manual sidebar maintenance, adds type safety, enables AI integration

### Week 2: Content Quality

5. βœ… Partials system (1 hour)
6. βœ… Build-time image validation (30 min)
7. βœ… Component barrel exports (15 min)

**Total Time:** ~2 hours
**Value:** Reduces duplication, catches errors early, cleaner imports

### Future: Advanced Features

8. Consider Algolia search (when content is mature)
9. Consider additional content collections (when needed)
10. Consider custom markdown plugins (based on content needs)

---

## User Preferences (Confirmed)

βœ… **Auto-generated sidebar** - Switch from manual to directory-based
βœ… **Extended frontmatter schema** - Implement with Zod for type safety
βœ… **Partials system** - Create reusable content components
βœ… **LLM-ready endpoint** - Enable AI assistant integration
⏸️ **Implementation timing** - Defer until ready (focus on visual styling first)

---

## Files That Would Be Modified/Created

**Modified:**

- `astro.config.mjs` - Auto-sidebar, remark plugins
- `src/content/config.ts` - Extended schema

**New Files:**

- `tsconfig.json` - Path aliases
- `src/util/sidebar.ts` - Auto-generation logic
- `src/schemas/base.ts` - Extended frontmatter schema
- `src/pages/llms.txt.ts` - LLM endpoint
- `src/components/Render.astro` - Partials component
- `src/components/index.ts` - Barrel exports
- `src/plugins/remark/validate-images.ts` - Image validation
- `src/content/partials/` - Partials directory

**No Breaking Changes:** All improvements are additive and maintain backward compatibility with existing content.

---

## Detailed Implementation Guide

When ready to implement, follow these steps in order:

### Step 1: TypeScript Configuration & Path Aliases (5 min)

Create `tsconfig.json`:

```json
{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "~/*": ["src/*"]
    },
    "jsx": "react-jsx",
    "jsxImportSource": "react"
  }
}
```

**Benefit:** Enables `import { Callout } from '~/components/Callout.astro'` instead of relative paths.

---

### Step 2: Auto-Generated Sidebar (30 min)

**2.1** Create `src/util/sidebar.ts`:

```typescript
import { readdir } from 'node:fs/promises';
import { join } from 'node:path';

export async function generateSidebar() {
  const docsDir = join(process.cwd(), 'src/content/docs');
  const entries = await readdir(docsDir, { withFileTypes: true });

  const sections = entries
    .filter((entry) => entry.isDirectory())
    .map((entry) => ({
      label: formatLabel(entry.name),
      autogenerate: {
        directory: entry.name,
        collapsed: false,
      },
    }));

  return sections;
}

function formatLabel(dirName: string): string {
  return dirName
    .split('-')
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
}
```

**2.2** Update `astro.config.mjs`:

```javascript
import { generateSidebar } from './src/util/sidebar';

export default defineConfig({
  integrations: [
    starlight({
      // ... other config
      sidebar: await generateSidebar(),
    }),
  ],
});
```

**2.3** Add frontmatter overrides to control ordering:

```mdx
---
title: Installation
sidebar:
  order: 1
  label: Quick Start
  badge:
    text: New
    variant: tip
---
```

**Result:** Sidebar automatically reflects directory structure. Add/remove files without touching config.

---

### Step 3: Extended Frontmatter Schema (20 min)

**3.1** Create `src/schemas/base.ts`:

```typescript
import { z } from 'astro:schema';

export const extendedSchema = z.object({
  // Content classification
  content_type: z
    .enum(['concept', 'how-to', 'tutorial', 'reference', 'troubleshooting', 'changelog'])
    .optional(),

  // Organization
  tags: z.array(z.string()).optional(),

  // SEO and display
  summary: z.string().optional(),

  // User guidance
  difficulty: z.enum(['Beginner', 'Intermediate', 'Advanced']).optional(),

  // Metadata
  reviewed: z.date().optional(),
  deprecated: z.boolean().default(false),

  // External references
  external_link: z.string().url().optional(),

  // Display control
  hide_title: z.boolean().default(false),
  hide_toc: z.boolean().default(false),
});
```

**3.2** Update `src/content/config.ts`:

```typescript
import { defineCollection } from 'astro:content';
import { docsLoader } from '@astrojs/starlight/loaders';
import { docsSchema } from '@astrojs/starlight/schema';
import { extendedSchema } from '../schemas/base';

export const collections = {
  docs: defineCollection({
    loader: docsLoader(),
    schema: docsSchema({
      extend: extendedSchema,
    }),
  }),
};
```

**3.3** Example usage in MDX:

```mdx
---
title: Docker Integration
content_type: how-to
difficulty: Intermediate
tags: [docker, deployment, containers]
summary: Learn how to deploy Astro Blue using Docker containers
reviewed: 2024-01-15
---
```

**Result:** Type-safe frontmatter with auto-completion and build-time validation.

---

### Step 4: Partials System (1 hour)

**4.1** Update `src/content/config.ts`:

```typescript
export const collections = {
  docs: defineCollection({
    loader: docsLoader(),
    schema: docsSchema({ extend: extendedSchema }),
  }),
  partials: defineCollection({
    loader: docsLoader(),
    schema: docsSchema(),
  }),
};
```

**4.2** Create `src/components/Render.astro`:

```astro
---
import { getEntry } from 'astro:content';
import { z } from 'astro:schema';

const props = z
  .object({
    file: z.string(),
    category: z.string().default('common'),
    params: z.record(z.string(), z.any()).optional(),
  })
  .parse(Astro.props);

const { file, category, params } = props;
const partialId = `${category}/${file}`;

const partial = await getEntry('partials', partialId);

if (!partial) {
  throw new Error(`[Render] Partial not found: "${partialId}" (included in ${Astro.url.pathname})`);
}

const { Content } = await partial.render();
---

<Content {...params || {}} />
```

**4.3** Create partials directory structure:

```
src/content/partials/
β”œβ”€β”€ common/
β”‚   β”œβ”€β”€ prerequisites.mdx
β”‚   └── troubleshooting-tips.mdx
β”œβ”€β”€ installation/
β”‚   β”œβ”€β”€ windows.mdx
β”‚   β”œβ”€β”€ macos.mdx
β”‚   └── linux.mdx
└── deployment/
    β”œβ”€β”€ vercel.mdx
    └── cloudflare.mdx
```

**4.4** Example partial (`src/content/partials/installation/macos.mdx`):

````mdx
---
title: macOS Installation
---

### Prerequisites

- macOS 10.15 or later
- Homebrew (recommended)

### Installation Steps

1. Install via Homebrew:
   ```bash
   brew install astro-blue
   ```
````

2. Verify installation:
   ```bash
   astro-blue --version
   ```

````

**4.5** Use in docs:
```mdx
---
title: Installation Guide
---

import { Render } from '~/components/Render.astro';

## Platform-Specific Instructions

### macOS
<Render file="macos" category="installation" />

### Windows
<Render file="windows" category="installation" />

## Common Prerequisites
<Render file="prerequisites" category="common" />
````

**Result:** Reusable content components. Update once, reflect everywhere.

---

### Step 5: LLM-Ready Content Endpoint (10 min)

**5.1** Create `src/pages/llms.txt.ts`:

```typescript
import type { APIRoute } from 'astro';
import { getCollection } from 'astro:content';

export const GET: APIRoute = async () => {
  const docs = await getCollection('docs');

  const baseUrl = 'https://astro-blue.com'; // Replace with your domain

  const markdown = docs
    .map((doc) => {
      const frontmatterFields = [
        doc.data.content_type && `Type: ${doc.data.content_type}`,
        doc.data.tags?.length && `Tags: ${doc.data.tags.join(', ')}`,
        doc.data.difficulty && `Difficulty: ${doc.data.difficulty}`,
        doc.data.summary && `Summary: ${doc.data.summary}`,
      ]
        .filter(Boolean)
        .join('\n');

      return `# ${doc.data.title}
URL: ${baseUrl}/${doc.slug}
${frontmatterFields ? '\n' + frontmatterFields + '\n' : ''}
${doc.body}`;
    })
    .join('\n\n---\n\n');

  return new Response(markdown, {
    headers: {
      'content-type': 'text/markdown; charset=utf-8',
      'cache-control': 'public, max-age=3600',
    },
  });
};
```

**5.2** Test endpoint:

```bash
curl http://localhost:4321/llms.txt
```

**5.3** (Optional) Add to robots.txt:

```
User-agent: *
Allow: /llms.txt

# AI crawlers
User-agent: GPTBot
User-agent: ChatGPT-User
Allow: /llms.txt
```

**Result:** AI assistants can access your docs at `https://yourdomain.com/llms.txt`

---

### Step 6: Component Barrel Exports (Optional, 15 min)

**6.1** Create `src/components/index.ts`:

```typescript
// Re-export all Starlight components
export * from '@astrojs/starlight/components';

// Export custom components
export { default as Callout } from './Callout.astro';
export { default as PageTitle } from './PageTitle.astro';
export { default as SiteTitle } from './SiteTitle.astro';
export { default as ThemeSelect } from './ThemeSelect.astro';
export { default as Render } from './Render.astro';
```

**6.2** Use in MDX:

```mdx
---
import { Card, CardGrid, Callout, Render } from '~/components';
---

<Callout type="tip">This is much cleaner than individual imports!</Callout>

<Render file="prerequisites" category="common" />
```

**Result:** Single import for all components.

---

## Testing Checklist

After implementing each feature:

- [ ] **Auto-sidebar:** Navigate to all pages, verify sidebar structure matches directories
- [ ] **Extended schema:** Add frontmatter to a page, verify auto-completion works
- [ ] **Partials:** Create a partial, render it on 2+ pages, verify updates reflect everywhere
- [ ] **LLM endpoint:** Visit `/llms.txt`, verify markdown is properly formatted
- [ ] **Build:** Run `npm run build` - should complete without errors
- [ ] **Type-check:** If using TypeScript, verify no type errors

---

## Migration Notes

**Sidebar Migration:**

- Current 82-line manual sidebar β†’ ~10-line auto-generated config
- Directory structure becomes the source of truth
- Use frontmatter for custom ordering/labeling

**Content Migration:**

- No changes required to existing content
- Gradually add extended frontmatter fields as needed
- Identify duplicated content β†’ move to partials over time

**Breaking Changes:**

- None - all features are additive
- Existing content continues to work without modification