add support from pulling from a Git repo for docs content
Some checks failed
Deploy / deploy (ubuntu-latest, 2.44.0) (push) Failing after 47s

Took 24 minutes
This commit is contained in:
Braydon 2024-10-09 16:54:36 -04:00
parent 1614889a62
commit d364ea83b5
5 changed files with 44 additions and 14 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -32,6 +32,7 @@
"react-dom": "^19.0.0-rc-1460d67c-20241003", "react-dom": "^19.0.0-rc-1460d67c-20241003",
"remark-gfm": "^4.0.0", "remark-gfm": "^4.0.0",
"remote-mdx": "^0.0.8", "remote-mdx": "^0.0.8",
"simple-git": "^3.27.0",
"tailwind-merge": "^2.5.3", "tailwind-merge": "^2.5.3",
"tailwindcss-animate": "^1.0.7" "tailwindcss-animate": "^1.0.7"
}, },

View File

@ -30,7 +30,7 @@ const DocsPage = async ({
); );
// Get the content to display based on the provided slug // Get the content to display based on the provided slug
const pages: DocsContentMetadata[] = getDocsContent(); const pages: DocsContentMetadata[] = await getDocsContent();
const decodedSlug: string = decodeURIComponent(slug || ""); const decodedSlug: string = decodeURIComponent(slug || "");
const page: DocsContentMetadata | undefined = pages.find( const page: DocsContentMetadata | undefined = pages.find(
(metadata: DocsContentMetadata): boolean => (metadata: DocsContentMetadata): boolean =>
@ -101,7 +101,9 @@ export const generateMetadata = async ({
"/" "/"
); // The slug of the content ); // The slug of the content
if (slug) { if (slug) {
const content: DocsContentMetadata | undefined = getDocsContent().find( const content: DocsContentMetadata | undefined = (
await getDocsContent()
).find(
(metadata: DocsContentMetadata): boolean => metadata.slug === slug (metadata: DocsContentMetadata): boolean => metadata.slug === slug
); // Get the content based on the provided slug ); // Get the content based on the provided slug
if (content) { if (content) {

View File

@ -38,12 +38,12 @@ export const viewport: Viewport = {
/** /**
* The primary layout for this app. * The primary layout for this app.
*/ */
const RootLayout = ({ const RootLayout = async ({
children, children,
}: Readonly<{ }: Readonly<{
children: ReactNode; children: ReactNode;
}>): ReactElement => { }>): Promise<ReactElement> => {
const pages: DocsContentMetadata[] = getDocsContent(); const pages: DocsContentMetadata[] = await getDocsContent();
return ( return (
<html lang="en"> <html lang="en">
<ThemeProvider attribute="class" defaultTheme="dark" enableSystem> <ThemeProvider attribute="class" defaultTheme="dark" enableSystem>

View File

@ -2,6 +2,8 @@ import * as fs from "node:fs";
import { Stats } from "node:fs"; import { Stats } from "node:fs";
import path from "node:path"; import path from "node:path";
import config from "@/config"; import config from "@/config";
import simpleGit from "simple-git";
import os from "node:os";
/** /**
* The regex to match for metadata. * The regex to match for metadata.
@ -9,20 +11,45 @@ import config from "@/config";
const METADATA_REGEX: RegExp = /---\s*([\s\S]*?)\s*---/; const METADATA_REGEX: RegExp = /---\s*([\s\S]*?)\s*---/;
/** /**
* The directory docs are stored in. * Check if the DOCS_DIR is a Git URL.
*/ */
const DOCS_DIR: string = path.join( const isGitUrl = (url: string): boolean => {
config.contentSource.replace("{process}", process.cwd()) return /^https?:\/\/|git@|\.git$/.test(url);
); };
/** /**
* Get the content to * The directory docs are stored in.
* display in the docs.
*/ */
export const getDocsContent = (): DocsContentMetadata[] => { const DOCS_DIR: string = isGitUrl(config.contentSource)
? config.contentSource
: path.join(config.contentSource.replace("{process}", process.cwd()));
/**
* Clone the Git repository if DOCS_DIR is a URL, else use the local directory.
* If it's a Git URL, clone it to a cache directory and reuse it.
*/
const getDocsDirectory = async (): Promise<string> => {
if (isGitUrl(DOCS_DIR)) {
const repoHash: string = Buffer.from(DOCS_DIR).toString("base64"); // Create a unique identifier based on the repo URL
const cacheDir: string = path.join(os.tmpdir(), "docs_cache", repoHash);
// Pull the latest changes from the repo if we don't have it
if (!fs.existsSync(cacheDir) || fs.readdirSync(cacheDir).length < 1) {
await simpleGit().clone(DOCS_DIR, cacheDir, { "--depth": 1 });
}
return cacheDir;
}
return DOCS_DIR;
};
/**
* Get the content to display in the docs.
*/
export const getDocsContent = async (): Promise<DocsContentMetadata[]> => {
const docsDir: string = await getDocsDirectory();
const content: DocsContentMetadata[] = []; const content: DocsContentMetadata[] = [];
for (const directory of getRecursiveDirectories(DOCS_DIR)) { for (const directory of getRecursiveDirectories(docsDir)) {
content.push(...getMetadata<DocsContentMetadata>(DOCS_DIR, directory)); content.push(...getMetadata<DocsContentMetadata>(docsDir, directory));
} }
return content.sort((a: DocsContentMetadata, b: DocsContentMetadata) => { return content.sort((a: DocsContentMetadata, b: DocsContentMetadata) => {
const orderA = a.order ?? Number.MAX_SAFE_INTEGER; const orderA = a.order ?? Number.MAX_SAFE_INTEGER;