From a0eab19f31131f18f5e5cffc94f1a1c90d93778c Mon Sep 17 00:00:00 2001 From: ClovertaTheTrilobita Date: Sat, 4 Apr 2026 12:48:25 +0300 Subject: [PATCH] refactor(comments): refactor comments from giscus to remark42 --- .env.example | 3 +- astro.config.mjs | 3 +- package-lock.json | 273 ++++++++++++++ package.json | 1 + src/components/Comments.astro | 341 ------------------ src/components/Giscus.astro | 67 ---- src/components/Posts/PostItem.astro | 6 +- src/components/Posts/PostList.astro | 86 +---- src/components/remark42-counter.svelte | 30 ++ src/components/remark42-embed.svelte | 41 +++ .../remark42-latest-comments.svelte | 231 ++++++++++++ src/layouts/MarkdownPostLayout.astro | 6 +- src/pages/[lang]/index.astro | 4 +- src/pages/api/comments.json.ts | 231 ------------ src/styles/global.css | 2 +- svelte.config.js | 5 + 16 files changed, 597 insertions(+), 733 deletions(-) delete mode 100644 src/components/Comments.astro delete mode 100644 src/components/Giscus.astro create mode 100644 src/components/remark42-counter.svelte create mode 100644 src/components/remark42-embed.svelte create mode 100644 src/components/remark42-latest-comments.svelte delete mode 100644 src/pages/api/comments.json.ts create mode 100644 svelte.config.js diff --git a/.env.example b/.env.example index 7542537..15d7730 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,5 @@ GITHUB_TOKEN= GISCUS_REPO_OWNER= GISCUS_REPO_NAME= GISCUS_CATEGORY_ID= -GISCUS_DATA_REPO_ID= \ No newline at end of file +GISCUS_DATA_REPO_ID= +PUBLIC_COMMENT_SERVER_URL= \ No newline at end of file diff --git a/astro.config.mjs b/astro.config.mjs index aa46ad3..73a5245 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,6 +1,7 @@ // @ts-check import { defineConfig } from 'astro/config'; import sitemap from "@astrojs/sitemap"; +import svelte from "@astrojs/svelte"; // https://astro.build/config import { fileURLToPath } from 'node:url' @@ -16,5 +17,5 @@ export default defineConfig({ redirects: { "/": "/zh", }, - integrations: [sitemap()], + integrations: [sitemap(), svelte()], }) diff --git a/package-lock.json b/package-lock.json index 4fa092b..5bed1fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "@astrojs/rss": "^4.0.17", "@astrojs/sitemap": "^3.7.1", + "@astrojs/svelte": "^8.0.4", "astro": "^6.0.8", "url": "^0.11.4" }, @@ -98,6 +99,25 @@ "zod": "^4.3.6" } }, + "node_modules/@astrojs/svelte": { + "version": "8.0.4", + "resolved": "https://registry.npmjs.org/@astrojs/svelte/-/svelte-8.0.4.tgz", + "integrity": "sha512-c5m3chjtgxBE3BzsE/bZbCFBkLPhq041rm2WJFaTIKGwt/3xNm/5efYCj23reuAcBsl4iYS8n2UwkAHQJzhkZA==", + "license": "MIT", + "dependencies": { + "@sveltejs/vite-plugin-svelte": "^6.2.4", + "svelte2tsx": "^0.7.52", + "vite": "^7.3.1" + }, + "engines": { + "node": ">=22.12.0" + }, + "peerDependencies": { + "astro": "^6.0.0", + "svelte": "^5.43.6", + "typescript": "^5.3.3" + } + }, "node_modules/@astrojs/telemetry": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/@astrojs/telemetry/-/telemetry-3.3.0.tgz", @@ -1155,12 +1175,55 @@ "url": "https://opencollective.com/libvips" } }, + "node_modules/@jridgewell/gen-mapping": { + "version": "0.3.13", + "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz", + "integrity": "sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==", + "license": "MIT", + "peer": true, + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.0", + "@jridgewell/trace-mapping": "^0.3.24" + } + }, + "node_modules/@jridgewell/remapping": { + "version": "2.3.5", + "resolved": "https://registry.npmjs.org/@jridgewell/remapping/-/remapping-2.3.5.tgz", + "integrity": "sha512-LI9u/+laYG4Ds1TDKSJW2YPrIlcVYOwi2fUC6xB43lueCjgxV4lffOCZCtYFiH6TNOX+tQKXx97T4IKHbhyHEQ==", + "license": "MIT", + "peer": true, + "dependencies": { + "@jridgewell/gen-mapping": "^0.3.5", + "@jridgewell/trace-mapping": "^0.3.24" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "license": "MIT", + "peer": true, + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/@jridgewell/sourcemap-codec": { "version": "1.5.5", "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.5.tgz", "integrity": "sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og==", "license": "MIT" }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.31", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.31.tgz", + "integrity": "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==", + "license": "MIT", + "peer": true, + "dependencies": { + "@jridgewell/resolve-uri": "^3.1.0", + "@jridgewell/sourcemap-codec": "^1.4.14" + } + }, "node_modules/@npmcli/fs": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/@npmcli/fs/-/fs-2.1.2.tgz", @@ -1682,6 +1745,53 @@ "integrity": "sha512-83yeghZ2xxin3Nj8z1NMd/NCuca+gsYXswywDy5bHvwlWL8tpTQmzGeUuHd9FC3E/SBEMvzJRwWEOz5gGes9Qg==", "license": "MIT" }, + "node_modules/@sveltejs/acorn-typescript": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@sveltejs/acorn-typescript/-/acorn-typescript-1.0.9.tgz", + "integrity": "sha512-lVJX6qEgs/4DOcRTpo56tmKzVPtoWAaVbL4hfO7t7NVwl9AAXzQR6cihesW1BmNMPl+bK6dreu2sOKBP2Q9CIA==", + "license": "MIT", + "peer": true, + "peerDependencies": { + "acorn": "^8.9.0" + } + }, + "node_modules/@sveltejs/vite-plugin-svelte": { + "version": "6.2.4", + "resolved": "https://registry.npmjs.org/@sveltejs/vite-plugin-svelte/-/vite-plugin-svelte-6.2.4.tgz", + "integrity": "sha512-ou/d51QSdTyN26D7h6dSpusAKaZkAiGM55/AKYi+9AGZw7q85hElbjK3kEyzXHhLSnRISHOYzVge6x0jRZ7DXA==", + "license": "MIT", + "dependencies": { + "@sveltejs/vite-plugin-svelte-inspector": "^5.0.0", + "deepmerge": "^4.3.1", + "magic-string": "^0.30.21", + "obug": "^2.1.0", + "vitefu": "^1.1.1" + }, + "engines": { + "node": "^20.19 || ^22.12 || >=24" + }, + "peerDependencies": { + "svelte": "^5.0.0", + "vite": "^6.3.0 || ^7.0.0" + } + }, + "node_modules/@sveltejs/vite-plugin-svelte-inspector": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@sveltejs/vite-plugin-svelte-inspector/-/vite-plugin-svelte-inspector-5.0.2.tgz", + "integrity": "sha512-TZzRTcEtZffICSAoZGkPSl6Etsj2torOVrx6Uw0KpXxrec9Gg6jFWQ60Q3+LmNGfZSxHRCZL7vXVZIWmuV50Ig==", + "license": "MIT", + "dependencies": { + "obug": "^2.1.0" + }, + "engines": { + "node": "^20.19 || ^22.12 || >=24" + }, + "peerDependencies": { + "@sveltejs/vite-plugin-svelte": "^6.0.0-next.0", + "svelte": "^5.0.0", + "vite": "^6.3.0 || ^7.0.0" + } + }, "node_modules/@tootallnate/once": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz", @@ -1782,12 +1892,33 @@ "@types/node": "*" } }, + "node_modules/@types/trusted-types": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz", + "integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==", + "license": "MIT", + "peer": true + }, "node_modules/@types/unist": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.3.tgz", "integrity": "sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==", "license": "MIT" }, + "node_modules/@typescript-eslint/types": { + "version": "8.58.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-8.58.0.tgz", + "integrity": "sha512-O9CjxypDT89fbHxRfETNoAnHj/i6IpRK0CvbVN3qibxlLdo5p5hcLmUuCCrHMpxiWSwKyI8mCP7qRNYuOJ0Uww==", + "license": "MIT", + "peer": true, + "engines": { + "node": "^18.18.0 || ^20.9.0 || >=21.1.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, "node_modules/@ungap/structured-clone": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.3.0.tgz", @@ -1811,6 +1942,19 @@ "dev": true, "license": "ISC" }, + "node_modules/acorn": { + "version": "8.16.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.16.0.tgz", + "integrity": "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==", + "license": "MIT", + "peer": true, + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/agent-base": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", @@ -2760,6 +2904,21 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/dedent-js": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dedent-js/-/dedent-js-1.0.1.tgz", + "integrity": "sha512-OUepMozQULMLUmhxS95Vudo0jb0UchLimi3+pQ2plj61Fcy8axbP9hbiD4Sz6DPqn6XG3kfmziVfQ1rSys5AJQ==", + "license": "MIT" + }, + "node_modules/deepmerge": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz", + "integrity": "sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/define-data-property": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz", @@ -3134,6 +3293,24 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/esm-env": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/esm-env/-/esm-env-1.2.2.tgz", + "integrity": "sha512-Epxrv+Nr/CaL4ZcFGPJIYLWFom+YeV1DqMLHJoEd9SYRxNbaFruBwfEX/kkHUJf55j2+TUbmDcmuilbP1TmXHA==", + "license": "MIT", + "peer": true + }, + "node_modules/esrap": { + "version": "2.2.4", + "resolved": "https://registry.npmjs.org/esrap/-/esrap-2.2.4.tgz", + "integrity": "sha512-suICpxAmZ9A8bzJjEl/+rLJiDKC0X4gYWUxT6URAWBLvlXmtbZd5ySMu/N2ZGEtMCAmflUDPSehrP9BQcsGcSg==", + "license": "MIT", + "peer": true, + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.4.15", + "@typescript-eslint/types": "^8.2.0" + } + }, "node_modules/estree-walker": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz", @@ -4117,6 +4294,16 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/is-reference": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-3.0.3.tgz", + "integrity": "sha512-ixkJoqQvAP88E6wLydLGGqCJsrFUnqoH6HnaczB8XmDH1oaWU+xxdptvikTgaEhtZ53Ky6YXiBuUI2WXLMCwjw==", + "license": "MIT", + "peer": true, + "dependencies": { + "@types/estree": "^1.0.6" + } + }, "node_modules/is-relative": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-relative/-/is-relative-1.0.0.tgz", @@ -4336,6 +4523,13 @@ "dev": true, "license": "MIT" }, + "node_modules/locate-character": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/locate-character/-/locate-character-3.0.0.tgz", + "integrity": "sha512-SW13ws7BjaeJ6p7Q6CO2nchbYEc3X3J6WrmTTDto7yMPqVSZTUyY5Tjbid+Ab8gLnATtygYtiDIJGQRRn2ZOiA==", + "license": "MIT", + "peer": true + }, "node_modules/locate-path": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", @@ -6662,6 +6856,12 @@ "node": ">=11.0.0" } }, + "node_modules/scule": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/scule/-/scule-1.3.0.tgz", + "integrity": "sha512-6FtHJEvt+pVMIB9IBY+IcCJ6Z5f1iQnytgyfKMhDKgmzYG+TeH/wx1y3l27rshSbLiSanrR9ffZDrEsmjlQF2g==", + "license": "MIT" + }, "node_modules/semver": { "version": "7.7.4", "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz", @@ -7117,6 +7317,58 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/svelte": { + "version": "5.55.1", + "resolved": "https://registry.npmjs.org/svelte/-/svelte-5.55.1.tgz", + "integrity": "sha512-QjvU7EFemf6mRzdMGlAFttMWtAAVXrax61SZYHdkD6yoVGQ89VeyKfZD4H1JrV1WLmJBxWhFch9H6ig/87VGjw==", + "license": "MIT", + "peer": true, + "dependencies": { + "@jridgewell/remapping": "^2.3.4", + "@jridgewell/sourcemap-codec": "^1.5.0", + "@sveltejs/acorn-typescript": "^1.0.5", + "@types/estree": "^1.0.5", + "@types/trusted-types": "^2.0.7", + "acorn": "^8.12.1", + "aria-query": "5.3.1", + "axobject-query": "^4.1.0", + "clsx": "^2.1.1", + "devalue": "^5.6.4", + "esm-env": "^1.2.1", + "esrap": "^2.2.4", + "is-reference": "^3.0.3", + "locate-character": "^3.0.0", + "magic-string": "^0.30.11", + "zimmerframe": "^1.1.2" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/svelte/node_modules/aria-query": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/aria-query/-/aria-query-5.3.1.tgz", + "integrity": "sha512-Z/ZeOgVl7bcSYZ/u/rh0fOpvEpq//LZmdbkXyc7syVzjPAhfOa9ebsdTSjEBDU4vs5nC98Kfduj1uFo0qyET3g==", + "license": "Apache-2.0", + "peer": true, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/svelte2tsx": { + "version": "0.7.53", + "resolved": "https://registry.npmjs.org/svelte2tsx/-/svelte2tsx-0.7.53.tgz", + "integrity": "sha512-ljVSwmnYRDHRm8+7ICP6QoAN7U7vgOFfPBLN6T745YWNYqRRSzHxlrzUVqMjYls2Un8MzJissfziy/38e6Deeg==", + "license": "MIT", + "dependencies": { + "dedent-js": "^1.0.1", + "scule": "^1.3.0" + }, + "peerDependencies": { + "svelte": "^3.55 || ^4.0.0-next.0 || ^4.0 || ^5.0.0-next.0", + "typescript": "^4.9.4 || ^5.0.0" + } + }, "node_modules/svgo": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/svgo/-/svgo-4.0.1.tgz", @@ -7400,6 +7652,20 @@ "dev": true, "license": "MIT" }, + "node_modules/typescript": { + "version": "5.9.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", + "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", + "license": "Apache-2.0", + "peer": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/ufo": { "version": "1.6.3", "resolved": "https://registry.npmjs.org/ufo/-/ufo-1.6.3.tgz", @@ -8186,6 +8452,13 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/zimmerframe": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/zimmerframe/-/zimmerframe-1.1.4.tgz", + "integrity": "sha512-B58NGBEoc8Y9MWWCQGl/gq9xBCe4IiKM0a2x7GZdQKOW5Exr8S1W24J6OgM1njK8xCRGvAJIL/MxXHf6SkmQKQ==", + "license": "MIT", + "peer": true + }, "node_modules/zod": { "version": "4.3.6", "resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz", diff --git a/package.json b/package.json index 8afaa90..f0651db 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dependencies": { "@astrojs/rss": "^4.0.17", "@astrojs/sitemap": "^3.7.1", + "@astrojs/svelte": "^8.0.4", "astro": "^6.0.8", "url": "^0.11.4" }, diff --git a/src/components/Comments.astro b/src/components/Comments.astro deleted file mode 100644 index 87f5f8f..0000000 --- a/src/components/Comments.astro +++ /dev/null @@ -1,341 +0,0 @@ ---- -import { getLangFromUrl } from "@/i18n"; - -const limit = Number(Astro.props.limit ?? 5); -const lang = getLangFromUrl(Astro.url); - -function formatDate(iso: string) { - return new Date(iso).toISOString().slice(0, 10); -} ---- - -
-

