|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +import { |
| 5 | + absoluteRoute, |
| 6 | + OutputPage, |
| 7 | + pickSeoDescription, |
| 8 | + STRAPI_BASE, |
| 9 | + strapiGet, |
| 10 | +} from './generate-llms-shared'; |
| 11 | + |
| 12 | +const OUTPUT_DIR = process.env.LLMS_PAGES_DIR || 'uxcore_/llms-full-pages'; |
| 13 | + |
| 14 | +if (!STRAPI_BASE) { |
| 15 | + console.error('[error] STRAPI_URL or NEXT_PUBLIC_STRAPI must be set in .env'); |
| 16 | + process.exit(1); |
| 17 | +} |
| 18 | + |
| 19 | +function routeSlug(route: string): string | null { |
| 20 | + const normalized = route.replace(/\/+$/, ''); |
| 21 | + const parts = normalized.split('/'); |
| 22 | + return parts[parts.length - 1] || null; |
| 23 | +} |
| 24 | + |
| 25 | +function writeSlugMarkdownFiles(pages: OutputPage[], baseDir: string): void { |
| 26 | + for (const page of pages) { |
| 27 | + if (!page.slugSection) continue; |
| 28 | + const slug = routeSlug(page.route); |
| 29 | + if (!slug) continue; |
| 30 | + |
| 31 | + const sectionDir = path.join(baseDir, page.slugSection); |
| 32 | + fs.mkdirSync(sectionDir, { recursive: true }); |
| 33 | + |
| 34 | + const content = [ |
| 35 | + `# ${page.name}`, |
| 36 | + '', |
| 37 | + `- URL: ${absoluteRoute(page.route)}`, |
| 38 | + `- Description: ${page.seoDescription ?? ''}`, |
| 39 | + '', |
| 40 | + ].join('\n'); |
| 41 | + |
| 42 | + fs.writeFileSync(path.join(sectionDir, `${slug}.md`), content, 'utf-8'); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +async function fetchUxcoreSlugPages(): Promise<OutputPage[]> { |
| 47 | + try { |
| 48 | + const data = await strapiGet( |
| 49 | + 'biases?locale=en&sort=number&pagination[pageSize]=1000&pagination[page]=1&populate[OGTags][populate]=ogImage', |
| 50 | + ); |
| 51 | + const items = Array.isArray(data?.data) ? data.data : []; |
| 52 | + return items |
| 53 | + .map((item: any) => { |
| 54 | + const attrs = item?.attributes ?? {}; |
| 55 | + const slug = attrs?.slug; |
| 56 | + if (!slug) return null; |
| 57 | + return { |
| 58 | + route: `/uxcore/${slug}`, |
| 59 | + name: String(attrs?.title ?? `UXCore ${attrs?.number ?? slug}`), |
| 60 | + seoDescription: pickSeoDescription(attrs), |
| 61 | + slugSection: 'uxcore' as const, |
| 62 | + }; |
| 63 | + }) |
| 64 | + .filter(Boolean) as OutputPage[]; |
| 65 | + } catch (err) { |
| 66 | + console.log( |
| 67 | + `[pages] skipping uxcore slugs — fetch failed: ${(err as Error).message}`, |
| 68 | + ); |
| 69 | + return []; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +async function fetchUxcgSlugPages(): Promise<OutputPage[]> { |
| 74 | + try { |
| 75 | + const data = await strapiGet( |
| 76 | + 'questions?locale=en&sort=number&pagination[pageSize]=1000&pagination[page]=1&populate[OGTags][populate]=ogImage', |
| 77 | + ); |
| 78 | + const items = Array.isArray(data?.data) ? data.data : []; |
| 79 | + return items |
| 80 | + .map((item: any) => { |
| 81 | + const attrs = item?.attributes ?? {}; |
| 82 | + const slug = attrs?.slug; |
| 83 | + if (!slug) return null; |
| 84 | + return { |
| 85 | + route: `/uxcg/${slug}`, |
| 86 | + name: String(attrs?.title ?? `UXCG ${attrs?.number ?? slug}`), |
| 87 | + seoDescription: pickSeoDescription(attrs), |
| 88 | + slugSection: 'uxcg' as const, |
| 89 | + }; |
| 90 | + }) |
| 91 | + .filter(Boolean) as OutputPage[]; |
| 92 | + } catch (err) { |
| 93 | + console.log( |
| 94 | + `[pages] skipping uxcg slugs — fetch failed: ${(err as Error).message}`, |
| 95 | + ); |
| 96 | + return []; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +async function main(): Promise<void> { |
| 101 | + console.log('=== generate-llms-pages.ts ===\n'); |
| 102 | + |
| 103 | + console.log('[step 1] Fetching all slug pages from Strapi...'); |
| 104 | + const [uxcorePages, uxcgPages] = await Promise.all([ |
| 105 | + fetchUxcoreSlugPages(), |
| 106 | + fetchUxcgSlugPages(), |
| 107 | + ]); |
| 108 | + |
| 109 | + const allPages = [...uxcorePages, ...uxcgPages]; |
| 110 | + console.log( |
| 111 | + ` found ${uxcorePages.length} uxcore + ${uxcgPages.length} uxcg = ${allPages.length} total\n`, |
| 112 | + ); |
| 113 | + |
| 114 | + console.log(`[step 2] Writing markdown files to public/${OUTPUT_DIR}...`); |
| 115 | + const baseDir = path.join(process.cwd(), 'public', OUTPUT_DIR); |
| 116 | + writeSlugMarkdownFiles(allPages, baseDir); |
| 117 | + |
| 118 | + console.log( |
| 119 | + `\nSuccessfully wrote ${allPages.length} page files to public/${OUTPUT_DIR}`, |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +main().catch(err => { |
| 124 | + console.error('\n[error] generate-llms-pages failed:', err); |
| 125 | + process.exit(1); |
| 126 | +}); |
0 commit comments