From 1d10ddecb79632422de834d0c45128aac02d9fec Mon Sep 17 00:00:00 2001 From: Gordon Leigh Date: Mon, 31 Jul 2023 21:42:47 +0200 Subject: [PATCH] add README --- README.md | 9 ++++ packages/docs/app/docs/[[...slug]]/page.tsx | 2 +- packages/docs/app/page.tsx | 2 +- packages/docs/util/content.ts | 46 ++++++++++++++------- 4 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..781ddcc --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# superdocs + +🚧 Work in Progress 🚧 + +Automagic documentation generation. + +## Documentation + +See [Documentation](https://gordonmleigh.github.io/superdocs). diff --git a/packages/docs/app/docs/[[...slug]]/page.tsx b/packages/docs/app/docs/[[...slug]]/page.tsx index 4a823cf..0cf1ff1 100644 --- a/packages/docs/app/docs/[[...slug]]/page.tsx +++ b/packages/docs/app/docs/[[...slug]]/page.tsx @@ -34,7 +34,7 @@ export default async function DocsPage({ return ( -

{meta.title}

+ {!meta.hideTitle &&

{meta.title}

} {content}
); diff --git a/packages/docs/app/page.tsx b/packages/docs/app/page.tsx index 87fe6d3..12fbca2 100644 --- a/packages/docs/app/page.tsx +++ b/packages/docs/app/page.tsx @@ -1,5 +1,5 @@ import { redirect } from "next/navigation"; export default function IndexPage(): JSX.Element { - return redirect("/docs/learn"); + return redirect("/docs/introduction"); } diff --git a/packages/docs/util/content.ts b/packages/docs/util/content.ts index 00e83c9..b00a666 100644 --- a/packages/docs/util/content.ts +++ b/packages/docs/util/content.ts @@ -3,7 +3,7 @@ import rehypePrism from "@mapbox/rehype-prism"; import { glob } from "fast-glob"; import { readFile } from "fs/promises"; import { compileMDX } from "next-mdx-remote/rsc"; -import { join, resolve } from "path"; +import { resolve } from "path"; import { ReactNode, cache } from "react"; const ContentPath = resolve("./content"); @@ -11,24 +11,14 @@ const ContentPath = resolve("./content"); export interface ContentPage { content: ReactNode; meta: { + hideTitle?: boolean; + order?: number; pageTitle?: string; slug?: string; title?: string; }; } -export const fetchContentBySlug = cache( - async (slug: string | string[] | undefined) => { - const content = await fetchAllContent(); - const normalised = normaliseSlug(slug); - const page = content.find((x) => x.meta.slug === normalised); - if (!page) { - throw new Error(`can't find content with slug ${normalised}`); - } - return page; - }, -); - export const fetchAllContent = cache(fetchAllContentInternal); async function fetchAllContentInternal(): Promise { @@ -38,12 +28,21 @@ async function fetchAllContentInternal(): Promise { for (const path of paths) { pages.push(await fetchPage(path)); } + + const readme = await fetchPage(resolve("../../README.md")); + readme.meta.order = 0; + readme.meta.hideTitle = true; + readme.meta.slug = "/introduction"; + readme.meta.title = "Introduction"; + pages.push(readme); + + pages.sort((a, b) => (a.meta.order ?? 1000) - (b.meta.order ?? 1000)); return pages; } async function fetchPage(path: string): Promise { - // https://github.com/vercel/neBySlugxt.js/discussions/50897#discussioncomment-6122518 - const fileContent = await readFile(join(ContentPath, path), { + // https://github.com/vercel/next.js/discussions/50897#discussioncomment-6122518 + const fileContent = await readFile(resolve(ContentPath, path), { encoding: "utf-8", }); const { frontmatter, content } = await compileMDX({ @@ -56,6 +55,11 @@ async function fetchPage(path: string): Promise { }, components: mdxComponents, }); + if (frontmatter.order) { + try { + frontmatter.order = parseInt(frontmatter.order as string, 10); + } catch {} + } return { meta: { @@ -69,6 +73,18 @@ async function fetchPage(path: string): Promise { }; } +export const fetchContentBySlug = cache( + async (slug: string | string[] | undefined) => { + const content = await fetchAllContent(); + const normalised = normaliseSlug(slug); + const page = content.find((x) => x.meta.slug === normalised); + if (!page) { + throw new Error(`can't find content with slug ${normalised}`); + } + return page; + }, +); + function normaliseSlug(slug: string | string[] | undefined): string { if (Array.isArray(slug)) { // join and split again in case array elements also need normalised