Skip to content

Commit a80f0b0

Browse files
committed
refactor: simplify codebase for clarity and consistency
- Parallelize getCollection calls with Promise.all - Simplify path parsing and theme resolution logic - Remove unused isScrollLocked function and alias - Improve naming consistency (MAX_DESC_LENGTH, firstSegment)
1 parent 3ab28be commit a80f0b0

File tree

12 files changed

+66
-73
lines changed

12 files changed

+66
-73
lines changed

src/components/FormattedDate.astro

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ interface Props {
44
}
55
66
const { date } = Astro.props;
7+
const isValidDate = date && !Number.isNaN(date.valueOf());
78
---
89

910
{
10-
date && !isNaN(date.valueOf()) && (
11-
<time datetime={date.toISOString()}>
12-
{date.toLocaleDateString("en-us", {
13-
year: "numeric",
14-
month: "short",
15-
day: "numeric",
16-
})}
17-
</time>
18-
)
11+
isValidDate && (
12+
<time datetime={date.toISOString()}>
13+
{date.toLocaleDateString("en-us", {
14+
year: "numeric",
15+
month: "short",
16+
day: "numeric",
17+
})}
18+
</time>
19+
)
1920
}

src/components/Header.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const navLinks = [
1616
];
1717
1818
const pathname = Astro.url.pathname.replace(import.meta.env.BASE_URL, "");
19-
const subpath = pathname.match(/[^/]+/g);
20-
const isActive = (href: string) => href === pathname || href === `/${subpath?.[0] || ""}`;
19+
const firstSegment = pathname.match(/^[^/]+/)?.[0] || "";
20+
const isActive = (href: string) => href === pathname || href === `/${firstSegment}`;
2121
---
2222

2323
<header

src/components/HeaderLink.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ type Props = HTMLAttributes<"a">;
55
66
const { href, class: className, ...props } = Astro.props;
77
const pathname = Astro.url.pathname.replace(import.meta.env.BASE_URL, "");
8-
const subpath = pathname.match(/[^/]+/g);
9-
const isActive = href === pathname || href === `/${subpath?.[0] || ""}`;
8+
const firstSegment = pathname.match(/^[^/]+/)?.[0] || "";
9+
const isActive = href === pathname || href === `/${firstSegment}`;
1010
---
1111

1212
<a

src/components/ShareSidebar.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const { title, description, tags = [], url = Astro.url.href, slug } = Astro.prop
1717
const twitterHashtags = tags.slice(0, 3).join(",");
1818
1919
// Twitter: truncate description to fit 280 char limit (title + desc + url ~23 + hashtags)
20-
const maxDescLength = 150;
20+
const MAX_DESC_LENGTH = 150;
2121
const truncatedDesc =
22-
description && description.length > maxDescLength
23-
? `${description.slice(0, maxDescLength)}...`
22+
description && description.length > MAX_DESC_LENGTH
23+
? `${description.slice(0, MAX_DESC_LENGTH)}...`
2424
: description;
2525
2626
// Twitter text: title + truncated description

src/components/Spoiler.astro

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
---
2-
// Astro component script (runs on server)
2+
// Spoiler component - reveals hidden text on click
33
---
4+
45
<span
5-
class="spoiler inline-block cursor-pointer select-none rounded px-1 transition-colors"
6-
style="background: var(--color-text-primary); color: var(--color-text-primary);"
6+
class="spoiler inline-block cursor-pointer select-none rounded px-1 transition-colors"
7+
style="background: var(--color-text-primary); color: var(--color-text-primary);"
78
>
8-
<slot />
9+
<slot />
910
</span>
1011

