Skip to content

Commit 73b94b8

Browse files
committed
update
1 parent 9176ab8 commit 73b94b8

File tree

6 files changed

+75
-8
lines changed

6 files changed

+75
-8
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.tabSize": 2
3+
}

astro.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { defineConfig } from "astro/config";
44
import { localeConfig } from "./config/locale";
55
import { sidebarConfig } from "./config/sidebar";
66

7+
import react from "@astrojs/react";
8+
79
const urlHandler = (url: string) => {
810
if (url.endsWith("/")) {
911
url += "index.html";
@@ -96,6 +98,7 @@ export default defineConfig({
9698
favicon: "/favicon.ico",
9799
credits: true,
98100
}),
101+
react(),
99102
],
100103
build: {
101104
format: "file",

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
},
1313
"dependencies": {
1414
"@astrojs/check": "^0.9.6",
15+
"@astrojs/react": "^4.4.2",
1516
"@astrojs/starlight": "^0.37.3",
17+
"@types/react": "^19.2.9",
18+
"@types/react-dom": "^19.2.3",
1619
"astro": "^5.6.1",
1720
"csv-parse": "^6.1.0",
1821
"qrcode": "^1.5.4",
22+
"react": "^19.2.3",
23+
"react-dom": "^19.2.3",
1924
"sharp": "^0.34.2",
2025
"typescript": "^5.9.3"
2126
},

src/components/react/HitsInfo.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from "react";
2+
3+
export type Props = {};
4+
5+
type Data = {
6+
today: string;
7+
total: string;
8+
};
9+
10+
const HitsInfo: React.FC<Props> = ({}) => {
11+
const [data, setData] = React.useState<Data | null>(null);
12+
React.useEffect(() => {
13+
const hitsUrl = new URL("https://hits.zkitefly.eu.org");
14+
hitsUrl.searchParams.set("tag", window.location.origin + window.location.pathname);
15+
16+
fetch(hitsUrl)
17+
.then(async (response) => {
18+
if (response.status !== 200) return;
19+
20+
const svg = await response.text();
21+
const parser = new DOMParser();
22+
const doc = parser.parseFromString(svg, "image/svg+xml");
23+
const textNode = doc.querySelector("text");
24+
25+
if (!textNode || !textNode.textContent) return;
26+
27+
const parts = textNode.textContent.split(" / ");
28+
if (parts.length !== 2) return;
29+
30+
const [total, today] = parts;
31+
setData({ total, today });
32+
})
33+
.catch(() => {
34+
// 与原逻辑一致:忽略错误
35+
});
36+
}, []);
37+
38+
if (!data) return <></>;
39+
40+
return (
41+
<div className="hits">
42+
<strong>访问量</strong> <span title="今日访问">{data.today}</span>
43+
{" / "}
44+
<span title="总访问">{data.total}</span>
45+
</div>
46+
);
47+
};
48+
49+
export default HitsInfo;
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
---
22
import DefaultPageTitle from "@astrojs/starlight/components/PageTitle.astro";
3+
import HitsInfo from "@components/react/HitsInfo";
34
45
const { contributors } = Astro.locals.starlightRoute.entry.data;
6+
const showContributors = Array.isArray(contributors) && contributors.length > 0;
57
---
68

79
<DefaultPageTitle />
810

9-
{
10-
Array.isArray(contributors) && contributors.length > 0 && (
11-
<p>
12-
<strong>{Astro.locals.t("page.contributors")}</strong> {contributors.join(" ")}
13-
</p>
14-
)
15-
}
11+
<div class="info">
12+
{
13+
showContributors && (
14+
<div class="contributors">
15+
<strong>{Astro.locals.t("page.contributors")}</strong> {contributors.join(" ")}
16+
</div>
17+
)
18+
}
19+
<HitsInfo client:load />
20+
</div>

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"compilerOptions": {
33
"paths": {
44
"@components/*": ["./src/components/*"]
5-
}
5+
},
6+
"jsx": "react-jsx",
7+
"jsxImportSource": "react"
68
},
79
"extends": "astro/tsconfigs/strict",
810
"include": [".astro/types.d.ts", "**/*"],

0 commit comments

Comments
 (0)