|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + */ |
| 4 | + |
| 5 | +import {Fragment, useMemo} from 'react'; |
| 6 | +import {useRouter} from 'next/router'; |
| 7 | +import {Page} from 'components/Layout/Page'; |
| 8 | +import sidebarHome from '../sidebarHome.json'; |
| 9 | +import sidebarLearn from '../sidebarLearn.json'; |
| 10 | +import sidebarReference from '../sidebarReference.json'; |
| 11 | +import sidebarCommunity from '../sidebarCommunity.json'; |
| 12 | +import sidebarBlog from '../sidebarBlog.json'; |
| 13 | +import {MDXComponents} from 'components/MDX/MDXComponents'; |
| 14 | +import compileMDX from 'utils/compileMDX'; |
| 15 | +import {generateRssFeed} from '../utils/rss'; |
| 16 | + |
| 17 | +export default function Layout({content, toc, meta, languages}) { |
| 18 | + const parsedContent = useMemo( |
| 19 | + () => JSON.parse(content, reviveNodeOnClient), |
| 20 | + [content] |
| 21 | + ); |
| 22 | + const parsedToc = useMemo(() => JSON.parse(toc, reviveNodeOnClient), [toc]); |
| 23 | + const section = useActiveSection(); |
| 24 | + let routeTree; |
| 25 | + switch (section) { |
| 26 | + case 'home': |
| 27 | + case 'unknown': |
| 28 | + routeTree = sidebarHome; |
| 29 | + break; |
| 30 | + case 'learn': |
| 31 | + routeTree = sidebarLearn; |
| 32 | + break; |
| 33 | + case 'reference': |
| 34 | + routeTree = sidebarReference; |
| 35 | + break; |
| 36 | + case 'community': |
| 37 | + routeTree = sidebarCommunity; |
| 38 | + break; |
| 39 | + case 'blog': |
| 40 | + routeTree = sidebarBlog; |
| 41 | + break; |
| 42 | + } |
| 43 | + return ( |
| 44 | + <Page |
| 45 | + toc={parsedToc} |
| 46 | + routeTree={routeTree} |
| 47 | + meta={meta} |
| 48 | + section={section} |
| 49 | + languages={languages}> |
| 50 | + {parsedContent} |
| 51 | + </Page> |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function useActiveSection() { |
| 56 | + const {asPath} = useRouter(); |
| 57 | + const cleanedPath = asPath.split(/[\?\#]/)[0]; |
| 58 | + if (cleanedPath === '/') { |
| 59 | + return 'home'; |
| 60 | + } else if (cleanedPath.startsWith('/reference')) { |
| 61 | + return 'reference'; |
| 62 | + } else if (asPath.startsWith('/learn')) { |
| 63 | + return 'learn'; |
| 64 | + } else if (asPath.startsWith('/community')) { |
| 65 | + return 'community'; |
| 66 | + } else if (asPath.startsWith('/blog')) { |
| 67 | + return 'blog'; |
| 68 | + } else { |
| 69 | + return 'unknown'; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Deserialize a client React tree from JSON. |
| 74 | +function reviveNodeOnClient(parentPropertyName, val) { |
| 75 | + if (Array.isArray(val) && val[0] == '$r') { |
| 76 | + // Assume it's a React element. |
| 77 | + let Type = val[1]; |
| 78 | + let key = val[2]; |
| 79 | + if (key == null) { |
| 80 | + key = parentPropertyName; // Index within a parent. |
| 81 | + } |
| 82 | + let props = val[3]; |
| 83 | + if (Type === 'wrapper') { |
| 84 | + Type = Fragment; |
| 85 | + props = {children: props.children}; |
| 86 | + } |
| 87 | + if (Type in MDXComponents) { |
| 88 | + Type = MDXComponents[Type]; |
| 89 | + } |
| 90 | + if (!Type) { |
| 91 | + console.error('Unknown type: ' + Type); |
| 92 | + Type = Fragment; |
| 93 | + } |
| 94 | + return <Type key={key} {...props} />; |
| 95 | + } else { |
| 96 | + return val; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Put MDX output into JSON for client. |
| 101 | +export async function getStaticProps(context) { |
| 102 | + generateRssFeed(); |
| 103 | + const fs = require('fs'); |
| 104 | + const rootDir = process.cwd() + '/src/content/'; |
| 105 | + |
| 106 | + // Read MDX from the file. |
| 107 | + let path = (context.params.markdownPath || []).join('/') || 'index'; |
| 108 | + let mdx; |
| 109 | + try { |
| 110 | + mdx = fs.readFileSync(rootDir + path + '.md', 'utf8'); |
| 111 | + } catch { |
| 112 | + mdx = fs.readFileSync(rootDir + path + '/index.md', 'utf8'); |
| 113 | + } |
| 114 | + |
| 115 | + const {toc, content, meta, languages} = await compileMDX(mdx, path, {}); |
| 116 | + return { |
| 117 | + props: { |
| 118 | + toc, |
| 119 | + content, |
| 120 | + meta, |
| 121 | + languages, |
| 122 | + }, |
| 123 | + }; |
| 124 | +} |
| 125 | + |
| 126 | +// Collect all MDX files for static generation. |
| 127 | +export async function getStaticPaths() { |
| 128 | + const {promisify} = require('util'); |
| 129 | + const {resolve} = require('path'); |
| 130 | + const fs = require('fs'); |
| 131 | + const readdir = promisify(fs.readdir); |
| 132 | + const stat = promisify(fs.stat); |
| 133 | + const rootDir = process.cwd() + '/src/content'; |
| 134 | + |
| 135 | + // Find all MD files recursively. |
| 136 | + async function getFiles(dir) { |
| 137 | + const subdirs = await readdir(dir); |
| 138 | + const files = await Promise.all( |
| 139 | + subdirs.map(async (subdir) => { |
| 140 | + const res = resolve(dir, subdir); |
| 141 | + return (await stat(res)).isDirectory() |
| 142 | + ? getFiles(res) |
| 143 | + : res.slice(rootDir.length + 1); |
| 144 | + }) |
| 145 | + ); |
| 146 | + return ( |
| 147 | + files |
| 148 | + .flat() |
| 149 | + // ignores `errors/*.md`, they will be handled by `pages/errors/[errorCode].tsx` |
| 150 | + .filter((file) => file.endsWith('.md') && !file.startsWith('errors/')) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + // 'foo/bar/baz.md' -> ['foo', 'bar', 'baz'] |
| 155 | + // 'foo/bar/qux/index.md' -> ['foo', 'bar', 'qux'] |
| 156 | + function getSegments(file) { |
| 157 | + let segments = file.slice(0, -3).replace(/\\/g, '/').split('/'); |
| 158 | + if (segments[segments.length - 1] === 'index') { |
| 159 | + segments.pop(); |
| 160 | + } |
| 161 | + return segments; |
| 162 | + } |
| 163 | + |
| 164 | + const files = await getFiles(rootDir); |
| 165 | + |
| 166 | + const paths = files.map((file) => ({ |
| 167 | + params: { |
| 168 | + markdownPath: getSegments(file), |
| 169 | + // ^^^ CAREFUL HERE. |
| 170 | + // If you rename markdownPath, update patches/next-remote-watch.patch too. |
| 171 | + // Otherwise you'll break Fast Refresh for all MD files. |
| 172 | + }, |
| 173 | + })); |
| 174 | + |
| 175 | + return { |
| 176 | + paths: paths, |
| 177 | + fallback: false, |
| 178 | + }; |
| 179 | +} |
0 commit comments