/[sudobot]/branches/7.x/docs/generate-page-index.js
ViewVC logotype

Annotation of /branches/7.x/docs/generate-page-index.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: text/javascript
File size: 2591 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 const { readFile, writeFile, cp } = require("fs/promises");
2     const { glob } = require("glob");
3     const path = require("path");
4    
5     (async () => {
6     const pages = await glob("app/**/page.{tsx,mdx}");
7     const index = [];
8    
9     for (const page of pages) {
10     const isMDX = page.endsWith(".mdx");
11     index.push(
12     isMDX
13     ? await generateIndexForMDXPage(page)
14     : await generateIndexForTSXPage(page),
15     );
16     console.log("DONE ", page);
17     }
18    
19     await writeFile(path.join(__dirname, "index.json"), JSON.stringify(index));
20    
21     try {
22     await cp(
23     path.join(__dirname, "index.json"),
24     path.join(__dirname, ".next/server/index.json"),
25     );
26     } catch (error) {
27     console.error(error);
28     }
29    
30     console.table(
31     index.map(item => ({
32     ...item,
33     data: `${item.data?.substring(0, 50)}${
34     item.data?.length && item.data?.length > 15 ? "..." : ""
35     }`,
36     })),
37     );
38     })();
39    
40     async function generateIndexForMDXPage(page) {
41     const contents = await readFile(page, {
42     encoding: "utf-8",
43     });
44     let [frontmatter, data] = contents
45     .substring(contents.startsWith("---") ? 3 : 0)
46     .split("\n---\n");
47    
48     if (!contents.startsWith("---")) {
49     data = frontmatter;
50     frontmatter = null;
51     }
52    
53     const entries = frontmatter
54     ?.split("\n")
55     .filter(Boolean)
56     .map(entry =>
57     entry
58     .split(/:(.*)/s)
59     .filter(Boolean)
60     .map((a, i) => {
61     const trimmed = a.trim();
62     return i === 1 &&
63     trimmed.startsWith('"') &&
64     trimmed.endsWith('"')
65     ? trimmed.substring(1, trimmed.length - 1).trim()
66     : trimmed;
67     }),
68     );
69    
70     const frontmatterData = entries ? Object.fromEntries(entries) : null;
71    
72     return {
73     ...frontmatterData,
74     data: data
75     .replace(/^(([\s\r\n]*)import([^.]+);)+/gi, "")
76     .replace(/^(([\s\r\n]*)export([^.]+);)+/gi, "")
77     .replace(/([\s\r\n]*)export default ([^;]+);$/gi, "")
78     .replace(/<\/?[^>]+(>|$)/g, ""),
79     url:
80     "/" +
81     page
82     .replace(/\/\([a-z0-9A-Z_-]+\)/gi, "")
83     .replace(/^app\//gi, "")
84     .replace(/page\.(ts|md)x$/gi, ""),
85     path: page,
86     };
87     }
88    
89     async function generateIndexForTSXPage(page) {
90     throw new Error("Not implemented");
91     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26