Skip to content

Commit

Permalink
add README
Browse files Browse the repository at this point in the history
  • Loading branch information
gordonmleigh committed Jul 31, 2023
1 parent 977af11 commit 1d10dde
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# superdocs

🚧 Work in Progress 🚧

Automagic documentation generation.

## Documentation

See [Documentation](https://gordonmleigh.github.io/superdocs).
2 changes: 1 addition & 1 deletion packages/docs/app/docs/[[...slug]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default async function DocsPage({

return (
<MainLayout>
<h1>{meta.title}</h1>
{!meta.hideTitle && <h1>{meta.title}</h1>}
{content}
</MainLayout>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { redirect } from "next/navigation";

export default function IndexPage(): JSX.Element {
return redirect("/docs/learn");
return redirect("/docs/introduction");
}
46 changes: 31 additions & 15 deletions packages/docs/util/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,22 @@ 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");

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<ContentPage[]> {
Expand All @@ -38,12 +28,21 @@ async function fetchAllContentInternal(): Promise<ContentPage[]> {
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<ContentPage> {
// 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({
Expand All @@ -56,6 +55,11 @@ async function fetchPage(path: string): Promise<ContentPage> {
},
components: mdxComponents,
});
if (frontmatter.order) {
try {
frontmatter.order = parseInt(frontmatter.order as string, 10);
} catch {}
}

return {
meta: {
Expand All @@ -69,6 +73,18 @@ async function fetchPage(path: string): Promise<ContentPage> {
};
}

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
Expand Down

0 comments on commit 1d10dde

Please sign in to comment.