Skip to content

Commit fe25567

Browse files
committed
Add last author of page
1 parent b0b3eaf commit fe25567

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

astro.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default defineConfig({
3838
Sidebar: "./src/components/overrides/Sidebar.astro",
3939
PageFrame: "./src/components/overrides/PageFrame.astro",
4040
Footer: "./src/components/overrides/Footer.astro",
41+
LastUpdated: "./src/components/overrides/LastUpdated.astro",
4142
Banner: "./src/components/overrides/Banner.astro",
4243
TableOfContents: "./src/components/overrides/TableOfContents.astro",
4344
MobileTableOfContents: "./src/components/overrides/MobileTableOfContents.astro",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
import { type GitHubAccount, getGitHubAccountFromFile } from "/src/utils/git-utils.ts"
3+
4+
const { lang, lastUpdated } = Astro.locals.starlightRoute;
5+
const filePath = Astro.locals.starlightRoute.entry.filePath
6+
7+
const acc: GitHubAccount = await getGitHubAccountFromFile(filePath);
8+
---
9+
10+
{
11+
lastUpdated && (
12+
<p>
13+
{"Last updated: "}
14+
<time datetime={lastUpdated.toISOString()}>
15+
{lastUpdated.toLocaleDateString(lang, {
16+
dateStyle: "medium",
17+
timeZone: "UTC",
18+
})}
19+
</time>
20+
{" by "}
21+
<a set:html={acc.displayName} href={acc.accountLink} />
22+
</p>
23+
)
24+
}

src/content/docs/paper/dev/api/command-api/basics/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ The following pages will be added to the documentation in the future:
4848
:::
4949

5050
## Additional support
51-
For support regarding the command API, you can always ask in our [Discord Server](https://discord.gg/PaperMC) in the `#paper-dev` channel!
51+
For support regarding the command API, you can always ask in our Discord server under the [`#paper-dev`](https://discord.com/channels/289587909051416579/555462289851940864) channel!

src/utils/git-utils.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { execSync } from "child_process";
2+
3+
export interface GitHubAccount {
4+
displayName: string;
5+
email: string;
6+
accountLink?: string;
7+
}
8+
9+
const headers = {
10+
headers: {
11+
Accept: "application/vnd.github+json",
12+
"User-Agent": "papermc-docs/author",
13+
},
14+
};
15+
16+
const repo: string = "PaperMC/docs"
17+
const cache: Map<string, GitHubAccount> = new Map();
18+
19+
// Git
20+
export async function getGitHubAccountFromFile(
21+
filePath: string
22+
): Promise<GitHubAccount | null> {
23+
const displayName = execSync(
24+
`git log -1 --pretty="format:%an" -- "${filePath}"`
25+
).toString();
26+
const email = execSync(
27+
`git log -1 --pretty="format:%ae" -- "${filePath}"`
28+
).toString();
29+
30+
const cached = cache.get(email);
31+
if (cached != null) {
32+
cached.displayName = displayName;
33+
return cached;
34+
}
35+
36+
const accLink = await getGitHubAccountLinkByEmail(email);
37+
if (accLink != null) {
38+
const acc: GitHubAccount = {
39+
displayName: displayName,
40+
email: email,
41+
accountLink: accLink,
42+
};
43+
cache.set(email, acc);
44+
return acc;
45+
}
46+
47+
// As the email seems to not be directly linked to an account, we instead use the GitHub API
48+
const url = new URL(`https://api.github.com/repos/${repo}/commits`);
49+
url.searchParams.set('path', filePath);
50+
url.searchParams.set('per_page', '1');
51+
52+
const commit = (await fetch(url, headers).then(response => response.json()))[0]
53+
const acc: GitHubAccount = {
54+
displayName: displayName,
55+
email: email,
56+
accountLink: commit?.author?.html_url
57+
}
58+
59+
console.log("Latest commit data is as follows:")
60+
console.log(commit)
61+
cache.set(email, acc);
62+
return acc;
63+
}
64+
65+
// E-Mail related
66+
67+
export async function getGitHubAccountLinkByEmail(
68+
email: string
69+
): Promise<string | null> {
70+
const url = new URL(`https://api.github.com/search/users?q=${email}`);
71+
const result = await fetch(url, headers).then((response) => response.json());
72+
73+
if (result?.items == null || result.items.length < 1) {
74+
return null;
75+
}
76+
77+
const item = result.items[0];
78+
return item.html_url;
79+
}

0 commit comments

Comments
 (0)