spaCy/website/pages/[...listPathPage].tsx

133 lines
4.0 KiB
TypeScript
Raw Normal View History

import type { GetStaticPaths, GetStaticProps } from 'next'
import { serialize } from 'next-mdx-remote/serialize'
import fs from 'fs'
import { MDXRemote, MDXRemoteSerializeResult } from 'next-mdx-remote'
import path from 'path'
2022-11-14 03:44:12 +03:00
import Layout from '../src/templates'
2022-11-13 18:31:26 +03:00
import remarkPlugins from '../plugins/index.mjs'
2022-11-16 23:05:53 +03:00
import recordSection from '../meta/recordSections'
type ApiDetails = {
stringName: string | null
baseClass: {
title: string
slug: string
} | null
trainable: string | null
}
2022-11-15 03:49:53 +03:00
export type PropsPageBase = {
2022-11-16 23:05:53 +03:00
/**
* TODO: This is only here for legacy support of the old code base
* It should be refactort to pass the file path and page path instead.
*/
slug: string
sectionTitle: string | null
theme: string | null
section: string
isIndex: boolean
}
2022-11-15 03:49:53 +03:00
export type PropsPage = PropsPageBase & {
mdx: MDXRemoteSerializeResult
apiDetails: ApiDetails
}
2022-11-16 23:05:53 +03:00
const PostPage = ({ mdx: mdx, ...props }: PropsPage) => {
return (
2022-11-16 23:05:53 +03:00
<Layout {...props}>
<MDXRemote {...mdx} />
</Layout>
)
}
export default PostPage
type ParsedUrlQuery = {
2022-11-16 22:39:02 +03:00
listPathPage: Array<string>
}
export const getStaticPaths: GetStaticPaths<ParsedUrlQuery> = async () => {
// This function needs to be defined inside `getStaticPath` to be executed in executed in the correct context
2022-11-16 22:39:02 +03:00
const loadFolder = (pathBase: Array<string> = []): Array<{ params: ParsedUrlQuery }> =>
fs
2022-11-16 22:39:02 +03:00
.readdirSync(path.join('docs', ...pathBase), { withFileTypes: true })
.flatMap((dirent: fs.Dirent) => {
if (dirent.isDirectory()) {
2022-11-16 22:39:02 +03:00
return loadFolder([...pathBase, dirent.name])
}
if (!dirent.name.includes('.mdx')) {
return []
}
return {
params: {
listPathPage:
dirent.name === 'index.mdx'
? pathBase
: [...pathBase, dirent.name.replace('.mdx', '')],
},
}
})
return {
paths: loadFolder(),
fallback: false,
}
}
const getPathFileWithExtension = (listPathFile: ReadonlyArray<string>) =>
`${path.join(...listPathFile)}.mdx`
export const getStaticProps: GetStaticProps<PropsPage, ParsedUrlQuery> = async (args) => {
if (!args.params) {
return { notFound: true }
}
2022-11-16 22:48:13 +03:00
const listPathFile = ['docs', ...args.params.listPathPage]
const isIndex = fs.existsSync(getPathFileWithExtension(listPathFile)) !== true
const listPathFileWithIndex = isIndex ? [...listPathFile, 'index'] : listPathFile
2022-11-16 23:05:53 +03:00
const pathFileWithIndexAndExtension = getPathFileWithExtension(listPathFileWithIndex)
2022-11-16 23:05:53 +03:00
const mdx = await serialize(fs.readFileSync(pathFileWithIndexAndExtension, 'utf-8'), {
parseFrontmatter: true,
2022-11-25 06:43:07 +03:00
mdxOptions: { remarkPlugins },
})
2022-11-16 22:48:13 +03:00
2022-11-16 23:05:53 +03:00
if (!mdx.frontmatter) {
throw new Error(`Frontmatter missing for ${pathFileWithIndexAndExtension}`)
}
const parentFolder =
listPathFileWithIndex.length > 1
? listPathFileWithIndex[listPathFileWithIndex.length - 2]
: null
const section = mdx.frontmatter.section ?? parentFolder
const sectionMeta = section ? recordSection[section] ?? null : null
const baseClass = null
const apiDetails: ApiDetails = {
stringName: mdx.frontmatter.api_string_name ?? null,
baseClass: baseClass
? {
title: mdx.frontmatter.title,
slug: mdx.frontmatter.api_base_class,
}
: null,
trainable: mdx.frontmatter.api_trainable ?? null,
}
return {
props: {
2022-11-16 23:05:53 +03:00
...mdx.frontmatter,
slug: `/${args.params.listPathPage.join('/')}`,
2022-11-16 22:48:13 +03:00
mdx,
2022-11-16 23:05:53 +03:00
sectionTitle: sectionMeta?.title ?? null,
theme: sectionMeta?.theme ?? null,
section: section,
apiDetails: apiDetails,
isIndex,
},
}
}