-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseLayout.astro
More file actions
286 lines (260 loc) · 9.21 KB
/
Copy pathBaseLayout.astro
File metadata and controls
286 lines (260 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
---
import { ClientRouter } from 'astro:transitions'; // Using ClientRouter for ViewTransitions in modern Astro
import { Header } from '@/components/layout/Header';
import { FloatingMusicPlayer } from '@/components/music/FloatingMusicPlayer';
import ScrollToTop from '@/components/ui/ScrollToTop.astro';
import { getIconComponent } from '@/lib/icons';
import { getMusicTracks } from '@/utils/music-tracks';
import { getSiteSettings } from '@/utils/site-settings';
import '@/styles/global.css';
interface Props {
title: string;
description?: string;
image?: string;
type?: 'website' | 'article';
publishedTime?: Date;
modifiedTime?: Date;
author?: string;
robots?: string;
canonical?: boolean;
}
const settings = await getSiteSettings();
const {
title,
description = settings.description,
image,
type = 'website',
publishedTime,
modifiedTime,
author = settings.author.name,
robots,
canonical = true,
} = Astro.props;
// 构建完整的 OG 图片 URL
const ogImage = image
? new URL(image, Astro.site || Astro.url)
: new URL(settings.og.defaultImage, Astro.site || Astro.url);
const canonicalUrl = new URL(Astro.url.pathname, Astro.site || Astro.url);
const busuanzi = settings.analytics.busuanzi;
const busuanziClientConfig = JSON.stringify(busuanzi).replace(/</g, '\\x3c');
const socialIconByLabel = {
Bilibili: 'bilibili',
Douyin: 'douyin',
Instagram: 'instagram',
GitHub: 'github',
Juejin: 'juejin',
RSS: 'rss',
} as const;
const footerSocials = settings.socials
.map((item) => ({
...item,
icon: socialIconByLabel[item.label as keyof typeof socialIconByLabel],
}))
.filter((item): item is typeof item & { icon: string } => Boolean(item.icon));
const musicTracks = await getMusicTracks();
---
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
{/* 无闪烁主题:在首屏绘制前同步应用 .dark,避免深色用户看到浅色闪烁 */}
<script is:inline>
(() => {
try {
const stored = localStorage.getItem('theme');
const isDark = stored
? stored === 'dark'
: window.matchMedia('(prefers-color-scheme: dark)').matches;
document.documentElement.classList.toggle('dark', isDark);
} catch {
/* localStorage 不可用时忽略 */
}
})();
</script>
<meta name="description" content={description} />
{robots && <meta name="robots" content={robots} />}
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#ffffff" />
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#09090b" />
<link rel="icon" href="/favicon.ico" sizes="any" />
<link rel="apple-touch-icon" href="/icon-192x192.png" />
<link rel="manifest" href="/manifest.webmanifest" />
<link
rel="alternate"
type="application/rss+xml"
title={settings.rss.title}
href={settings.links.rss}
/>
{
busuanzi.enabled && (
<>
{/* 不蒜子预连接优化 */}
<link rel="dns-prefetch" href={busuanzi.origin} />
<link rel="preconnect" href={busuanzi.origin} crossorigin />
</>
)
}
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
{
settings.verification.google.map((token) => (
<meta name="google-site-verification" content={token} />
))
}
<!-- Canonical URL -->
{canonical && <link rel="canonical" href={canonicalUrl} />}
<!-- Open Graph / Facebook -->
<meta property="og:type" content={type} />
<meta property="og:url" content={canonicalUrl} />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={ogImage} />
<meta property="og:site_name" content={settings.name} />
<meta property="og:locale" content={settings.og.locale} />
{/* Article 扩展标签 - 仅用于文章页 */}
{
type === 'article' && publishedTime && (
<meta property="article:published_time" content={publishedTime.toISOString()} />
)
}
{
type === 'article' && modifiedTime && (
<meta property="article:modified_time" content={modifiedTime.toISOString()} />
)
}
{type === 'article' && author && <meta property="article:author" content={author} />}
<!-- Twitter -->
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:url" content={canonicalUrl} />
<meta name="twitter:title" content={title} />
<meta name="twitter:description" content={description} />
<meta name="twitter:image" content={ogImage} />
<ClientRouter />
{/* Theme Initialization Script to prevent FOUC */}
<script is:inline type="module" src="/theme.js"></script>
{
busuanzi.enabled && (
<script
is:inline
set:html={`
(() => {
const config = ${busuanziClientConfig};
const stateKey = '__busuanziPageviewLoader';
const state = (window[stateKey] ||= { installed: false, timer: 0, lastUrl: '', lastAt: 0 });
const hideCounter = (container) => {
if (!container) return;
container.style.display = 'none';
container.setAttribute('aria-hidden', 'true');
};
const resetCounter = (container, value) => {
value.textContent = '...';
container.style.display = 'inline-flex';
container.removeAttribute('aria-hidden');
};
const loadCounter = () => {
const container = document.getElementById(config.pagePvContainerId);
const value = document.getElementById(config.pagePvValueId);
if (!container || !value) return;
const now = Date.now();
const currentUrl = window.location.href;
if (state.lastUrl === currentUrl && now - state.lastAt < 2000) return;
state.lastUrl = currentUrl;
state.lastAt = now;
resetCounter(container, value);
window.clearTimeout(state.timer);
document.getElementById(config.scriptId)?.remove();
const script = document.createElement('script');
script.id = config.scriptId;
script.src = config.scriptSrc;
script.async = true;
script.defer = true;
script.referrerPolicy = 'no-referrer-when-downgrade';
script.onerror = () => hideCounter(container);
state.timer = window.setTimeout(() => {
const latestContainer = document.getElementById(config.pagePvContainerId);
const latestValue = document.getElementById(config.pagePvValueId);
const text = latestValue?.textContent?.trim();
if (!text || text === '...') hideCounter(latestContainer);
}, Number(config.timeoutMs) || 5000);
document.head.appendChild(script);
};
if (!state.installed) {
state.installed = true;
document.addEventListener('astro:page-load', loadCounter);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', loadCounter, { once: true });
} else {
loadCounter();
}
})();
`}
/>
)
}
</head>
<body class="min-h-screen bg-background font-sans antialiased">
<!-- 跳过导航链接 - 改进可访问性 -->
<a
href="#main-content"
class="sr-only focus:not-sr-only focus:fixed focus:top-4 focus:left-4 focus:z-50 focus:px-4 focus:py-2 focus:bg-primary focus:text-primary-foreground focus:rounded-md focus:outline-none"
>
跳过导航
</a>
<div class="relative flex min-h-screen flex-col">
<Header
client:load
transition:persist
name={settings.name}
navigation={settings.navigation}
githubUrl={settings.links.github}
/>
<main
id="main-content"
class:list={['page-enter flex-1', type !== 'article' ? 'pb-8 md:pb-20' : 'pb-8']}
>
<slot />
</main>
<!-- Footer 美化版 -->
<footer
class:list={[
'border-t py-1 bg-background z-50 left-0 right-0',
type !== 'article' ? 'md:fixed md:bottom-0' : 'relative',
]}
>
<div class="container mx-auto flex flex-col items-center justify-center gap-0.5">
<div class="flex items-center gap-4">
{
footerSocials.map((item) => {
const Icon = getIconComponent(item.icon);
const external = !item.href.startsWith('/');
return (
<a
href={item.href}
target={external ? '_blank' : undefined}
rel={external ? 'noopener noreferrer' : undefined}
class="text-muted-foreground hover:text-primary transition-colors"
>
<span class="sr-only">{item.label}</span>
<Icon aria-hidden="true" width={24} height={24} />
</a>
);
})
}
</div>
<p class="text-balance text-center text-sm leading-none text-muted-foreground mt-1">
© {new Date().getFullYear()}
{settings.name}. {settings.ui.footer.rights}
</p>
</div>
</footer>
<!-- 滚动到顶部按钮 -->
<ScrollToTop />
{
musicTracks.length > 0 && (
<FloatingMusicPlayer client:load transition:persist tracks={musicTracks} />
)
}
</div>
</body>
</html>