feat: 可配置站点 URL 风格

- 新增 urlStyle 工具与测试:postPath/userPath/sitePagePath 统一生成链接
- 管理端新增 UrlStylePanel 设置面板,settings 服务与 handler 适配
- 全站路由链接与 JSON-LD/sitemap 切换为统一 URL 生成
- middleware 与相关组件适配
This commit is contained in:
2026-09-27 04:01:42 +08:00
parent 1d862a0dd3
commit 7a44c1f2cf
64 changed files with 1183 additions and 200 deletions

View File

@@ -12,6 +12,8 @@ import { formatRelative } from "@/lib/format";
import type {
UserProfile, UserCommentsResponse, UserFavoritesResponse, UserFollowingResponse, PostListItem,
} from "@/lib/api";
import { postPath, userPath } from "@/lib/urlStyle";
import { getPublicSettingsCached } from "@/lib/serverData";
/** 本页卡片统一阴影;用 color-mix 让暗色主题自动加深,避免写死浅色阴影 */
const CARD_SHADOW = "0 1px 3px color-mix(in srgb, #000 18%, transparent)";
@@ -26,11 +28,12 @@ const SORTS: { key: string; label: string }[] = [
];
/** 帖子卡(发帖列表与收藏列表共用) */
function PostCardItem({ post, allowComments }: { post: PostListItem; allowComments?: boolean }) {
async function PostCardItem({ post, allowComments }: { post: PostListItem; allowComments?: boolean }) {
const urlStyle = (await getPublicSettingsCached()).url_style;
const pinned = post.pinned > 0;
return (
<Link
href={`/post/${post.id}`}
href={postPath(urlStyle, post.id)}
className={`${ITEM_BASE} ${
pinned
? "bg-[color-mix(in_srgb,#fde68a_22%,var(--panel))] border-[color-mix(in_srgb,#fde68a_70%,var(--line))] hover:bg-[color-mix(in_srgb,#fde68a_30%,var(--panel))]"
@@ -139,7 +142,7 @@ function ErrorState({ retryHref }: { retryHref: string }) {
* tab 与分页全部走 URL(?tab=xxx&page=N&sort=xxx),刷新/后退/分享链接均保持一致。
* Tab 切换栏已移至 page.tsx 统一渲染(含 settings / security),此组件仅负责列表类 tab。
*/
export default function UserProfileContent({
export default async function UserProfileContent({
userId,
profile,
tab,
@@ -168,6 +171,7 @@ export default function UserProfileContent({
isOwner: boolean;
allowComments?: boolean;
}) {
const urlStyle = (await getPublicSettingsCached()).url_style;
const displayName = profile.user.nickname || profile.user.username;
const paginationQuery: Record<string, string> =
tab === "comments" ? { tab: "comments" }
@@ -189,7 +193,7 @@ export default function UserProfileContent({
{SORTS.map((s) => (
<Link
key={s.key}
href={s.key === "new" ? `/u/${userId}` : `/u/${userId}?sort=${s.key}`}
href={s.key === "new" ? userPath(urlStyle, Number(userId)) : `${userPath(urlStyle, Number(userId))}?sort=${s.key}`}
className="j13-chip"
aria-current={sort === s.key ? "page" : undefined}
>
@@ -215,13 +219,13 @@ export default function UserProfileContent({
total={profile.posts_total}
page={page}
size={20}
basePath={`/u/${userId}`}
basePath={userPath(urlStyle, Number(userId))}
query={paginationQuery}
/>
</div>
) : tab === "favorites" ? (
favoriteError ? (
<ErrorState retryHref={`/u/${userId}?tab=favorites`} />
<ErrorState retryHref={`${userPath(urlStyle, Number(userId))}?tab=favorites`} />
) : favoriteData && favoriteData.posts.length > 0 ? (
<div>
<div className="mb-3 flex flex-wrap items-center justify-between gap-2">
@@ -238,7 +242,7 @@ export default function UserProfileContent({
total={favoriteData.total}
page={favoriteData.page}
size={20}
basePath={`/u/${userId}`}
basePath={userPath(urlStyle, Number(userId))}
query={paginationQuery}
/>
</div>
@@ -250,7 +254,7 @@ export default function UserProfileContent({
)
) : tab === "following" ? (
followingError ? (
<ErrorState retryHref={`/u/${userId}?tab=following`} />
<ErrorState retryHref={`${userPath(urlStyle, Number(userId))}?tab=following`} />
) : followingData && followingData.users.length > 0 ? (
<div>
<div className="mb-3 flex flex-wrap items-center justify-between gap-2">
@@ -265,7 +269,7 @@ export default function UserProfileContent({
className="group flex items-center gap-3 rounded-xl border px-5 py-4 transition-[background-color,box-shadow,border-color] duration-150 bg-[var(--panel)] border-[var(--line)] hover:bg-[color-mix(in_srgb,var(--ink)_3.5%,var(--panel))]"
style={{ boxShadow: CARD_SHADOW }}
>
<Link href={`/u/${fu.id}`} className="flex min-w-0 flex-1 items-center gap-3">
<Link href={userPath(urlStyle, fu.id)} className="flex min-w-0 flex-1 items-center gap-3">
<Avatar name={fu.nickname || fu.username} src={fu.avatar || undefined} size={40} />
<div className="min-w-0">
<div className="flex items-center gap-1.5">
@@ -291,7 +295,7 @@ export default function UserProfileContent({
total={followingData.total}
page={followingData.page}
size={20}
basePath={`/u/${userId}`}
basePath={userPath(urlStyle, Number(userId))}
query={paginationQuery}
/>
</div>
@@ -302,14 +306,14 @@ export default function UserProfileContent({
/>
)
) : commentError ? (
<ErrorState retryHref={`/u/${userId}?tab=comments`} />
<ErrorState retryHref={`${userPath(urlStyle, Number(userId))}?tab=comments`} />
) : commentData && (commentData.comments?.length ?? 0) > 0 ? (
<div>
<div className="panel overflow-hidden">
{(commentData.comments ?? []).map((c) => (
<Link
key={c.id}
href={`/post/${c.post_id}#comment-${c.id}`}
href={`${postPath(urlStyle, c.post_id)}#comment-${c.id}`}
className="group block px-4 sm:px-5 py-4 border-t transition-colors hover:bg-[var(--item-hover-bg)] focus-visible:bg-[var(--item-hover-bg)]"
style={{ borderColor: "var(--line)" }}
>
@@ -335,7 +339,7 @@ export default function UserProfileContent({
total={commentData.total}
page={commentData.page}
size={20}
basePath={`/u/${userId}`}
basePath={userPath(urlStyle, Number(userId))}
query={paginationQuery}
/>
</div>

View File

@@ -28,6 +28,7 @@ import FollowButton from "@/components/FollowButton";
import StaffUserMenu from "@/components/StaffUserMenu";
import { requestCanonical } from "@/lib/canonicalMeta";
import { absoluteCanonical, canonicalPage } from "@/lib/siteUrl";
import { userPath } from "@/lib/urlStyle";
import { profileJsonLd } from "@/lib/jsonLd";
import JsonLd from "@/components/JsonLd";
@@ -52,7 +53,9 @@ export async function generateMetadata({ params, searchParams }: UserPageProps):
const tab = parseTab(sp.tab);
try {
const data = await fetchUserProfile(id, 1);
const allowComments = (await getPublicSettingsCached()).allow_comments !== false;
const pub = await getPublicSettingsCached();
const allowComments = pub.allow_comments !== false;
const urlStyle = pub.url_style;
const title = data.user.nickname || data.user.username;
const description = allowComments
? `查看 ${data.user.nickname} 的资料、发帖与评论`
@@ -71,7 +74,7 @@ export async function generateMetadata({ params, searchParams }: UserPageProps):
return {
title,
description,
...(await requestCanonical(`/u/${data.user.id}`, query)),
...(await requestCanonical(userPath(urlStyle, data.user.id), query)),
};
} catch {
return { title: "用户不存在" };
@@ -93,6 +96,7 @@ export default async function UserPage({ params, searchParams }: UserPageProps)
const me = await getMeCached(cookie || undefined);
const settings = await getPublicSettingsCached();
const allowComments = settings.allow_comments !== false;
const urlStyle = settings.url_style;
const commentsRequireLogin = settings.comments_require_login === true;
const allowMessages = settings.allow_messages !== false;
const canReadComments = allowComments && (!commentsRequireLogin || !!me.user);
@@ -206,7 +210,8 @@ export default async function UserPage({ params, searchParams }: UserPageProps)
]
: [];
const visibleNav = [...mainNavItems, ...settingsNavItems];
const tabHref = (t: UserTab) => (t === "posts" ? `/u/${id}` : `/u/${id}?tab=${t}`);
const tabHref = (t: UserTab) =>
t === "posts" ? userPath(urlStyle, Number(id)) : `${userPath(urlStyle, Number(id))}?tab=${t}`;
const profileQuery: Record<string, string | number> = {};
if (canReadComments && effectiveTab === "comments") profileQuery.tab = "comments";
@@ -222,7 +227,8 @@ export default async function UserPage({ params, searchParams }: UserPageProps)
name: u.nickname || u.username,
username: u.username,
signature: (u.signature || "").trim() || undefined,
pageUrl: absoluteCanonical(`/u/${u.id}`, profileQuery),
pageUrl: absoluteCanonical(userPath(urlStyle, u.id), profileQuery),
urlStyle,
});
/** 本页卡片统一阴影;暗色随 color-mix 自动加深 */
@@ -255,12 +261,12 @@ export default async function UserPage({ params, searchParams }: UserPageProps)
{/* 本人操作按钮 / 访客私聊:绝对定位在卡片右上角 */}
{isOwner ? (
<div className="absolute top-3 right-3 z-20 flex items-center gap-2">
<Link href={`/u/${id}?tab=settings`} className="btn btn-line btn-sm">
<Link href={`${userPath(urlStyle, Number(id))}?tab=settings`} className="btn btn-line btn-sm">
<PenLine size={13.5} />
<span className="hidden sm:inline">编辑资料</span>
</Link>
<Link
href={`/u/${id}?tab=security`}
href={`${userPath(urlStyle, Number(id))}?tab=security`}
aria-label="安全设置"
data-tip="安全设置"
className="inline-flex w-8 h-8 items-center justify-center rounded-full border transition-colors duration-150 hover:border-[var(--accent)] hover:text-[var(--accent)]"