-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathastro.config.mjs
More file actions
251 lines (239 loc) · 8.51 KB
/
astro.config.mjs
File metadata and controls
251 lines (239 loc) · 8.51 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
import fs from 'node:fs';
import path from 'node:path';
import react from '@astrojs/react';
import sitemap from '@astrojs/sitemap';
import yaml from '@rollup/plugin-yaml';
import tailwindcss from '@tailwindcss/vite';
import umami from '@yeskunall/astro-umami';
import { defineConfig } from 'astro/config';
import icon from 'astro-icon';
import mermaid from 'astro-mermaid';
import pagefind from 'astro-pagefind';
import robotsTxt from 'astro-robots-txt';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypeKatex from 'rehype-katex';
import rehypeSlug from 'rehype-slug';
import remarkDirective from 'remark-directive';
import remarkMath from 'remark-math';
import Sonda from 'sonda/astro';
import { loadEnv } from 'vite';
import svgr from 'vite-plugin-svgr';
import YAML from 'yaml';
import { rehypeEncryptedBlock } from './src/lib/markdown/rehype-encrypted-block.ts';
import { rehypeEncryptedPost } from './src/lib/markdown/rehype-encrypted-post.ts';
import { rehypeImagePlaceholder } from './src/lib/markdown/rehype-image-placeholder.ts';
import { rehypeShokaAttrs } from './src/lib/markdown/rehype-shoka-attrs.ts';
import { remarkEncryptedDirective } from './src/lib/markdown/remark-encrypted-directive.ts';
import { remarkLinkEmbed } from './src/lib/markdown/remark-link-embed.ts';
import { remarkIns, remarkMark } from './src/lib/markdown/remark-shoka-effects.ts';
import { remarkShokaPreprocess } from './src/lib/markdown/remark-shoka-preprocess.ts';
import { remarkShokaRuby } from './src/lib/markdown/remark-shoka-ruby.ts';
import { remarkShokaSpoiler } from './src/lib/markdown/remark-shoka-spoiler.ts';
import { shokaMetaTransformer } from './src/lib/markdown/shiki-meta-transformer.ts';
import { normalizeUrl } from './src/lib/utils.ts';
// Load YAML config directly with Node.js (before Vite plugins are available)
// This is only used in astro.config.mjs - other files use @rollup/plugin-yaml
function loadConfigForAstro() {
const configPath = path.join(process.cwd(), 'config', 'site.yaml');
const content = fs.readFileSync(configPath, 'utf8');
return YAML.parse(content);
}
const yamlConfig = loadConfigForAstro();
// Bundle analysis mode: ANALYZE=true pnpm build
// Use loadEnv to read .env file (astro.config.mjs runs before Vite loads .env)
const { ANALYZE } = loadEnv(process.env.NODE_ENV || 'production', process.cwd(), '');
const isAnalyze = ANALYZE === 'true';
// Get Umami analytics config from YAML
const umamiConfig = yamlConfig.analytics?.umami;
const umamiEnabled = umamiConfig?.enabled ?? false;
const umamiId = umamiConfig?.id;
// Normalize endpoint URL to remove trailing slashes
const umamiEndpoint = normalizeUrl(umamiConfig?.endpoint);
// Get robots.txt config from YAML
const robotsConfig = yamlConfig.seo?.robots;
// i18n configuration from YAML
const i18nYaml = yamlConfig.i18n;
const i18nDefaultLocale = i18nYaml?.defaultLocale ?? 'zh';
const i18nLocales = (i18nYaml?.locales ?? [{ code: 'zh' }]).map((l) => l.code);
const hasMultipleLocales = i18nLocales.length > 1;
/**
* Vite plugin for conditional Three.js bundling
* When christmas snowfall is disabled, replaces SnowfallCanvas with a noop component
* This saves ~879KB from the bundle
*/
function conditionalSnowfall() {
const VIRTUAL_ID = 'virtual:snowfall-canvas';
const RESOLVED_ID = `\0${VIRTUAL_ID}`;
const christmas = yamlConfig.christmas || { enabled: false, features: {} };
const isEnabled = christmas.enabled && christmas.features?.snowfall;
return {
name: 'conditional-snowfall',
resolveId(id) {
if (id === VIRTUAL_ID) return RESOLVED_ID;
// Redirect the alias import to virtual module when disabled
if (!isEnabled && id === '@components/christmas/SnowfallCanvas') {
return RESOLVED_ID;
}
},
load(id) {
if (id === RESOLVED_ID) {
// Return noop component when christmas is disabled
return 'export function SnowfallCanvas() { return null; }';
}
},
};
}
// Build conditional plugin lists based on content config
const contentConfig = yamlConfig.content || {};
// Remark plugins — order matters
// remarkShokaPreprocess MUST be first: it re-parses raw text to fix GFM/remark conflicts
// (+++, ~sub~, {% links %} YAML etc.) before any AST-level plugin runs.
const remarkPlugins = [];
{
const needsPreprocess =
contentConfig.enableShokaContainers !== false ||
contentConfig.enableShokaHexoTags !== false ||
contentConfig.enableShokaEffects !== false;
if (needsPreprocess) {
remarkPlugins.push([
remarkShokaPreprocess,
{
enableContainers: contentConfig.enableShokaContainers !== false,
enableHexoTags: contentConfig.enableShokaHexoTags !== false,
enableSuperSub: contentConfig.enableShokaEffects !== false,
enableMath: contentConfig.enableMath !== false,
enableEncryptedBlock: contentConfig.enableEncryptedBlock ?? false,
},
]);
}
}
// remarkMath must run BEFORE ruby/spoiler/effects so that $...$ content
// is already parsed into inlineMath/math nodes and won't be touched by text-scanning plugins.
if (contentConfig.enableMath !== false) remarkPlugins.push(remarkMath);
if (contentConfig.enableShokaSpoiler !== false) remarkPlugins.push(remarkShokaSpoiler);
if (contentConfig.enableShokaRuby !== false) remarkPlugins.push(remarkShokaRuby);
if (contentConfig.enableShokaEffects !== false) {
remarkPlugins.push(remarkIns, remarkMark);
}
// Encrypted block: remarkDirective is registered in BOTH places —
// here for the main Astro pipeline (when remarkShokaPreprocess skips re-parse),
// and inside remarkShokaPreprocess's re-parse pipeline (when it does re-parse).
if (contentConfig.enableEncryptedBlock) {
remarkPlugins.push(remarkDirective, remarkEncryptedDirective);
}
// Link embed is always on (existing feature)
remarkPlugins.push([
remarkLinkEmbed,
{
enableTweetEmbed: contentConfig.enableTweetEmbed ?? true,
enableOGPreview: contentConfig.enableOGPreview ?? true,
},
]);
// Rehype plugins — order matters
const rehypePlugins = [
rehypeSlug,
[
rehypeAutolinkHeadings,
{
behavior: 'append',
properties: {
className: ['anchor-link'],
ariaLabel: 'Link to this section',
},
},
],
];
if (contentConfig.enableShokaAttrs !== false) rehypePlugins.push(rehypeShokaAttrs);
rehypePlugins.push(rehypeImagePlaceholder);
if (contentConfig.enableMath !== false) rehypePlugins.push(rehypeKatex);
// Encrypted block/post MUST be last rehype plugins — encrypt fully-rendered children
if (contentConfig.enableEncryptedBlock) {
rehypePlugins.push(rehypeEncryptedBlock);
rehypePlugins.push(rehypeEncryptedPost);
}
// Shiki transformers
const shikiTransformers = [];
if (contentConfig.enableCodeMeta !== false) shikiTransformers.push(shokaMetaTransformer());
// https://astro.build/config
export default defineConfig({
site: yamlConfig.site.url,
compressHTML: true,
markdown: {
// Enable GitHub Flavored Markdown
gfm: true,
remarkPlugins,
rehypePlugins,
syntaxHighlight: {
type: 'shiki',
excludeLangs: ['mermaid'],
},
shikiConfig: {
themes: {
light: 'github-light',
dark: 'github-dark',
},
transformers: shikiTransformers,
},
},
integrations: [
react(),
sitemap(),
icon({
include: {
gg: ['*'],
'fa6-regular': ['*'],
'fa6-solid': ['*'],
ri: ['*'],
},
}),
// Umami analytics - configured via config/site.yaml
...(umamiEnabled && umamiId
? [
umami({
id: umamiId,
endpointUrl: umamiEndpoint,
hostUrl: umamiEndpoint,
}),
]
: []),
pagefind(),
mermaid({
autoTheme: true,
}),
robotsTxt(robotsConfig || {}),
...(isAnalyze ? [Sonda()] : []),
],
devToolbar: {
enabled: true,
},
vite: {
build: {
// Enable sourcemap for Sonda bundle analysis
sourcemap: isAnalyze,
},
plugins: [yaml(), conditionalSnowfall(), svgr(), tailwindcss()],
ssr: {
noExternal: ['react-tweet'],
},
optimizeDeps: {
include: ['@antv/infographic'],
},
},
// Only enable Astro i18n routing when multiple locales are configured.
// Single-locale sites skip this entirely — no /[lang]/ routes are generated.
...(hasMultipleLocales && {
i18n: {
defaultLocale: i18nDefaultLocale,
locales: i18nLocales,
routing: {
prefixDefaultLocale: false,
redirectToDefaultLocale: true,
},
},
}),
prefetch: {
prefetchAll: true,
defaultStrategy: 'viewport',
},
trailingSlash: 'ignore',
});