/[sudobot]/branches/7.x/docs/utils/pages.ts
ViewVC logotype

Annotation of /branches/7.x/docs/utils/pages.ts

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: application/typescript
File size: 3958 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 import indexJson from "@/index.json";
2     import {
3     BOT_INVITE_REQUEST_URL,
4     DISCORD_URL,
5     DONATION_URL,
6     SUPPORT_EMAIL_ADDRESS,
7     } from "./links";
8     import { toTitleCase } from "./utils";
9    
10     export type DocsPage = {
11     name: string;
12     url?: string;
13     children?: DocsPageWithoutChildren[];
14     };
15    
16     export type DocsPageWithoutChildren = Omit<DocsPage, "children">;
17    
18     export const pages = [
19     {
20     name: "Home",
21     url: "/",
22     },
23     {
24     name: "FAQ",
25     url: "/faq",
26     },
27     {
28     name: "Invite",
29     url: BOT_INVITE_REQUEST_URL,
30     },
31     {
32     name: "Support",
33     url: SUPPORT_EMAIL_ADDRESS,
34     },
35     {
36     name: "Donate",
37     url: DONATION_URL,
38     },
39     {
40     name: "Discord",
41     url: DISCORD_URL,
42     },
43     ];
44    
45     export function resolveDocsURL(url: string) {
46     return url.startsWith("/") ? url : `/${url}`;
47     }
48    
49     export type Index = {
50     title?: string;
51     description?: string;
52     data: string;
53     };
54    
55     let index: Index[] | null = null,
56     lowercasedIndex: Index[] | null = null;
57    
58     function loadIndex() {
59     if (index !== null) {
60     return;
61     }
62    
63     index = indexJson;
64     lowercasedIndex =
65     index?.map(entry => ({
66     data: entry.data.toLowerCase(),
67     title: entry.title?.toLowerCase(),
68     description: entry.description?.toLowerCase(),
69     })) ?? null;
70     }
71    
72     export const getIndex = (lower = false) => {
73     if (!index) {
74     loadIndex();
75     }
76    
77     return lower ? lowercasedIndex! : index!;
78     };
79    
80     let docsPages: DocsPage[] | null = null;
81    
82     export const getDocsPages = () => {
83     if (!docsPages) {
84     loadDocsPages();
85     }
86    
87     return docsPages!;
88     };
89    
90     const excludedFromGrouping = ["features", "getting-started"];
91     const forceGrouping = ["extensions"];
92     const hoistedPages = [
93     "getting-started",
94     "features",
95     "features/screenshots",
96     ].map(a => `/${a}`);
97    
98     function loadDocsPages() {
99     if (docsPages) {
100     return;
101     }
102    
103     docsPages = [];
104    
105     const flattenedPages = indexJson.map(
106     page =>
107     ({
108     name: page.short_name ?? "Unnamed",
109     url: page.url,
110     } satisfies DocsPageWithoutChildren),
111     );
112     const pages: Record<string, DocsPageWithoutChildren[]> = {};
113    
114     for (const page of flattenedPages) {
115     const splitted = page.url.split("/");
116     const [, firstDirectory] = splitted;
117    
118     const key =
119     (forceGrouping.includes(firstDirectory) ||
120     (!excludedFromGrouping.includes(firstDirectory) &&
121     firstDirectory !== "")) &&
122     splitted.length > 3
123     ? toTitleCase(firstDirectory)
124     : "_";
125    
126     pages[key] ??= [];
127     pages[key].push({
128     name: page.name,
129     url:
130     page.url !== "/" && page.url.endsWith("/")
131     ? page.url.substring(0, page.url.length - 1)
132     : page.url,
133     });
134     }
135    
136     if (pages._) {
137     docsPages?.push(
138     ...pages._.sort(
139     (a, b) => a.name.charCodeAt(0) - b.name.charCodeAt(0),
140     ),
141     );
142     }
143    
144     for (const key in pages) {
145     if (key === "_") {
146     continue;
147     }
148    
149     docsPages?.push({
150     name: key,
151     children: pages[key].sort(
152     (a, b) => a.name.charCodeAt(0) - b.name.charCodeAt(0),
153     ),
154     });
155     }
156    
157     docsPages.sort((a, b) => {
158     const aIndex = hoistedPages.indexOf(a.url ?? "");
159     const bIndex = hoistedPages.indexOf(b.url ?? "");
160    
161     if (a.url === "/") {
162     return -1;
163     }
164    
165     if (b.url === "/") {
166     return 1;
167     }
168    
169     if (aIndex !== -1 && bIndex !== -1) {
170     return aIndex - bIndex;
171     }
172    
173     if (aIndex !== -1) {
174     return -1;
175     }
176    
177     if (bIndex !== -1) {
178     return 1;
179     }
180    
181     return a.name.localeCompare(b.name);
182     });
183     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26