From 440b2a757b018ff0b67e7fd78d83d9324cc721f1 Mon Sep 17 00:00:00 2001 From: Benny Guo Date: Thu, 10 Aug 2023 00:18:52 +0800 Subject: [PATCH] fix: path with extra hash at the end when reloaded --- src/utils/comments/twikoo-api.ts | 5 +++-- src/utils/comments/valine-api.ts | 4 +++- src/utils/comments/waline-api.ts | 6 +++--- src/utils/index.ts | 8 ++++++++ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/utils/comments/twikoo-api.ts b/src/utils/comments/twikoo-api.ts index b6192593..86a2e333 100644 --- a/src/utils/comments/twikoo-api.ts +++ b/src/utils/comments/twikoo-api.ts @@ -1,4 +1,4 @@ -import { RecentComment, formatTime } from '..' +import { RecentComment, cleanPath, formatTime } from '..' import { getGravatar, getGravatarUrl } from './gravatar' declare const twikoo: any @@ -51,9 +51,10 @@ export const twikooCommentsCount = async ( envId: string, path: string ): Promise => { + const cleanedPath = cleanPath(path) const commentCounts = await twikoo.getCommentsCount({ envId, - urls: [path], + urls: [cleanedPath], includeReply: true }) return commentCounts[0] ? Number(commentCounts[0].count) : 0 diff --git a/src/utils/comments/valine-api.ts b/src/utils/comments/valine-api.ts index 0f4605ab..39e22494 100644 --- a/src/utils/comments/valine-api.ts +++ b/src/utils/comments/valine-api.ts @@ -1,3 +1,5 @@ +import { cleanPath } from '..' + declare const Valine: any interface ValineConfig { @@ -36,6 +38,6 @@ export const valineInit = ({ meta: meta ?? [], requiredFields: requiredFields ?? [], avatarForce, - path + path: cleanPath(path) }) } diff --git a/src/utils/comments/waline-api.ts b/src/utils/comments/waline-api.ts index a215e504..bda55d96 100644 --- a/src/utils/comments/waline-api.ts +++ b/src/utils/comments/waline-api.ts @@ -7,7 +7,7 @@ import { RecentComments // @ts-expect-error } from 'https://unpkg.com/@waline/client@v2/dist/waline.mjs' -import { filterHTMLContent, formatTime } from '..' +import { cleanPath, filterHTMLContent, formatTime } from '..' import { PluginsData } from '@/models/ThemeConfig.class' type WalinePlugin = PluginsData['waline'] @@ -78,14 +78,14 @@ export const walineInit = ({ export const walinePageViewInit = (serverURL: string, path: string) => { pageviewCount({ serverURL, - path + path: cleanPath(path) }) } export const walineCommentViewInit = (serverURL: string, path: string) => { commentCount({ serverURL, - path + path: cleanPath(path) }) } diff --git a/src/utils/index.ts b/src/utils/index.ts index 601e6356..18e27e0c 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -148,3 +148,11 @@ export function convertToLocale(label: string) { return label } + +export function cleanPath(path: string) { + if (path.at(-1) === '/') { + return path.slice(0, -1) + } + + return path +}