|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import type {GetServerSideProps} from 'next'; |
| 9 | +import {siteConfig} from '../siteConfig'; |
| 10 | +import sidebarLearn from '../sidebarLearn.json'; |
| 11 | +import sidebarReference from '../sidebarReference.json'; |
| 12 | +import sidebarBlog from '../sidebarBlog.json'; |
| 13 | + |
| 14 | +interface RouteItem { |
| 15 | + title?: string; |
| 16 | + path?: string; |
| 17 | + routes?: RouteItem[]; |
| 18 | +} |
| 19 | + |
| 20 | +interface Sidebar { |
| 21 | + title: string; |
| 22 | + routes: RouteItem[]; |
| 23 | +} |
| 24 | + |
| 25 | +function extractRoutes( |
| 26 | + routes: RouteItem[], |
| 27 | + baseUrl: string |
| 28 | +): {title: string; url: string}[] { |
| 29 | + const result: {title: string; url: string}[] = []; |
| 30 | + |
| 31 | + for (const route of routes) { |
| 32 | + if (route.title && route.path) { |
| 33 | + result.push({ |
| 34 | + title: route.title, |
| 35 | + url: `${baseUrl}${route.path}.md`, |
| 36 | + }); |
| 37 | + } |
| 38 | + if (route.routes) { |
| 39 | + result.push(...extractRoutes(route.routes, baseUrl)); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return result; |
| 44 | +} |
| 45 | + |
| 46 | +const sidebars: Sidebar[] = [ |
| 47 | + sidebarLearn as Sidebar, |
| 48 | + sidebarReference as Sidebar, |
| 49 | + sidebarBlog as Sidebar, |
| 50 | +]; |
| 51 | + |
| 52 | +export const getServerSideProps: GetServerSideProps = async ({res}) => { |
| 53 | + const subdomain = |
| 54 | + siteConfig.languageCode === 'en' ? '' : siteConfig.languageCode + '.'; |
| 55 | + const baseUrl = 'https://' + subdomain + 'react.dev'; |
| 56 | + |
| 57 | + const lines = [ |
| 58 | + '# React Documentation', |
| 59 | + '', |
| 60 | + '> The library for web and native user interfaces.', |
| 61 | + ]; |
| 62 | + |
| 63 | + for (const sidebar of sidebars) { |
| 64 | + lines.push(''); |
| 65 | + lines.push(`## ${sidebar.title}`); |
| 66 | + lines.push(''); |
| 67 | + |
| 68 | + const routes = extractRoutes(sidebar.routes, baseUrl); |
| 69 | + for (const route of routes) { |
| 70 | + lines.push(`- [${route.title}](${route.url})`); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + const content = lines.join('\n'); |
| 75 | + |
| 76 | + res.setHeader('Content-Type', 'text/plain; charset=utf-8'); |
| 77 | + res.write(content); |
| 78 | + res.end(); |
| 79 | + |
| 80 | + return {props: {}}; |
| 81 | +}; |
| 82 | + |
| 83 | +export default function LlmsTxt() { |
| 84 | + return null; |
| 85 | +} |
0 commit comments