1112
<script>
12-
// Client-side script (runs in browser)
13-
document.addEventListener('DOMContentLoaded', function() {
14-
const spoilers = document.querySelectorAll('.spoiler');
15-
16-
spoilers.forEach(spoiler => {
17-
spoiler.addEventListener('click', function(event) {
18-
const target = event.currentTarget as HTMLElement;
19-
target.classList.toggle('revealed');
20-
target.style.color = target.classList.contains('revealed')
21-
? 'var(--color-bg-surface)'
22-
: 'var(--color-text-primary)';
23-
});
13+
document.addEventListener("DOMContentLoaded", () => {
14+
document.querySelectorAll(".spoiler").forEach((spoiler) => {
15+
spoiler.addEventListener("click", (event) => {
16+
const target = event.currentTarget as HTMLElement;
17+
target.classList.toggle("revealed");
18+
target.style.color = target.classList.contains("revealed")
19+
? "var(--color-bg-surface)"
20+
: "var(--color-text-primary)";
21+
});
22+
});
2423
});
25-
});
2624
</script>

src/pages/[...slug].astro

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import BlogPost from "../layouts/BlogPost.astro";
55
export const prerender = true;
66
77
export async function getStaticPaths() {
8-
const blogPosts = await getCollection("blog");
9-
const monthlyPosts = await getCollection("monthly");
10-
const tilPosts = await getCollection("til");
8+
const [blogPosts, monthlyPosts, tilPosts] = await Promise.all([
9+
getCollection("blog"),
10+
getCollection("monthly"),
11+
getCollection("til"),
12+
]);
1113
const posts = [...blogPosts, ...monthlyPosts, ...tilPosts];
1214
return posts.map((post) => ({
1315
params: { slug: post.id },

src/pages/channel/[...cursor].astro

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ const CHANNEL_USERNAME = "tomoko_channel";
2525
const CACHE_SECONDS = 300;
2626
2727
// Parse cursor parameter
28-
const cursorType = cursor?.startsWith("before-")
29-
? "before"
30-
: cursor?.startsWith("after-")
31-
? "after"
32-
: null;
33-
const cursorValue = cursor ? cursor.replace(/^(before-|after-)/, "") : undefined;
28+
function parseCursor(cursor: string | undefined): {
29+
type: "before" | "after" | null;
30+
value: string | undefined;
31+
} {
32+
if (!cursor) return { type: null, value: undefined };
33+
if (cursor.startsWith("before-")) return { type: "before", value: cursor.slice(7) };
34+
if (cursor.startsWith("after-")) return { type: "after", value: cursor.slice(6) };
35+
return { type: null, value: undefined };
36+
}
37+
38+
const { type: cursorType, value: cursorValue } = parseCursor(cursor);
3439
3540
let channelData: TelegramChannel | undefined;
3641
let error: string | null = null;

src/pages/tags/[tag].astro

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import { SITE_TITLE } from "../../consts";
99
export const prerender = true;
1010
1111
export async function getStaticPaths() {
12-
const blogPosts = await getCollection("blog");
13-
const monthlyPosts = await getCollection("monthly");
14-
const tilPosts = await getCollection("til");
12+
const [blogPosts, monthlyPosts, tilPosts] = await Promise.all([
13+
getCollection("blog"),
14+
getCollection("monthly"),
15+
getCollection("til"),
16+
]);
1517
const allPosts = [...blogPosts, ...monthlyPosts, ...tilPosts].filter(
1618
(post) => !post.data.hidden,
1719
);

src/pages/tags/index.astro

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import Footer from "../../components/Footer.astro";
55
import Header from "../../components/Header.astro";
66
import { SITE_DESCRIPTION, SITE_TITLE } from "../../consts";
77
8-
const blogPosts = await getCollection("blog");
9-
const monthlyPosts = await getCollection("monthly");
10-
const tilPosts = await getCollection("til");
8+
const [blogPosts, monthlyPosts, tilPosts] = await Promise.all([
9+
getCollection("blog"),
10+
getCollection("monthly"),
11+
getCollection("til"),
12+
]);
1113
const posts = [...blogPosts, ...monthlyPosts, ...tilPosts].filter((post) => !post.data.hidden);
1214
const tagCountMap = new Map<string, number>();
1315

src/scripts/blog-interactive.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,8 @@ function adjustSidebarPositions(): void {
343343
});
344344
}
345345

346-
// 保持向后兼容的别名
347-
function adjustTocPosition(): void {
348-
adjustSidebarPositions();
349-
}
350-
351-
const debouncedAdjustTocPosition = debounce(adjustTocPosition, 150);
352-
const throttledAdjustTocPosition = throttle(adjustTocPosition, 16);
346+
const debouncedAdjustSidebarPositions = debounce(adjustSidebarPositions, 150);
347+
const throttledAdjustSidebarPositions = throttle(adjustSidebarPositions, 16);
353348

354349
// ============ 初始化所有功能 ============
355350
export function initBlogInteractive() {
@@ -369,8 +364,8 @@ export function initBlogInteractive() {
369364
initMobileTocDrawer();
370365
window.addEventListener("astro:after-swap", initMobileTocDrawer);
371366

372-
adjustTocPosition();
373-
window.addEventListener("resize", debouncedAdjustTocPosition);
374-
window.addEventListener("scroll", throttledAdjustTocPosition, { passive: true });
375-
window.addEventListener("astro:after-swap", adjustTocPosition);
367+
adjustSidebarPositions();
368+
window.addEventListener("resize", debouncedAdjustSidebarPositions);
369+
window.addEventListener("scroll", throttledAdjustSidebarPositions, { passive: true });
370+
window.addEventListener("astro:after-swap", adjustSidebarPositions);
376371
}

0 commit comments

Comments
 (0)