{lang === "zh" ? "最新评论" : "Latest Comments"}

- -
-
-
-
-
- - - - - - -
- - - - diff --git a/src/components/Giscus.astro b/src/components/Giscus.astro deleted file mode 100644 index 984b8bf..0000000 --- a/src/components/Giscus.astro +++ /dev/null @@ -1,67 +0,0 @@ ---- -const { term } = Astro.props; - -const repoOwner = import.meta.env.GISCUS_REPO_OWNER; -const repoName = import.meta.env.GISCUS_REPO_NAME; -const categoryId = import.meta.env.GISCUS_CATEGORY_ID; -const dataRepoId = import.meta.env.GISCUS_DATA_REPO_ID; -const repo = `${repoOwner}/${repoName}`; ---- - -
-

Comments

- - - - -
diff --git a/src/components/Posts/PostItem.astro b/src/components/Posts/PostItem.astro index 093ff9e..c566a77 100644 --- a/src/components/Posts/PostItem.astro +++ b/src/components/Posts/PostItem.astro @@ -1,5 +1,6 @@ --- import "@/styles/global.css"; +import Remark42Count from "@/components/remark42-counter.svelte"; const data = Astro.props; --- @@ -13,8 +14,9 @@ const data = Astro.props; {data.date} - 💬 {data.commentCount ?? 0} - ❤ {data.reactionCount ?? 0} + 💬 diff --git a/src/components/Posts/PostList.astro b/src/components/Posts/PostList.astro index 4dd27dd..327c775 100644 --- a/src/components/Posts/PostList.astro +++ b/src/components/Posts/PostList.astro @@ -3,77 +3,9 @@ import { getCollection } from "astro:content"; import PostItem from "./PostItem.astro"; import { getLangFromUrl } from "@/i18n"; -const token = import.meta.env.GITHUB_TOKEN; -const owner = import.meta.env.GISCUS_REPO_OWNER; -const name = import.meta.env.GISCUS_REPO_NAME; -const categoryId = import.meta.env.GISCUS_CATEGORY_ID; const lang = getLangFromUrl(Astro.url); const allPosts = await getCollection("blog"); -type ReactionGroup = { - users?: { - totalCount?: number; - }; -}; - -type DiscussionNode = { - title: string; - comments?: { - totalCount?: number; - }; - reactionGroups?: ReactionGroup[]; -}; - -function normalizePath(path: string) { - return path.replace(/^\/+|\/+$/g, ""); -} - -async function fetchDiscussionStats(): Promise { - // const token = import.meta.env.GITHUB_TOKEN; - if (!token) return []; - - const query = ` - query($owner: String!, $name: String!, $categoryId: ID!) { - repository(owner: $owner, name: $name) { - discussions(first: 100, categoryId: $categoryId) { - nodes { - title - comments(first: 0) { - totalCount - } - reactionGroups { - users { - totalCount - } - } - } - } - } - } - `; - - const res = await fetch("https://api.github.com/graphql", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify({ - query, - variables: { - owner: owner, - name: name, - categoryId: categoryId, - }, - }), - }); - - const json = await res.json(); - return (json?.data?.repository?.discussions?.nodes ?? []) as DiscussionNode[]; -} - -const discussions = await fetchDiscussionStats(); - const sortedPosts = [...allPosts].sort( (a, b) => new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime(), @@ -97,30 +29,14 @@ const latestPosts = filteredPosts.slice(0, 5); const [postLang, ...slugParts] = post.id.split("/"); const slug = slugParts.join("/"); - const pathname = `/${slug}/`; - - const matchedDiscussion = discussions.find((d: DiscussionNode) => { - return normalizePath(d.title) === normalizePath(pathname); - }); - - const reactionCount = - matchedDiscussion?.reactionGroups?.reduce( - (sum: number, group: ReactionGroup) => - sum + (group.users?.totalCount ?? 0), - 0, - ) ?? 0; - - const commentCount = matchedDiscussion?.comments?.totalCount ?? 0; - return ( ); }) diff --git a/src/components/remark42-counter.svelte b/src/components/remark42-counter.svelte new file mode 100644 index 0000000..4a91f01 --- /dev/null +++ b/src/components/remark42-counter.svelte @@ -0,0 +1,30 @@ + + + + + + + diff --git a/src/components/remark42-embed.svelte b/src/components/remark42-embed.svelte new file mode 100644 index 0000000..8fda99b --- /dev/null +++ b/src/components/remark42-embed.svelte @@ -0,0 +1,41 @@ + + +
diff --git a/src/components/remark42-latest-comments.svelte b/src/components/remark42-latest-comments.svelte new file mode 100644 index 0000000..dbf0ce3 --- /dev/null +++ b/src/components/remark42-latest-comments.svelte @@ -0,0 +1,231 @@ + + +
+

最新评论

+ + {#if !loaded} + + {/if} + +
+
+ + diff --git a/src/layouts/MarkdownPostLayout.astro b/src/layouts/MarkdownPostLayout.astro index 9bcf54d..24f8562 100644 --- a/src/layouts/MarkdownPostLayout.astro +++ b/src/layouts/MarkdownPostLayout.astro @@ -1,10 +1,11 @@ --- import BaseLayout from "./BaseLayout.astro"; -import Giscus from "@/components/Giscus.astro"; +import Remark42Embed from "@/components/remark42-embed.svelte"; import { getLangFromUrl, getTranslations } from "@/i18n"; import "@/styles/global.css"; const { frontmatter, lang, postId } = Astro.props; +const comments = lang === "zh" ? "评论区" : "comments"; const t = getTranslations(lang); --- @@ -47,7 +48,8 @@ const t = getTranslations(lang); - +

{comments}

+ diff --git a/src/pages/[lang]/index.astro b/src/pages/[lang]/index.astro index 7d4dea9..c334f1c 100644 --- a/src/pages/[lang]/index.astro +++ b/src/pages/[lang]/index.astro @@ -1,7 +1,7 @@ --- import BaseLayout from "@/layouts/BaseLayout.astro"; import PostList from "@/components/Posts/PostList.astro"; -import Comments from "@/components/Comments.astro"; +import Remark42LatestComments from "@/components/remark42-latest-comments.svelte"; import { getLangFromUrl, getTranslations } from "@/i18n"; import "@/styles/global.css"; @@ -50,7 +50,7 @@ const pageTitle = t.home.title;
- +