File size: 2,444 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { EditPageButton } from "@/components/edit-page-button"
import { MDXContent } from "@/components/mdx-content"
import { MDXPagination } from "@/components/mdx-pagination"
import { PageHeader } from "@/components/page-header"
import { ScrollToTop } from "@/components/scroll-to-top"
import { Toc } from "@/components/toc"
import { docsConfig } from "@/docs.config"
import { flattenToc } from "@/lib/flatten-toc"
import { Box, Show, Stack } from "@chakra-ui/react"
import { Metadata } from "next"
import { notFound } from "next/navigation"
import { SidebarEnd } from "../sidebar"
import { docs } from ".velite"

interface PageContext {
  params: Promise<{ slug: string[] }>
}

export default async function Page(props: PageContext) {
  const params = await props.params

  const page = docs.find(
    (doc) => doc.slug === ["docs", ...params.slug].join("/"),
  )

  if (!page) {
    return notFound()
  }

  return (
    <>
      <Stack
        flex="1"
        width="full"
        px={{ md: "12" }}
        pt="10"
        pb="16"
        overflow="auto"
        minHeight="var(--content-height)"
      >
        <PageHeader data={page} />
        <Box>
          <MDXContent code={page.code} />
          <MDXPagination />
        </Box>
      </Stack>

      <Show when={!page.hideToc}>
        <SidebarEnd
          id="toc"
          visibility={page.toc.length === 0 ? "hidden" : undefined}
        >
          <Toc items={flattenToc(page.toc)} />
          <Stack borderTopWidth="1px" pt="4" align="start">
            <EditPageButton href={`${docsConfig.editUrl}/${page.slug}.mdx`} />
            <ScrollToTop />
            {/* <EventBadge /> */}
          </Stack>
        </SidebarEnd>
      </Show>
    </>
  )
}

export const generateMetadata = async (ctx: PageContext): Promise<Metadata> => {
  const params = await ctx.params
  const page = docs.find(
    (doc) => doc.slug === ["docs", ...params.slug].join("/"),
  )

  const category = page?.slug
    .replace("docs/", "")
    .split("/")
    .slice(0, -1)
    .join(" > ")
    ?.replace("-", " ")
    .replace(/\b\w/g, (l: string) => l.toUpperCase())

  return {
    title: page?.title,
    description: page?.description,
    openGraph: {
      images: `/og?title=${page?.title}&category=${category}&description=${page?.description}`,
    },
  }
}

export function generateStaticParams() {
  return docs.map((item) => ({
    slug: item.slug.split("/").slice(1),
  }))
}