Skip to content

Commit 6ddffde

Browse files
authored
fix: fixed errors
1 parent dee0efa commit 6ddffde

4 files changed

Lines changed: 7 additions & 63 deletions

File tree

astro.config.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { defineConfig } from "astro/config";
22

3+
const site = process.env.SITE_URL ?? "https://rapidai.org";
34
const repoName = process.env.GITHUB_REPOSITORY?.split("/")[1];
45
const isGithubPagesBuild = process.env.GITHUB_ACTIONS === "true" && repoName;
5-
const base = process.env.SITE_BASE ?? (isGithubPagesBuild ? `/${repoName}/` : "/");
6-
const site = process.env.SITE_URL ?? "https://rapidai.org";
6+
const siteHost = new URL(site).hostname;
7+
const usesProjectSubpath = siteHost.endsWith(".github.io");
8+
const base = process.env.SITE_BASE ?? (isGithubPagesBuild && usesProjectSubpath ? `/${repoName}/` : "/");
79

810
export default defineConfig({
911
site,

src/components/GitHubStars.astro

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
---
22
interface Props {
3-
repo: string;
43
fallback: string;
54
}
65
7-
const { repo, fallback } = Astro.props;
6+
const { fallback } = Astro.props;
87
const normalizedFallback = /^\d[\d,]*$/.test(fallback) ? fallback : "";
98
---
109

11-
<span class="card-tag github-stars" data-github-stars={repo} data-fallback={normalizedFallback}>
10+
<span class="card-tag github-stars">
1211
{normalizedFallback ? `收藏 ${normalizedFallback}` : "仓库"}
1312
</span>

src/layouts/MainLayout.astro

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -71,61 +71,6 @@ const homeHref = withBase("/");
7171
</footer>
7272
</div>
7373
<script is:inline>
74-
const formatStarCount = (value) =>
75-
Number.isFinite(value) ? new Intl.NumberFormat("en-US").format(value) : null;
76-
const normalizeFallback = (value) => (/^\d[\d,]*$/.test(value || "") ? value : null);
77-
78-
const applyGitHubStars = async () => {
79-
const nodes = Array.from(document.querySelectorAll("[data-github-stars]"));
80-
const repos = [...new Set(nodes.map((node) => node.dataset.githubStars).filter(Boolean))];
81-
if (!repos.length) return;
82-
83-
const cacheKey = "rapidai-github-stars-v1";
84-
const cacheTtl = 1000 * 60 * 30;
85-
const now = Date.now();
86-
let cache = {};
87-
88-
try {
89-
cache = JSON.parse(sessionStorage.getItem(cacheKey) || "{}");
90-
} catch {
91-
cache = {};
92-
}
93-
94-
const pending = repos.map(async (repo) => {
95-
const cached = cache[repo];
96-
if (cached && now - cached.timestamp < cacheTtl) {
97-
return [repo, cached.value];
98-
}
99-
100-
try {
101-
const response = await fetch(`https://api.github.com/repos/${repo}`, {
102-
headers: { Accept: "application/vnd.github+json" }
103-
});
104-
if (!response.ok) throw new Error(`GitHub API ${response.status}`);
105-
const data = await response.json();
106-
const formatted = formatStarCount(data.stargazers_count);
107-
if (!formatted) throw new Error("Invalid star count");
108-
cache[repo] = { value: formatted, timestamp: now };
109-
return [repo, formatted];
110-
} catch {
111-
return [repo, null];
112-
}
113-
});
114-
115-
const entries = await Promise.all(pending);
116-
117-
try {
118-
sessionStorage.setItem(cacheKey, JSON.stringify(cache));
119-
} catch {}
120-
121-
for (const node of nodes) {
122-
const repo = node.dataset.githubStars;
123-
const matched = entries.find(([entryRepo]) => entryRepo === repo);
124-
const value = matched?.[1] || normalizeFallback(node.dataset.fallback);
125-
node.textContent = value ? `收藏 ${value}` : "仓库";
126-
}
127-
};
128-
12974
const applyMemberNewBadges = () => {
13075
const today = new Date();
13176
today.setHours(0, 0, 0, 0);
@@ -158,13 +103,11 @@ const homeHref = withBase("/");
158103
document.addEventListener(
159104
"DOMContentLoaded",
160105
() => {
161-
applyGitHubStars();
162106
applyMemberNewBadges();
163107
},
164108
{ once: true }
165109
);
166110
} else {
167-
applyGitHubStars();
168111
applyMemberNewBadges();
169112
}
170113
</script>

src/utils/github.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export async function getRapidAIProjects(fallback: ProjectFallback[]): Promise<P
132132
name: repo.name,
133133
stars: String(repo.stargazers_count ?? 0),
134134
repo: repo.full_name,
135-
category: existing?.category || "GitHub Repo",
135+
category: existing?.category || "代码仓库",
136136
summary:
137137
existing?.summary ||
138138
repo.description ||

0 commit comments

Comments
 (0)