refactor: display readme correctly

This commit is contained in:
iCrawl
2023-11-08 14:49:28 +01:00
parent fcfe5cf142
commit 34d0224b68
7 changed files with 108 additions and 23 deletions

View File

@@ -7,7 +7,7 @@
"scripts": {
"test": "vitest run",
"test:lighthouse": "lighthouse http://localhost:3000 --output-path=./lighthouse-results",
"build:copy_readme": "cpy '../../packages/*/README.md' 'src/assets/readme' --rename='home-{{basename}}'",
"build:copy_readme": "cpy '../../packages/(discord.js|brokers|builders|collection|core|formatters|next|proxy|rest|util|voice|ws)/README.md' 'src/assets/readme' --rename='home-{{basename}}'",
"build:check": "tsc --noEmit",
"build:local": "cross-env NEXT_PUBLIC_LOCAL_DEV=true pnpm run build:prod",
"build:prod": "pnpm run build:copy_readme && pnpm run build:next",
@@ -47,10 +47,10 @@
},
"homepage": "https://discord.js.org",
"dependencies": {
"@discordjs/api-extractor-model": "workspace:^",
"@discordjs/api-extractor-utils": "workspace:^",
"@discordjs/scripts": "workspace:^",
"@discordjs/ui": "workspace:^",
"@discordjs/api-extractor-model": "workspace:^",
"@microsoft/tsdoc": "^0.14.2",
"@microsoft/tsdoc-config": "0.16.2",
"@planetscale/database": "^1.11.0",

View File

@@ -12,7 +12,7 @@ const sql = connect({
},
});
export const fetchVersions = cache(async (packageName: string) => {
export const fetchVersions = cache(async (packageName: string): Promise<string[]> => {
if (process.env.NEXT_PUBLIC_LOCAL_DEV || process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview') {
return ['main'];
}

View File

@@ -4,7 +4,6 @@ import dynamic from 'next/dynamic';
import { notFound } from 'next/navigation';
import { cache, type PropsWithChildren } from 'react';
import { fetchModelJSON, fetchVersions } from '~/app/docAPI';
// import { Banner } from '~/components/Banner';
import { CmdKDialog } from '~/components/CmdK';
import { Nav } from '~/components/Nav';
import type { SidebarSectionItemData } from '~/components/Sidebar';
@@ -77,7 +76,6 @@ export default async function PackageLayout({ children, params }: PropsWithChild
return (
<Providers>
{/* <Banner className="mb-6" /> */}
<main className="mx-auto max-w-7xl px-4 lg:max-w-full">
<Header />
<div className="relative top-2.5 mx-auto max-w-7xl gap-6 lg:max-w-full lg:flex">

View File

@@ -15,9 +15,8 @@ const loadREADME = cache(async (packageName: string) => {
const mdxOptions = {
mdxOptions: {
remarkPlugins: [remarkGfm],
remarkRehypeOptions: { allowDangerousHtml: true },
rehypePlugins: [rehypeSlug],
format: 'md',
format: 'mdx',
},
} satisfies SerializeOptions;
@@ -26,7 +25,7 @@ export default async function Page({ params }: { params: VersionRouteParams }) {
const readmeSource = await loadREADME(packageName);
return (
<div className="max-w-none prose">
<div className="relative top-4 max-w-none prose">
{/* @ts-expect-error SyntaxHighlighter is assignable */}
<MDXRemote components={{ pre: SyntaxHighlighter }} options={mdxOptions} source={readmeSource} />
</div>

View File

@@ -11,7 +11,7 @@ const sql = connect({
},
});
async function fetchLatestVersion(packageName: string) {
async function fetchLatestVersion(packageName: string): Promise<string> {
if (process.env.NEXT_PUBLIC_LOCAL_DEV || process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview') {
return 'main';
}
@@ -21,7 +21,7 @@ async function fetchLatestVersion(packageName: string) {
]);
// @ts-expect-error: https://github.com/planetscale/database-js/issues/71
return rows.map((row) => row.version).at(1);
return rows.map((row) => row.version).at(1) ?? 'main';
}
export default async function middleware(request: NextRequest) {
@@ -39,9 +39,7 @@ export default async function middleware(request: NextRequest) {
// eslint-disable-next-line prefer-named-capture-group
const packageName = /\/docs\/packages\/([^/]+)\/.*/.exec(request.nextUrl.pathname)?.[1] ?? 'discord.js';
const latestVersion = await fetchLatestVersion(packageName);
return NextResponse.redirect(
new URL(request.nextUrl.pathname.replace('stable', latestVersion ?? 'main'), request.url),
);
return NextResponse.redirect(new URL(request.nextUrl.pathname.replace('stable', latestVersion), request.url));
}
return NextResponse.redirect(new URL('/docs/packages', request.url));