|
| 1 | +import type { APIRoute } from 'astro'; |
| 2 | +import { getCollection } from 'astro:content'; |
| 3 | +import micromatch from 'micromatch'; |
| 4 | + |
| 5 | +const BASE_URL = 'https://www.pgflow.dev'; |
| 6 | + |
| 7 | +const TOPIC_ORDER = [ |
| 8 | + 'get-started', |
| 9 | + 'build', |
| 10 | + 'deploy', |
| 11 | + 'concepts', |
| 12 | + 'reference', |
| 13 | + 'tutorials', |
| 14 | + 'comparisons', |
| 15 | +]; |
| 16 | + |
| 17 | +const TOPIC_LABELS: Record<string, string> = { |
| 18 | + 'get-started': 'Get Started', |
| 19 | + build: 'Build', |
| 20 | + deploy: 'Deploy', |
| 21 | + concepts: 'Concepts', |
| 22 | + reference: 'Reference', |
| 23 | + tutorials: 'Tutorials', |
| 24 | + comparisons: 'Comparisons', |
| 25 | +}; |
| 26 | + |
| 27 | +const DEPRIORITIZED_PATTERNS = [ |
| 28 | + 'reference/manual-installation', |
| 29 | + 'deploy/connection-string', |
| 30 | + 'deploy/prune-records', |
| 31 | + 'deploy/troubleshooting-connections', |
| 32 | + 'reference/queue-worker/*', |
| 33 | + 'comparisons/*', |
| 34 | +]; |
| 35 | + |
| 36 | +function isDeprioritized(slug: string, patterns: string[]): boolean { |
| 37 | + return micromatch.isMatch(slug, patterns); |
| 38 | +} |
| 39 | + |
| 40 | +function getTopicFromSlug(slug: string): string { |
| 41 | + const parts = slug.split('/'); |
| 42 | + return parts[0] || ''; |
| 43 | +} |
| 44 | + |
| 45 | +function getDocPath(slug: string): string { |
| 46 | + return `${BASE_URL}/${slug}/index.md`; |
| 47 | +} |
| 48 | + |
| 49 | +export const GET: APIRoute = async () => { |
| 50 | + const docs = await getCollection('docs'); |
| 51 | + |
| 52 | + const prioritizedDocs = docs.filter( |
| 53 | + (doc) => !isDeprioritized(doc.id, DEPRIORITIZED_PATTERNS) |
| 54 | + ); |
| 55 | + const deprioritizedDocs = docs.filter((doc) => |
| 56 | + isDeprioritized(doc.id, DEPRIORITIZED_PATTERNS) |
| 57 | + ); |
| 58 | + |
| 59 | + const groupedByTopic: Record<string, typeof docs> = {}; |
| 60 | + for (const doc of prioritizedDocs) { |
| 61 | + const topic = getTopicFromSlug(doc.id); |
| 62 | + if (!groupedByTopic[topic]) { |
| 63 | + groupedByTopic[topic] = []; |
| 64 | + } |
| 65 | + groupedByTopic[topic].push(doc); |
| 66 | + } |
| 67 | + |
| 68 | + const lines: string[] = [ |
| 69 | + '# pgflow Documentation', |
| 70 | + '', |
| 71 | + 'All links below are raw markdown files:', |
| 72 | + '', |
| 73 | + '```bash', |
| 74 | + 'curl -s https://www.pgflow.dev/get-started/installation/index.md', |
| 75 | + '```', |
| 76 | + '', |
| 77 | + ]; |
| 78 | + |
| 79 | + for (const topicId of TOPIC_ORDER) { |
| 80 | + const topicDocs = groupedByTopic[topicId]; |
| 81 | + if (!topicDocs || topicDocs.length === 0) continue; |
| 82 | + |
| 83 | + const topicLabel = TOPIC_LABELS[topicId] || topicId; |
| 84 | + lines.push(`## ${topicLabel}`); |
| 85 | + lines.push(''); |
| 86 | + |
| 87 | + const sortedDocs = [...topicDocs].sort((a, b) => { |
| 88 | + const aOrder = a.data.sidebar?.order ?? 999; |
| 89 | + const bOrder = b.data.sidebar?.order ?? 999; |
| 90 | + return aOrder - bOrder; |
| 91 | + }); |
| 92 | + |
| 93 | + for (const doc of sortedDocs) { |
| 94 | + const title = doc.data.title; |
| 95 | + const description = doc.data.description || ''; |
| 96 | + const path = getDocPath(doc.id); |
| 97 | + const line = description |
| 98 | + ? `- [${title}](${path}) - ${description}` |
| 99 | + : `- [${title}](${path})`; |
| 100 | + lines.push(line); |
| 101 | + } |
| 102 | + lines.push(''); |
| 103 | + } |
| 104 | + |
| 105 | + if (deprioritizedDocs.length > 0) { |
| 106 | + lines.push('---'); |
| 107 | + lines.push(''); |
| 108 | + lines.push('## Niche / Advanced'); |
| 109 | + lines.push(''); |
| 110 | + lines.push( |
| 111 | + '> Only use these if you know what you are doing or have specific needs.' |
| 112 | + ); |
| 113 | + lines.push(''); |
| 114 | + |
| 115 | + const sortedDeprioritized = [...deprioritizedDocs].sort((a, b) => { |
| 116 | + const aOrder = a.data.sidebar?.order ?? 999; |
| 117 | + const bOrder = b.data.sidebar?.order ?? 999; |
| 118 | + return aOrder - bOrder; |
| 119 | + }); |
| 120 | + |
| 121 | + for (const doc of sortedDeprioritized) { |
| 122 | + const title = doc.data.title; |
| 123 | + const path = getDocPath(doc.id); |
| 124 | + lines.push(`- [${title}](${path})`); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return new Response(lines.join('\n'), { |
| 129 | + headers: { |
| 130 | + 'Content-Type': 'text/markdown; charset=utf-8', |
| 131 | + }, |
| 132 | + }); |
| 133 | +}; |
0 commit comments