refactor: v2

This commit is contained in:
2026-08-15 20:55:56 +08:00
parent 990d4b0728
commit 560f6f29ea
37 changed files with 687 additions and 1506 deletions

View File

@@ -2,28 +2,6 @@
import Layout from "@/layouts/Layout.astro";
---
<Layout title="404">
<div class="not-found">
<h1>404 Not Found</h1>
<a class="back-home" href="/">返回首页</a>
</div>
<Layout title="404" description="这里什么也没有发现~">
</Layout>
<style>
.not-found {
flex: 1;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
gap: 20px;
}
.back-home {
background: black;
color: white;
padding: 10px 20px;
border-radius: 12px;
}
</style>

View File

@@ -1,198 +0,0 @@
---
import Layout from "@/layouts/Layout.astro";
import type {Archive} from "@/types/archive";
import {createHttp} from "@/lib/request";
const http = createHttp(Astro.request.headers);
const {data} = await http.get<ApiResponse<Archive>>("posts/archives");
---
<Layout title="归档">
<div class="page-content">
<div class="archive">
{
data.map((year) => (
<div class="archive-year">
<h2 class="year-title">
<span class="year-badge">{year.year}</span>
<span class="year-count">共 {year.total} 篇</span>
</h2>
<div>
{year.list.map((month) => (
<div class="month-group">
<h3 class="month-title">{month.month} 月</h3>
<ul class="post-list">
{month.list.map((post) => (
<li class="post-item">
<span class="post-date">
{post.published_at_display}
</span>
<a href={`/post/${post.slug}`}>{post.title}</a>
<span class="post-cat">{post.category_name}</span>
</li>
))}
</ul>
</div>
))}
</div>
</div>
))
}
</div>
</div>
</Layout>
<style>
:root {
--border: #e5e7eb;
--radius: 12px;
--shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}
.archive {
width: 100%;
max-width: 860px;
margin: 0 auto;
}
.archive-year {
margin-bottom: 40px;
}
.year-title {
display: flex;
align-items: center;
gap: 14px;
font-size: 24px;
font-weight: 700;
margin-bottom: 18px;
}
.year-title .year-badge {
background: linear-gradient(135deg, #4f6df5, #7b5cf0);
color: #fff;
padding: 8px 16px;
border-radius: 999px;
font-size: 20px;
}
.year-title .year-count {
font-size: 13px;
color: var(--text-secondary);
font-weight: 400;
}
.year-title::after {
content: "";
flex: 1;
height: 1px;
background: var(--border);
}
/* 月份分组 */
.month-group {
position: relative;
padding-left: 28px;
margin-bottom: 24px;
}
/* 时间轴竖线 */
.month-group::before {
content: "";
position: absolute;
left: 7px;
top: 8px;
bottom: 0;
width: 2px;
background: var(--border);
}
.month-title {
position: relative;
font-size: 17px;
font-weight: 700;
color: var(--primary);
margin-bottom: 10px;
}
:global(.dark) .month-title{
color: inherit;
}
/* 月份节点圆点 */
.month-title::before {
content: "";
position: absolute;
left: -26px;
top: 8px;
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--primary);
box-shadow: 0 0 0 3px var(--tag-bg);
}
.post-list {
list-style: none;
background: var(--card-bg);
border-radius: var(--radius);
box-shadow: var(--shadow);
overflow: hidden;
}
.post-item {
display: flex;
align-items: baseline;
gap: 14px;
padding: 18px 20px;
border-bottom: 1px solid var(--border);
transition: background 0.2s;
}
.post-item:last-child {
border-bottom: none;
}
.post-item:hover {
background: #f9faff;
}
:global(.dark) .post-item:hover {
background: none;
}
.post-date {
flex-shrink: 0;
font-size: 13px;
color: var(--text-secondary);
font-variant-numeric: tabular-nums;
width: 52px;
}
.post-item a {
flex: 1;
min-width: 0;
font-size: 15px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.post-item a:hover {
color: var(--primary);
}
:global(.dark) .post-item a:hover {
color: inherit;
}
.post-cat {
flex-shrink: 0;
font-size: 12px;
color: var(--primary);
background: var(--tag-bg);
padding: 2px 10px;
border-radius: 999px;
}
</style>

View File

@@ -1,6 +1,5 @@
---
const noindex = true;
import {metadata} from '@/consts.ts'
---
<!doctype html>

View File

@@ -1,25 +1,23 @@
---
import Layout from "@/layouts/Layout.astro";
import IndexView from "@/components/IndexView.astro";
import type { Post } from "@/types/post";
import { createHttp } from "@/lib/request";
import { metadata } from "@/consts.ts";
import {siteConfig} from "@/consts.ts";
import {createHttp} from "@/lib/request.ts";
import type {Post} from "@/types";
import IndexPage from "@/components/IndexPage.astro";
const {postPageSize, description} = siteConfig;
const http = createHttp(Astro.request.headers);
const { list, total } = await http.get<ApiPageResponse<Post>>("posts", {
const {list, total} = await http.get<ApiPageResponse<Post>>("posts", {
searchParams: {
page: 1,
page_size: metadata.postPageSize,
page_size: postPageSize,
},
});
const totalPage = Math.ceil(total / metadata.postPageSize);
const totalPage = Math.ceil(total / postPageSize);
---
<Layout description="天若有情天亦老,人间正道是沧桑。" title="博客首页">
<IndexView
posts={list}
page={1}
totalPage={totalPage}
/>
<Layout title="博客首页" description={description}>
<IndexPage page={1} totalPage={totalPage} posts={list}/>
</Layout>

View File

@@ -1,36 +1,34 @@
---
import Layout from "@/layouts/Layout.astro";
import type { Post } from "@/types/post";
import { createHttp } from "@/lib/request";
import { metadata } from "@/consts.ts";
import IndexView from "@/components/IndexView.astro";
import {siteConfig} from "@/consts.ts";
import {createHttp} from "@/lib/request.ts";
import type {Post} from "@/types";
import IndexPage from "@/components/IndexPage.astro";
const {page} = Astro.params;
const { page } = Astro.params;
const pageInt = Number(page);
if (isNaN(Number(page))) {
if (isNaN(pageInt) || pageInt <= 1) {
return Astro.redirect("/");
}
const {postPageSize,description} = siteConfig;
const http = createHttp(Astro.request.headers);
const { list, total } = await http.get<ApiPageResponse<Post>>("posts", {
const {list, total} = await http.get<ApiPageResponse<Post>>("posts", {
searchParams: {
page,
page_size: metadata.postPageSize,
page: pageInt,
page_size: postPageSize,
},
});
const totalPage = Math.ceil(total / postPageSize);
const totalPage = Math.ceil(total / metadata.postPageSize);
if (totalPage < Number(page)) {
if (totalPage < pageInt) {
return Astro.redirect("/404");
}
---
<Layout description="天若有情天亦老,人间正道是沧桑。" title="博客首页">
<IndexView
posts={list}
page={Number(page)}
totalPage={totalPage}
/>
<Layout title="博客首页" description={description}>
<IndexPage page={pageInt} totalPage={totalPage} posts={list}/>
</Layout>

View File

@@ -3,159 +3,186 @@ import "@/styles/github-markdown.css";
import "@/styles/github-markdown-reset.css";
import "photoswipe/dist/photoswipe.css";
import Layout from "@/layouts/Layout.astro";
import { createHttp } from "@/lib/request.ts";
import { highlightHtmlWithJsdom } from "@/lib/highlight.js";
import { formatDatetime } from "@/lib/format.ts";
import type { Post } from "@/types";
import Layout from '@/layouts/Layout.astro';
import {createHttp} from "@/lib/request.ts";
import type {Post} from "@/types";
import {processHtmlWithJsdom} from "@/lib/processHtml.ts";
import {siteConfig} from '@/consts'
import {formatDatetime} from "@/lib/format.ts";
const { slug } = Astro.params;
const {siteTitle} = siteConfig
let data: Post;
const {slug} = Astro.params;
const http = createHttp(Astro.request.headers);
let data: Post;
try {
const res = await http.get<ApiResponse<Post>>(`posts/${slug}`);
data = res.data;
} catch (error) {
return Astro.redirect("/404");
}
const content = await highlightHtmlWithJsdom(data.content);
const published_at = formatDatetime(data.published_at, "YYYY 年 M 月 D 日");
const content = await processHtmlWithJsdom(data.content);
const published_at = formatDatetime(data.published_at, "YYYY.MM.DD");
---
<Layout title={data.title} description={data.summary}>
<div class="page-content">
<div class="post">
<h1 class="post-title">{data.title}</h1>
<Layout title={data.title} description="">
<div class="page-container">
<nav class="nav">
<a class="nav-title" href="/">{siteTitle}</a>
<a href="/">← 返回首页</a>
</nav>
<div class="post-meta">
<div class="published-at">
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="lucide lucide-calendar-days-icon lucide-calendar-days"
>
<path d="M8 2v4"></path>
<path d="M16 2v4"></path>
<rect width="18" height="18" x="3" y="4" rx="2"></rect>
<path d="M3 10h18"></path>
<path d="M8 14h.01"></path>
<path d="M12 14h.01"></path>
<path d="M16 14h.01"></path>
<path d="M8 18h.01"></path>
<path d="M12 18h.01"></path>
<path d="M16 18h.01"></path>
</svg>
{published_at}
<article>
<div class="meta">
<div class="meta-left">
<span>{data.category_name} · {data.category_code.toUpperCase()}</span>
<span>{published_at}</span>
</div>
<div class="post-view">
<svg
viewBox="0 0 1024 1024"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
p-id="5122"
>
<path
d="M509.618 415.795c-42.695 0-77.253 38.372-77.253 85.738 0 47.352 34.558 86.22 77.253 86.22 42.667 0 76.716-38.867 76.716-86.22 0-47.352-34.049-85.738-76.716-85.738z m1.9-177.9c-211.548 0-383.265 159.859-383.265 274.587 0 114.727 171.717 273.624 383.266 273.624 212.484 0 384.227-162.522 384.227-273.624 0-111.088-171.743-274.587-384.227-274.587z m-1.446 452.006c-89.7 0-162.134-81.71-162.134-181.903 0-101.157 72.435-182.397 162.134-182.397 90.183 0 163.1 81.241 163.1 182.397 0 100.206-72.917 181.903-163.1 181.903z"
p-id="5123"></path>
</svg>
{data.view_count}
</div>
<div class="meta-view-count">{data.view_count} 次阅读</div>
</div>
<h1>{data.title}</h1>
<div class="markdown-body" data-color-mode="dark" set:html={content} />
</div>
<p class="summary">{data.summary}</p>
<div class="divider"></div>
<div class="markdown-body" data-color-mode="dark" set:html={content}/>
{
data.tags.length > 0 &&
<div class="tags">
{data.tags.map(tag => <span>{tag.name}</span>)}
</div>
}
</article>
<footer>
<div>© {siteTitle}</div>
</footer>
</div>
</Layout>
<style>
.post {
background: #fff;
border-radius: 12px;
overflow: auto;
padding: 24px;
box-shadow: var(--shadow);
}
.post-title {
margin-bottom: 16px;
font-size: 32px;
}
.post-meta {
.page-container {
height: 100%;
min-height: 100%;
display: flex;
flex-direction: column;
}
.nav {
flex-shrink: 0;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20px;
align-items: center;
font-size: 13px;
width: 100%;
}
.published-at {
.nav a {
color: #999;
text-decoration: none;
}
.nav-title {
font-size: 16px;
}
.nav a:hover {
color: #24292e;
}
article {
width: 100%;
margin: 0 auto;
padding: 48px 0 0;
flex: 1;
flex-shrink: 0;
}
.meta {
color: var(--text-meta);
font-size: 13px;
display: flex;
justify-content: space-between;
align-items: center;
}
.meta-left {
display: flex;
gap: 16px;
flex-wrap: wrap;
}
.meta-view-count:hover{
color: var(--text-primary);
}
h1 {
font-size: 32px;
line-height: 1.4;
font-weight: 400;
margin: 20px 0 20px;
}
.summary {
font-size: 14px;
margin-bottom: 24px;
color: var(--text-meta);
}
.divider {
border-bottom: var(--border-secondary);
}
.markdown-body {
margin: 24px 0 24px 0;
line-height: 1.8;
font-size: 15px;
color: var(--article-text-color);
}
.tags {
margin-top: 44px;
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.tags span {
font-size: 12px;
color: #57606a;
border: var(--border-primary);
border-radius: 20px;
padding: 8px 16px;
}
footer{
flex-shrink: 0;
width: 100%;
margin-top: 44px;
border-top: var(--border-secondary);
height: 66px;
display: flex;
align-items: center;
gap: 8px;
color: #6b7280;
font-size: 14px;
}
.published-at svg {
width: 18px;
height: 18px;
}
:global(.markdown-body img) {
object-fit: contain;
height: auto !important;
}
.post-view {
display: flex;
align-items: center;
gap: 4px;
color: #6b7280;
padding-right: 12px;
font-size: 14px;
}
.post-view svg {
width: 20px;
height: 20px;
fill: currentColor; /* 让填充跟随 color */
stroke: currentColor; /* 让描边也跟随 color */
}
@media (max-width: 768px) {
.post-title {
font-size: 20px;
}
.post {
padding: 12px;
}
}
:global(.dark) .post {
background: var(--card-bg);
box-shadow: none;
border: none;
color: var(--text-tertiary);
font-size: 13px;
}
</style>
<script>
import PhotoSwipeLightbox from "photoswipe/lightbox";
import ClipboardJS from "clipboard";
const init = async () => {
const post = document.querySelector(".post")!;
const images = Array.from(post.querySelectorAll("img"));
const article = document.querySelector("article")!;
const images = Array.from(article.querySelectorAll("img"));
const waitImageLoaded = (img: HTMLImageElement) => {
if (img.complete && img.naturalWidth > 0 && img.naturalHeight > 0) {
@@ -163,8 +190,8 @@ const published_at = formatDatetime(data.published_at, "YYYY 年 M 月 D 日");
}
return new Promise<void>((resolve) => {
img.addEventListener("load", () => resolve(), { once: true });
img.addEventListener("error", () => resolve(), { once: true });
img.addEventListener("load", () => resolve(), {once: true});
img.addEventListener("error", () => resolve(), {once: true});
});
};
@@ -190,7 +217,7 @@ const published_at = formatDatetime(data.published_at, "YYYY 年 M 月 D 日");
);
const lightbox = new PhotoSwipeLightbox({
gallery: ".post",
gallery: "article",
children: "a[data-pswp-width]",
pswpModule: () => import("photoswipe"),
@@ -209,4 +236,38 @@ const published_at = formatDatetime(data.published_at, "YYYY 年 M 月 D 日");
};
init();
const copyTimers = new WeakMap<HTMLElement, ReturnType<typeof setTimeout>>();
// 代码块复制功能
const clipboard = new ClipboardJS(".code-copy-btn", {
text: (el) => {
const sibling = el.closest(".code-header")?.nextElementSibling;
return sibling?.textContent.trim() || "";
}
});
clipboard.on("success", (e) => {
const btn = e.trigger as HTMLElement;
// 清理当前按钮之前的定时器
const oldTimer = copyTimers.get(btn);
if (oldTimer) {
clearTimeout(oldTimer);
}
const originalText = btn.textContent || "";
btn.textContent = "✓ 代码已复制";
const timer = setTimeout(() => {
btn.textContent = originalText;
copyTimers.delete(btn);
}, 2000);
copyTimers.set(btn, timer);
e.clearSelection();
});
</script>

View File

@@ -1,214 +0,0 @@
---
import Layout from '@/layouts/Layout.astro'
import type {CategoriesStats, Post, Tag} from "@/types";
import {createHttp} from "@/lib/request.ts";
import PostList from "@/components/Post/List.astro";
const http = createHttp(Astro.request.headers);
const searchParams = Astro.url.searchParams;
const t = searchParams.get("t") ?? "";
const c = searchParams.get("c") ?? "";
const [{data: categories}, {data: tags}] = await Promise.all([
http.get<ApiResponse<CategoriesStats[]>>("categories/stats"),
http.get<ApiResponse<Tag[]>>("posts/tags"),
] as const)
let f_count = 0
const findCategory = categories.find(item => {
if (item.code === c) {
f_count++
}
return item.code === c
})
const findTag = tags.find(item => {
if (item.code === t) {
f_count++
}
return item.code === t
})
// 搜索条件必须匹配 不允许非法值
const {data:posts} = await http.get<ApiResponse<Post[]>>("posts/search", {
searchParams: {
category_code: findCategory?.code ?? "",
tag_code: findTag?.code ?? "",
}
})
const count = posts.length
const pageTitle =
f_count === 0 ? "所有文章" : [findCategory?.name, findTag?.name]
.filter(Boolean)
.join(" · ")
const pageDescription =
f_count === 0
? "浏览全部文章,按分类和标签筛选你感兴趣的内容。"
: `浏览${pageTitle}相关文章,共 ${count} 篇。`
const buildSearchUrl = (type: 'category' | 'tag', code: string) => {
const params = new URLSearchParams()
if (findTag?.code) {
params.set('t', findTag.code)
}
if (findCategory?.code) {
params.set('c', findCategory.code)
}
if (type === 'category') {
if (c === code) {
params.delete('c')
} else {
params.set('c', code)
}
} else {
if (t === code) {
params.delete('t')
} else {
params.set('t', code)
}
}
const query = params.toString()
return query ? `/search?${query}` : '/search'
}
---
<Layout title={pageTitle} description={pageDescription}>
<div class="page-content search-page">
<header class="search-header">
<h1 class="search-title">
{pageTitle}
</h1>
<div class="search-count">共 <span>{count}</span> 篇</div>
</header>
<div class="filter-group">
<div class="nav-group">
<div class="nav-label">分类:</div>
<div class="nav-chips">
{categories.map(item =>
<a href={buildSearchUrl("category", item.code)}
class={item.code === findCategory?.code ? 'chip active' : 'chip'}>{item.name}</a>)}
</div>
</div>
<div class="nav-group">
<div class="nav-label">标签:</div>
<div class="nav-chips">
{tags.map(item =>
<a href={buildSearchUrl("tag", item.code)}
class={item.code === findTag?.code ? 'chip active' : 'chip'}>{item.name}</a>)}
</div>
</div>
</div>
{posts.length > 0 ?
<PostList posts={posts}/> :
<div class="no-result">没有找到相关文章</div>}
</div>
</Layout>
<style>
.search-page {
display: flex;
flex-direction: column;
gap: 14px;
margin: 0 auto;
max-width: 860px;
width: 100%;
}
.search-header {
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
background: var(--card-bg);
border-radius: 10px;
box-shadow: var(--shadow);
padding: 16px 20px;
}
.search-count {
color: var(--text-secondary);
font-size: 14px;
line-height: 20px;
}
.search-count span {
color: var(--tag-text);
}
.search-title {
font-size: 20px;
font-weight: 700;
margin: 0;
}
.filter-group {
background: var(--card-bg);
border-radius: 10px;
box-shadow: var(--shadow);
padding: 14px 18px;
display: flex;
flex-direction: column;
gap: 18px;
}
.nav-group {
display: flex;
gap: 12px;
min-width: 0;
}
.nav-label {
color: var(--text-secondary);
font-size: 14px;
line-height: 20px;
}
.nav-chips {
flex: 1;
display: flex;
gap: 12px;
flex-wrap: wrap;
align-items: center;
}
.chip {
color: var(--tag-text);
border-radius: 999px;
background: var(--tag-bg);
padding: 4px 8px;
font-size: 12px;
user-select: none;
transition: background 0.2s;
cursor: pointer;
}
.chip.active, .chip:hover {
background: #3b82f6;
color: white;
}
:global(.dark) .chip.active,
:global(.dark) .chip:hover {
background: #d1d5db;
color: black;
}
.no-result {
text-align: center;
font-size: 14px;
margin-top: 32px;
color: var(--text-secondary);
}
</style>

View File

@@ -1,12 +1,10 @@
import type {APIRoute} from 'astro';
import {metadata} from "@/consts";
import {siteConfig} from "@/consts";
const site = metadata.site
const site = siteConfig.site
const staticUrls = [
'',
'/archive',
'/search',
];
export const GET: APIRoute = async () => {

View File

@@ -1,10 +1,10 @@
import type {APIRoute} from 'astro';
import {metadata} from "@/consts";
import {siteConfig} from "@/consts";
import {createHttp} from "@/lib/request.ts";
import type {PostForSitemap} from "@/types";
import dayjs from "dayjs";
const site = metadata.site
const site =siteConfig.site
export const GET: APIRoute = async (context) => {
const http = createHttp(context.request.headers);

View File

@@ -1,7 +1,7 @@
import type {APIRoute} from 'astro';
import {metadata} from "@/consts";
import {siteConfig} from "@/consts";
const site = metadata.site
const site = siteConfig.site
export const GET: APIRoute = () => {
const xml = `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">