Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"isomorphic-dompurify": "^2.22.0",
"ky": "^1.7.2",
"next": "^14.2.32",
"next-auth": "^4.24.7",
"next-auth": "4.24.11",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-dropzone": "^14.2.3",
Expand Down
48 changes: 48 additions & 0 deletions src/utils/mdx/files/mapMDXSlugByFilePaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import fs from "fs";
import * as path from "path";

/**
* Returns true if the file is an MDX file.
* @param fileName - File name.
* @returns true if the file is an MDX file.
*/
function isMdxFile(fileName: string): boolean {
return fileName.endsWith(".mdx");
}

/**
* Maps each MDX file path to its slug.
* @param docsDirectory - Docs directory.
* @param dirPath - Directory path.
* @param slugByFilePaths - Accumulator: Map of slug by mdx file path.
* @returns returns slug by mdx file path.
*/
export function mapMDXSlugByFilePaths(
docsDirectory: string,
dirPath = docsDirectory,
slugByFilePaths: Map<string, string[]> = new Map()
): Map<string, string[]> {
const dirents = fs.readdirSync(dirPath, { withFileTypes: true });
return dirents.reduce((acc, dirent) => {
/* Accumulate the slug for each MDX file. */
if (dirent.isFile() && isMdxFile(dirent.name)) {
const mdxPath = path.resolve(dirPath, dirent.name);
/* Build the slug from the file relative directory and file name. */
const mdxRelativePath = path.relative(docsDirectory, mdxPath);
const { dir, name } = path.parse(mdxRelativePath);
let slug = [] as string[];
if (dir) slug = dir.split(path.sep);
slug.push(name);
acc.set(mdxPath, slug);
}
/* Recurse into subdirectories. */
if (dirent.isDirectory()) {
mapMDXSlugByFilePaths(
docsDirectory,
path.resolve(dirPath, dirent.name),
acc
);
}
return acc;
}, slugByFilePaths);
}
10 changes: 10 additions & 0 deletions src/utils/mdx/files/resolveRelativeDirs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as path from "path";

/**
* Resolves an absolute docs path from a single set of directory segments.
* @param relativeDirs - Directory segments relative to the docs root.
* @returns Absolute path.
*/
export function resolveRelativeDirs(relativeDirs: string[]): string {
return path.join(process.cwd(), ...relativeDirs);
}
18 changes: 18 additions & 0 deletions src/utils/mdx/staticGeneration/staticPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { GetStaticPathsResult } from "next";
import { mapMDXSlugByFilePaths } from "../files/mapMDXSlugByFilePaths";
import { resolveRelativeDirs } from "../files/resolveRelativeDirs";

/**
* Builds Next.js static paths for MDX page files found under the given relative directories.
* Each path’s `slug` is composed of the file’s relative directory segments plus the MDX filename (without extension).
* @param relativeDirs - Relative directories to scan for MDX pages.
* @returns Array of path objects suitable for `getStaticPaths`.
*/
export function buildStaticPaths(
relativeDirs: string[]
): GetStaticPathsResult["paths"] {
const slugByFilePaths = mapMDXSlugByFilePaths(
resolveRelativeDirs(relativeDirs)
);
return [...slugByFilePaths].map(([, slug]) => ({ params: { slug } }));
}