|
| 1 | +import { RemoteContent } from 'nextra/data' |
| 2 | +import { buildDynamicMDX, buildDynamicMeta } from 'nextra/remote' |
| 3 | +import { pluginsConfig } from '../../remote/pluginsConfig' |
| 4 | + |
| 5 | +export async function getStaticProps({ params }) { |
| 6 | + |
| 7 | + const pluginBranch = params.pluginBranchSlug?.join('/') || null |
| 8 | + const branchNamePath = pluginsConfig.branches.find(filePath => filePath.replace(/\.mdx?/, '') === pluginBranch) |
| 9 | + const branchName = branchNamePath?.replace(/\.mdx?/, '')?.replace?.('plugins/', '') |
| 10 | + let pageMarkdown = '# 404 | Plugin Not Found\n\nThe page may not exist, or a remote MDX rate limit may have triggered' |
| 11 | + try { |
| 12 | + if (branchName) { |
| 13 | + const githubApiBranchPRsUrl = `https://api.github.com/repos/Aetherspace/universal-app-router/pulls?head=Aetherspace:with/${branchName}` |
| 14 | + const response = await fetch(githubApiBranchPRsUrl) |
| 15 | + const branchPRs = await response.json() |
| 16 | + const branchPR = branchPRs[0] |
| 17 | + const branchPRMarkdown = branchPR.body |
| 18 | + const branchPRTitle = branchPR.title |
| 19 | + pageMarkdown = branchPRMarkdown.split('\n').reduce((acc, line) => { |
| 20 | + let newLine = line |
| 21 | + if (line.includes('<img ') && line.includes('">')) newLine = newLine.replace('">', '" />') |
| 22 | + return acc + newLine + '\n' |
| 23 | + }, `# \`${branchPRTitle}\`\n\n`) |
| 24 | + } |
| 25 | + } catch (error) { |
| 26 | + if (!error) console.error(error) |
| 27 | + } |
| 28 | + const dynamicMDX = await buildDynamicMDX(pageMarkdown, { defaultShowCopyCode: true }) |
| 29 | + const dynamicMeta = await buildDynamicMeta() |
| 30 | + return { |
| 31 | + props: { |
| 32 | + ...dynamicMeta, |
| 33 | + ...dynamicMDX, |
| 34 | + }, |
| 35 | + revalidate: false, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export const getStaticPaths = () => ({ |
| 40 | + fallback: 'blocking', |
| 41 | + paths: pluginsConfig.branches.map(filePath => ({ |
| 42 | + params: { pluginBranchSlug: filePath.replace(/\.mdx?$/, '').split('/') } |
| 43 | + })) |
| 44 | +}) |
| 45 | + |
| 46 | +<RemoteContent /> |
0 commit comments