generated from nhcarrigan/template
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
/**
|
|
* @copyright nhcarrigan
|
|
* @license Naomi's Public License
|
|
* @author Naomi Carrigan
|
|
*/
|
|
|
|
/**
|
|
* This script fetches our documentation from our repository,
|
|
* compiles it into an MCP format, and writes it to a JSON file.
|
|
* It is intended to run automatically as part of the build process.
|
|
*/
|
|
|
|
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
import matter from "gray-matter";
|
|
import { promisify } from "node:util";
|
|
import { exec } from "node:child_process";
|
|
|
|
const execAsync = promisify(exec);
|
|
|
|
const docsDirectory = path.resolve(process.cwd(), "temp-docs");
|
|
const docsPath = path.resolve(process.cwd(), "temp-docs", "src", "content", "docs")
|
|
|
|
async function walk(directory: string): Promise<Array<string>> {
|
|
const dirents = await fs.readdir(directory, { withFileTypes: true });
|
|
const files = await Promise.all(
|
|
dirents.map(async (dirent) => {
|
|
const result = path.resolve(directory, dirent.name);
|
|
return dirent.isDirectory() ? await walk(result) : result;
|
|
})
|
|
);
|
|
return files.flat();
|
|
}
|
|
|
|
await execAsync(`git clone https://git.nhcarrigan.com/nhcarrigan/docs.git ${docsDirectory}`, {
|
|
cwd: process.cwd(),
|
|
stdio: "inherit",
|
|
});
|
|
|
|
const files = await walk(docsPath);
|
|
const markdownFiles = files.filter((f) => {
|
|
return f.endsWith(".md");
|
|
});
|
|
|
|
const results = await Promise.all(
|
|
markdownFiles.map(async (file) => {
|
|
const raw = await fs.readFile(file, "utf-8");
|
|
const { content, data } = matter(raw);
|
|
|
|
// Split content by header blocks (basic chunking)
|
|
const chunks = content.split(/^#+\s+/gm).map((chunk, index) => {
|
|
return {
|
|
content: chunk.trim(),
|
|
file: path.relative(docsDirectory, file),
|
|
id: `${path.relative(docsDirectory, file)}::${index}`,
|
|
metadata: data,
|
|
title:
|
|
index === 0 ? "(intro)" : chunk.split("\n")[0]?.trim() ?? "Unknown",
|
|
url: `https://docs.nhcarrigan.com/${path
|
|
.relative(docsPath, file)
|
|
.replace(/\.md$/, "").replace(/\/$/, "")}#${index === 0 ? "" : chunk.split("\n")[0]?.trim().toLowerCase().replace(/\s+/g, "-").replace(/\./g, "")}`,
|
|
};
|
|
});
|
|
|
|
return chunks;
|
|
})
|
|
);
|
|
|
|
const flat = results.flat();
|
|
|
|
const string = `/**
|
|
* @copyright nhcarrigan
|
|
* @license Naomi's Public License
|
|
* @author Naomi Carrigan
|
|
*/
|
|
|
|
export const documentationData = ${JSON.stringify(flat, null, 2)};
|
|
`;
|
|
|
|
await fs.writeFile(
|
|
path.resolve(process.cwd(), "src", "data", "docs.ts"),
|
|
string
|
|
);
|
|
|
|
await fs.rm(docsDirectory, { recursive: true, force: true });
|