|
1 | 1 | import fs from "fs";
|
| 2 | +import type { OpenAPIV3 } from "@scalar/openapi-types"; |
2 | 3 | import path from "path";
|
3 | 4 | import matter from "gray-matter";
|
4 | 5 | import { getAllMdxFiles } from "./utils/getAllMdxFiles";
|
@@ -153,7 +154,7 @@ function removeReferences(content: string): string {
|
153 | 154 | return lines.join("\n").trim();
|
154 | 155 | }
|
155 | 156 |
|
156 |
| -async function parseMdxContent(filePath: string): Promise<string> { |
| 157 | +async function parseMdxContent(filePath: string, usePageTitle = false): Promise<string> { |
157 | 158 | const mdxContent = await fs.promises.readFile(filePath, "utf8");
|
158 | 159 | const { content } = matter(mdxContent);
|
159 | 160 | const imports: Array<{ name: string; module: string }> = await extractImportStatements(content);
|
@@ -223,8 +224,8 @@ async function parseMdxContent(filePath: string): Promise<string> {
|
223 | 224 | const { title, content: contentWithoutTitle } = extractMainTitle(processedContent);
|
224 | 225 | const filePathWithoutExtension = filePath.replace(/\.[^/.]+$/, "");
|
225 | 226 | const [, relativePath] = filePathWithoutExtension.split("/docs").filter(Boolean);
|
226 |
| - const url = `https://supertokens.com/docs/${relativePath}`; |
227 |
| - const parsedTitle = buildTitle(title, filePath); |
| 227 | + const url = `https://supertokens.com/docs${relativePath}`; |
| 228 | + const parsedTitle = usePageTitle ? title : buildTitle(title, filePath); |
228 | 229 | processedContent = `
|
229 | 230 | # ${parsedTitle}
|
230 | 231 | Source: ${url}
|
@@ -275,6 +276,28 @@ function buildTitle(currentTitle: string, filePath: string): string {
|
275 | 276 | return `${title} ${currentTitle}`;
|
276 | 277 | }
|
277 | 278 |
|
| 279 | +async function generateApiReferenceText(apiName: "cdi" | "fdi") { |
| 280 | + const apiReference = JSON.parse(await fs.promises.readFile(`./static/${apiName}.json`, "utf8")) as OpenAPIV3.Document; |
| 281 | + const apiReferenceMapping = JSON.parse( |
| 282 | + await fs.promises.readFile(`./static/${apiName}-mapping.json`, "utf8"), |
| 283 | + ) as OpenAPIV3.Document; |
| 284 | + |
| 285 | + let text = ``; |
| 286 | + for (const route in apiReference.paths) { |
| 287 | + for (const method in apiReference.paths[route]) { |
| 288 | + const operation = apiReference.paths[route][method]; |
| 289 | + const mapping = apiReferenceMapping[operation.operationId]; |
| 290 | + if (operation.deprecated) continue; |
| 291 | + if (!mapping) continue; |
| 292 | + const title = operation.summary || route; |
| 293 | + const pathWithoutExtension = mapping.filePath.replace(/\.[^/.]+$/, ""); |
| 294 | + const url = `https://supertokens.com/docs/references${pathWithoutExtension}`; |
| 295 | + text = `${text}\n\n## ${title}\nSource: ${url}\nEndpoint: ${method.toUpperCase()} ${route}`; |
| 296 | + } |
| 297 | + } |
| 298 | + return text; |
| 299 | +} |
| 300 | + |
278 | 301 | export default function createLLMFullFile(context) {
|
279 | 302 | return {
|
280 | 303 | name: "generate-llms-full-txt",
|
@@ -314,10 +337,22 @@ export default function createLLMFullFile(context) {
|
314 | 337 | return { files: filesWithoutReferences };
|
315 | 338 | },
|
316 | 339 | postBuild: async ({ content, outDir, routes }) => {
|
317 |
| - const llmsFullTxt = (await Promise.all(content.files.map((file) => parseMdxContent(file.path)))).join("\n\n"); |
| 340 | + let llmsFullTxt = (await Promise.all(content.files.map((file) => parseMdxContent(file.path)))).join("\n\n"); |
| 341 | + |
| 342 | + const fdiIntro = await parseMdxContent("./docs/references/fdi/introduction.mdx", true); |
| 343 | + const cdiIntro = await parseMdxContent("./docs/references/cdi/introduction.mdx", true); |
| 344 | + const fdiReferenceFullTxt = await generateApiReferenceText("fdi"); |
| 345 | + const cdiReferenceFullTxt = await generateApiReferenceText("cdi"); |
| 346 | + |
| 347 | + llmsFullTxt = `${llmsFullTxt} |
| 348 | +${fdiIntro} |
| 349 | +${fdiReferenceFullTxt} |
| 350 | +\n\n |
| 351 | +${cdiIntro} |
| 352 | +${cdiReferenceFullTxt} |
| 353 | +`; |
318 | 354 |
|
319 | 355 | fs.writeFileSync(path.join(outDir, "llms-full.txt"), llmsFullTxt);
|
320 | 356 | },
|
321 | 357 | };
|
322 | 358 | }
|
323 |
| - |
|
0 commit comments