Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/BackButton.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import IconChevronLeft from "@/assets/icons/IconChevronLeft.svg";
import LinkButton from "./LinkButton.astro";
import { SITE } from "@/config";
import { getUrl } from "@/utils/getUrl";
---

{
SITE.showBackButton && (
<div class="app-layout flex items-center justify-start">
<LinkButton
id="back-button"
href="/"
href={getUrl("/")}
class="focus-outline -ms-2 mt-8 mb-2 hover:text-foreground/75"
>
<IconChevronLeft class="inline-block size-6 rtl:rotate-180" />
Expand Down
6 changes: 4 additions & 2 deletions src/components/Breadcrumb.astro
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ if (breadcrumbList[0] === "tags" && !isNaN(Number(breadcrumbList[2]))) {
function getDisplayName(path: string): string {
return pathNameMap[path] || path;
}

import { getUrl } from "@/utils/getUrl";

Copilot AI Jan 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import statement is placed after the executable code (lines 3-38) instead of at the top of the frontmatter section. Move this import to the top with other imports for consistency and better readability.

Copilot uses AI. Check for mistakes.
---

<nav class="app-layout mt-8 mb-1" aria-label="breadcrumb">
<ul
class="font-light [&>li]:inline [&>li:not(:last-child)>a]:hover:opacity-100"
>
<li>
<a href="/" class="opacity-80">首页</a>
<a href={getUrl("/")} class="opacity-80">首页</a>
<span aria-hidden="true" class="opacity-80">&raquo;</span>
</li>
{
Expand All @@ -59,7 +61,7 @@ function getDisplayName(path: string): string {
</li>
) : (
<li>
<a href={`/${breadcrumb}.html`} class="opacity-70">
<a href={getUrl(`/${breadcrumb}.html`)} class="opacity-70">
{getDisplayName(breadcrumb)}
</a>
<span aria-hidden="true">&raquo;</span>
Expand Down
16 changes: 10 additions & 6 deletions src/components/Header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import IconMenuDeep from "@/assets/icons/IconMenuDeep.svg";
import IconLogo from "@/assets/icons/IconLogo.svg";
import LinkButton from "./LinkButton.astro";
import { SITE } from "@/config";
import { getUrl } from "@/utils/getUrl";

const { pathname } = Astro.url;

Expand Down Expand Up @@ -44,7 +45,7 @@ const isActive = (path: string) => {
]}
>
<a
href="/"
href={getUrl("/")}
class="absolute flex items-center gap-2 py-1 whitespace-nowrap sm:static sm:my-auto"
>
<IconLogo class="size-7 sm:size-8" />
Expand Down Expand Up @@ -77,20 +78,23 @@ const isActive = (path: string) => {
>
<li class="col-span-2">
<a
href="/posts.html"
href={getUrl("/posts.html")}
class:list={{ "active-nav": isActive("/posts") }}
>
文章
</a>
</li>
<li class="col-span-2">
<a href="/tags.html" class:list={{ "active-nav": isActive("/tags") }}>
<a
href={getUrl("/tags.html")}
class:list={{ "active-nav": isActive("/tags") }}
>
标签
</a>
</li>
<li class="col-span-2">
<a
href="/about.html"
href={getUrl("/about.html")}
class:list={{ "active-nav": isActive("/about") }}
>
关于
Expand All @@ -100,7 +104,7 @@ const isActive = (path: string) => {
SITE.showArchives && (
<li class="col-span-2">
<LinkButton
href="/archives.html"
href={getUrl("/archives.html")}
class:list={[
"focus-outline flex justify-center p-3 sm:p-1",
{
Expand All @@ -118,7 +122,7 @@ const isActive = (path: string) => {
}
<li class="col-span-1 flex items-center justify-center">
<LinkButton
href="/search.html"
href={getUrl("/search.html")}
class:list={[
"focus-outline flex p-3 sm:p-1",
{ "[&>svg]:stroke-accent": isActive("/search") },
Expand Down
5 changes: 3 additions & 2 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Font } from "astro:assets";
import { ClientRouter } from "astro:transitions";
import { PUBLIC_GOOGLE_SITE_VERIFICATION } from "astro:env/client";
import { SITE } from "@/config";
import { getUrl } from "@/utils/getUrl";
import "@/styles/global.css";

type Props = {
Expand Down Expand Up @@ -57,7 +58,7 @@ const structuredData = {
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="icon" type="image/svg+xml" href={getUrl("/favicon.svg")} />
<link rel="canonical" href={canonicalURL} />
<meta name="generator" content={Astro.generator} />

Expand All @@ -72,7 +73,7 @@ const structuredData = {
<meta name="title" content={title} />
<meta name="description" content={description} />
<meta name="author" content={author} />
<link rel="sitemap" href="/sitemap-index.xml" />
<link rel="sitemap" href={getUrl("/sitemap-index.xml")} />

<!-- Open Graph / Facebook -->
<meta property="og:title" content={title} />
Expand Down
3 changes: 2 additions & 1 deletion src/pages/404.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Header from "@/components/Header.astro";
import Footer from "@/components/Footer.astro";
import LinkButton from "@/components/LinkButton.astro";
import { SITE } from "@/config";
import { getUrl } from "@/utils/getUrl";
---

<Layout title={`404 Not Found | ${SITE.title}`}>
Expand All @@ -18,7 +19,7 @@ import { SITE } from "@/config";
<span aria-hidden="true">¯\_(ツ)_/¯</span>
<p class="mt-4 text-2xl sm:text-3xl">Page Not Found</p>
<LinkButton
href="/"
href={getUrl("/")}
class="my-6 text-lg underline decoration-dashed underline-offset-8"
>
Go back home
Expand Down
9 changes: 5 additions & 4 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import IconRss from "@/assets/icons/IconRss.svg";
import IconArrowRight from "@/assets/icons/IconArrowRight.svg";
import { SITE } from "@/config";
import { SOCIALS } from "@/constants";
import { getUrl } from "@/utils/getUrl";

const posts = await getCollection("blog");

Expand All @@ -28,7 +29,7 @@ const recentPosts = sortedPosts;
</h1>
<a
target="_blank"
href="/rss.xml"
href={getUrl("/rss.xml")}
class="inline-block"
aria-label="rss feed"
title="RSS Feed"
Expand All @@ -53,13 +54,13 @@ const recentPosts = sortedPosts;
欢迎阅读
<LinkButton
class="underline decoration-dashed underline-offset-4 hover:text-accent"
href="/posts.html"
href={getUrl("/posts.html")}
>
全部文章
</LinkButton> 或了解
<LinkButton
class="underline decoration-dashed underline-offset-4 hover:text-accent"
href="/about.html"
href={getUrl("/about.html")}
>
关于我
</LinkButton>。
Expand Down Expand Up @@ -121,7 +122,7 @@ const recentPosts = sortedPosts;
}

<div class="my-8 text-center">
<LinkButton href="/posts.html">
<LinkButton href={getUrl("/posts.html")}>
全部文章
<IconArrowRight class="inline-block rtl:-rotate-180" />
</LinkButton>
Expand Down
19 changes: 19 additions & 0 deletions src/utils/getUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 获取带 base 路径的 URL
* @param path - 路径,如 "/" 或 "/posts.html"
* @returns 完整路径,如 "/MyBlog/" 或 "/MyBlog/posts.html"
*/
export function getUrl(path: string): string {
const base = (import.meta.env.BASE_URL || "/").replace(/\/$/, "");
// 如果 path 已经是完整 URL 或以 # 开头,直接返回
if (path.startsWith("http") || path.startsWith("#")) {
return path;
}
// 确保 path 以 / 开头
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
// 如果 base 是 /,直接返回 path
if (base === "") {
return normalizedPath;
}
return `${base}${normalizedPath}`;
}
Loading