|
| 1 | +import fs from 'fs/promises'; |
| 2 | +import path from 'path'; |
| 3 | +import {Page} from 'components/Layout/Page'; |
| 4 | +import sidebarHome from '../../sidebarHome.json'; |
| 5 | +import sidebarLearn from '../../sidebarLearn.json'; |
| 6 | +import sidebarReference from '../../sidebarReference.json'; |
| 7 | +import sidebarCommunity from '../../sidebarCommunity.json'; |
| 8 | +import sidebarBlog from '../../sidebarBlog.json'; |
| 9 | +import {generateMDX} from '../../utils/generateMDX'; |
| 10 | + |
| 11 | +import {generateMetadata as generateSeoMetadata} from '../../utils/generateMetadata'; |
| 12 | + |
| 13 | +import {getRouteMeta, RouteItem} from 'components/Layout/getRouteMeta'; |
| 14 | +import {LanguageItem} from 'components/MDX/LanguagesContext'; |
| 15 | +import {cache} from 'react'; |
| 16 | + |
| 17 | +function getActiveSection(pathname: string) { |
| 18 | + if (pathname === '/') { |
| 19 | + return 'home'; |
| 20 | + } else if (pathname.startsWith('/reference')) { |
| 21 | + return 'reference'; |
| 22 | + } else if (pathname.startsWith('/learn')) { |
| 23 | + return 'learn'; |
| 24 | + } else if (pathname.startsWith('/community')) { |
| 25 | + return 'community'; |
| 26 | + } else if (pathname.startsWith('/blog')) { |
| 27 | + return 'blog'; |
| 28 | + } else { |
| 29 | + return 'unknown'; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +async function getRouteTree(section: string) { |
| 34 | + switch (section) { |
| 35 | + case 'home': |
| 36 | + case 'unknown': |
| 37 | + return sidebarHome; |
| 38 | + case 'learn': |
| 39 | + return sidebarLearn; |
| 40 | + case 'reference': |
| 41 | + return sidebarReference; |
| 42 | + case 'community': |
| 43 | + return sidebarCommunity; |
| 44 | + case 'blog': |
| 45 | + return sidebarBlog; |
| 46 | + default: |
| 47 | + throw new Error(`Unknown section: ${section}`); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +const getPageContent = cache(async function getPageContent( |
| 52 | + markdownPath: any[] |
| 53 | +) { |
| 54 | + const rootDir = path.join(process.cwd(), 'src/content'); |
| 55 | + let mdxPath = markdownPath?.join('/') || 'index'; |
| 56 | + let mdx; |
| 57 | + |
| 58 | + try { |
| 59 | + mdx = await fs.readFile(path.join(rootDir, mdxPath + '.md'), 'utf8'); |
| 60 | + } catch { |
| 61 | + mdx = await fs.readFile(path.join(rootDir, mdxPath, 'index.md'), 'utf8'); |
| 62 | + } |
| 63 | + |
| 64 | + return await generateMDX(mdx, mdxPath, {}); |
| 65 | +}); |
| 66 | + |
| 67 | +// This replaces getStaticPaths |
| 68 | +export async function generateStaticParams() { |
| 69 | + const rootDir = path.join(process.cwd(), 'src/content'); |
| 70 | + |
| 71 | + async function getFiles(dir: string): Promise<string[]> { |
| 72 | + const entries = await fs.readdir(dir, {withFileTypes: true}); |
| 73 | + const files = await Promise.all( |
| 74 | + entries.map(async (entry) => { |
| 75 | + const res = path.resolve(dir, entry.name); |
| 76 | + return entry.isDirectory() |
| 77 | + ? getFiles(res) |
| 78 | + : res.slice(rootDir.length + 1); |
| 79 | + }) |
| 80 | + ); |
| 81 | + |
| 82 | + return files |
| 83 | + .flat() |
| 84 | + .filter( |
| 85 | + (file: string) => file.endsWith('.md') && !file.startsWith('errors/') |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + function getSegments(file: string) { |
| 90 | + let segments = file.slice(0, -3).replace(/\\/g, '/').split('/'); |
| 91 | + if (segments[segments.length - 1] === 'index') { |
| 92 | + segments.pop(); |
| 93 | + } |
| 94 | + return segments; |
| 95 | + } |
| 96 | + |
| 97 | + const files = await getFiles(rootDir); |
| 98 | + |
| 99 | + return files.map((file: any) => ({ |
| 100 | + markdownPath: getSegments(file), |
| 101 | + })); |
| 102 | +} |
| 103 | + |
| 104 | +export default async function WrapperPage({ |
| 105 | + params, |
| 106 | +}: { |
| 107 | + params: Promise<{markdownPath: string[]}>; |
| 108 | +}) { |
| 109 | + const {markdownPath} = await params; |
| 110 | + |
| 111 | + // Get the MDX content and associated data |
| 112 | + const {content, toc, meta} = await getPageContent(markdownPath); |
| 113 | + |
| 114 | + const pathname = '/' + (markdownPath?.join('/') || ''); |
| 115 | + const section = getActiveSection(pathname); |
| 116 | + const routeTree = await getRouteTree(section); |
| 117 | + |
| 118 | + // Load the list of translated languages conditionally. |
| 119 | + let languages: Array<LanguageItem> | null = null; |
| 120 | + if (pathname.endsWith('/translations')) { |
| 121 | + languages = await ( |
| 122 | + await fetch( |
| 123 | + 'https://raw.githubusercontent.com/reactjs/translations.react.dev/main/langs/langs.json' |
| 124 | + ) |
| 125 | + ).json(); // { code: string; name: string; enName: string}[] |
| 126 | + } |
| 127 | + |
| 128 | + // Pass the content and TOC directly, as `getPageContent` should already return them in the correct format |
| 129 | + return ( |
| 130 | + <Page |
| 131 | + toc={toc} // Pass the TOC directly without parsing |
| 132 | + routeTree={routeTree as RouteItem} |
| 133 | + meta={meta} |
| 134 | + section={section} |
| 135 | + pathname={pathname} |
| 136 | + languages={languages}> |
| 137 | + {content} |
| 138 | + </Page> |
| 139 | + ); |
| 140 | +} |
| 141 | +// Configure dynamic segments to be statically generated |
| 142 | +export const dynamicParams = false; |
| 143 | + |
| 144 | +export async function generateMetadata({ |
| 145 | + params, |
| 146 | +}: { |
| 147 | + params: Promise<{markdownPath: string[]}>; |
| 148 | +}) { |
| 149 | + const {markdownPath} = await params; |
| 150 | + const pathname = '/' + (markdownPath?.join('/') || ''); |
| 151 | + const section = getActiveSection(pathname); |
| 152 | + const routeTree = await getRouteTree(section); |
| 153 | + const {route, order} = getRouteMeta(pathname, routeTree as RouteItem); |
| 154 | + const { |
| 155 | + title = route?.title || '', |
| 156 | + description = route?.description || '', |
| 157 | + titleForTitleTag, |
| 158 | + } = await getPageContent(markdownPath).then(({meta}) => meta); |
| 159 | + |
| 160 | + return generateSeoMetadata({ |
| 161 | + title, |
| 162 | + isHomePage: pathname === '/', |
| 163 | + path: pathname, |
| 164 | + description, |
| 165 | + titleForTitleTag, |
| 166 | + image: `/images/og-${section}.png`, |
| 167 | + searchOrder: |
| 168 | + section === 'learn' || (section === 'blog' && pathname !== '/blog') |
| 169 | + ? order |
| 170 | + : undefined, |
| 171 | + }); |
| 172 | +} |
0 commit comments