fix: 改进评论获取接口的slug规范化处理
修复当post_slug参数为根路径或包含查询参数/锚点时,slug列表生成逻辑不正确的问题。现在会先去除查询参数和锚点,然后正确处理根路径和带斜杠的变体。 新增单元测试验证各种slug格式的规范化行为,包括相对路径、完整URL和根路径。
This commit is contained in:
@@ -30,7 +30,15 @@ export const getComments = async (c: Context<{ Bindings: Bindings }>) => {
|
||||
slugList = Array.from(new Set([withSlash, withoutSlash]))
|
||||
}
|
||||
} catch {
|
||||
slugList = [postSlug]
|
||||
const path = postSlug.split('?')[0].split('#')[0]
|
||||
if (path === '/' || path === '') {
|
||||
slugList = ['/']
|
||||
} else {
|
||||
const hasTrailingSlash = path.endsWith('/')
|
||||
const withSlash = hasTrailingSlash ? path : path + '/'
|
||||
const withoutSlash = hasTrailingSlash ? path.slice(0, -1) : path
|
||||
slugList = Array.from(new Set([withSlash, withoutSlash]))
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user