/[sudobot]/trunk/docs/actions/pageinfo.ts
ViewVC logotype

Diff of /trunk/docs/actions/pageinfo.ts

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 575 by rakinar2, Mon Jul 29 17:59:26 2024 UTC revision 626 by rakinar2, Sat Sep 7 09:38:45 2024 UTC
# Line 1  Line 1 
1  "use server";  "use server";
2    
3    import index from "@/index.json";
4  import { branch } from "@/utils/links";  import { branch } from "@/utils/links";
5    
6    const map = {} as Record<string, any>;
7    
8  export async function getPageInfo(pathname: string) {  export async function getPageInfo(pathname: string) {
9      "use server";      "use server";
10    
11        if (Object.keys(map).length === 0) {
12            for (const page of index) {
13                map[page.url] = page;
14            }
15        }
16    
17        const trimmedPathname = pathname.replace(/\/$/, "");
18        const path = (map[trimmedPathname] ?? map[trimmedPathname + "/"])?.path;
19        const pageExtension = path?.endsWith(".mdx")
20            ? "mdx"
21            : path?.endsWith(".md")
22              ? "md"
23              : "tsx";
24    
25        const urlEncodedPath = `docs/app${
26            pathname === "/" ? "" : "/(docs)"
27        }${pathname.trim() + (pathname === "/" ? "/" : "")}page.${pageExtension}`;
28    
29      const githubURL = pathname      const githubURL = pathname
30          ? `https://api.github.com/repos/onesoft-sudo/sudobot/commits?path=${encodeURIComponent(          ? `https://api.github.com/repos/onesoft-sudo/sudobot/commits?path=${encodeURIComponent(urlEncodedPath)}&sha=${encodeURIComponent(branch)}`
               "docs/app" + (pathname === "/" ? "" : "/(docs)"),  
           )}${encodeURIComponent(pathname)}${  
               pathname === "/" ? "" : "%2F"  
           }page.mdx&sha=${encodeURIComponent(branch)}`  
31          : null;          : null;
32    
33      let lastModifiedDate = new Date();      let lastModifiedDate = new Date();
34      let avatarURL = null;      let avatarURL = null;
35        let username = null;
36    
37      if (githubURL) {      if (githubURL) {
38          try {          try {
# Line 26  export async function getPageInfo(pathna Line 45  export async function getPageInfo(pathna
45              const json = await response.json();              const json = await response.json();
46              const timestamp = json?.[0]?.commit?.author?.date;              const timestamp = json?.[0]?.commit?.author?.date;
47              avatarURL = json?.[0]?.author?.avatar_url;              avatarURL = json?.[0]?.author?.avatar_url;
48                username = json?.[0]?.author?.name ?? json?.[0]?.author?.login;
49    
50              if (timestamp) {              if (timestamp) {
51                  lastModifiedDate = new Date(timestamp);                  lastModifiedDate = new Date(timestamp);
# Line 35  export async function getPageInfo(pathna Line 55  export async function getPageInfo(pathna
55          }          }
56      }      }
57    
58      return { lastModifiedDate, avatarURL };      return { lastModifiedDate, avatarURL, urlEncodedPath, username };
59  }  }

Legend:
Removed from v.575  
changed lines
  Added in v.626

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26