-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastro.config.mjs
More file actions
266 lines (253 loc) · 8.42 KB
/
Copy pathastro.config.mjs
File metadata and controls
266 lines (253 loc) · 8.42 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
// @ts-check
import { defineConfig } from 'astro/config';
import { unified } from '@astrojs/markdown-remark';
import { readdirSync, readFileSync, existsSync, cpSync } from 'fs';
import { createHash } from 'crypto';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import react from '@astrojs/react';
import mdx from '@astrojs/mdx';
import sitemap from '@astrojs/sitemap';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
import rehypeSlug from 'rehype-slug';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
const __dirname = dirname(fileURLToPath(import.meta.url));
// Cache-buster for social scrapers (Slack/LinkedIn cache og:image by URL):
// hashing the card template means the URL changes exactly when the design does.
const ogVersion = createHash('md5')
.update(readFileSync(join(__dirname, 'src/lib/og.tsx'), 'utf8'))
.digest('hex')
.slice(0, 8);
const postLastmod = new Map();
try {
const postsDir = join(__dirname, 'src/content/posts');
for (const file of readdirSync(postsDir)) {
if (!file.endsWith('.mdx') && !file.endsWith('.md')) continue;
try {
const content = readFileSync(join(postsDir, file), 'utf8');
const fm = content.match(/^---\n([\s\S]*?)\n---/);
if (!fm) continue;
const dateMatch = fm[1].match(/^date:\s*["']?(\d{4}-\d{2}-\d{2})["']?\s*$/m);
const updatedMatch = fm[1].match(/^updated:\s*["']?(\d{4}-\d{2}-\d{2})["']?\s*$/m);
const draftMatch = fm[1].match(/^draft:\s*(true|false)\s*$/m);
if (draftMatch && draftMatch[1] === 'true') continue;
const stamp = updatedMatch?.[1] ?? dateMatch?.[1];
if (stamp) {
const slug = file.replace(/\.(mdx?|md)$/, '');
postLastmod.set(`/blog/${slug}`, new Date(stamp));
}
} catch (err) {
console.warn(
`[sitemap] could not read frontmatter for ${file}:`,
err instanceof Error ? err.message : err,
);
}
}
} catch (err) {
if (err instanceof Error && 'code' in err && err.code !== 'ENOENT') {
console.warn('[sitemap] posts dir scan failed:', err.message);
}
}
// Draft posts build and answer at their real URL, but must stay out of the
// sitemap. Collections don't exist yet at config time, so read the frontmatter
// off disk — the same rule content.config.ts applies, one layer earlier.
/** @type {Set<string>} */
const draftPaths = new Set();
try {
const postsDir = join(__dirname, 'src/content/posts');
for (const slug of readdirSync(postsDir)) {
if (slug.startsWith('.')) continue;
try {
const content = readFileSync(join(postsDir, slug, 'index.mdx'), 'utf8');
const fm = content.match(/^---\n([\s\S]*?)\n---/);
if (fm && /^draft:[ \t]*true[ \t]*$/m.test(fm[1])) draftPaths.add(`/blog/${slug}`);
} catch {
// not a post directory, or no index.mdx — nothing to exclude
}
}
} catch (err) {
if (err instanceof Error && 'code' in err && err.code !== 'ENOENT') {
console.warn('[sitemap] draft scan failed:', err.message);
}
}
/** @type {string[]} */
const SKIP_PATTERNS = ['/write', '/search', '/drafts'];
// Paginated listing pages (/blog/2, /topics/x/2, /tags/x/2, /authors/x/articles/2)
// are secondary — keep them below their first page and below real articles.
/**
* @param {string} path
* @returns {boolean}
*/
function isPaginatedListing(path) {
return /\/\d+$/.test(path);
}
/**
* @param {string} path
* @returns {number}
*/
function priorityFor(path) {
if (path === '/' || path === '') return 1.0;
if (path === '/blog' || path === '/topics') return 0.9;
if (path === '/playground' || path === '/community' || path === '/contribute' || path === '/why')
return 0.8;
if (isPaginatedListing(path)) return 0.4;
if (path.startsWith('/blog/')) return 0.7;
if (path.startsWith('/topics/') || path.startsWith('/tags/')) return 0.6;
if (path.startsWith('/authors/')) return 0.6;
return 0.5;
}
/**
* @param {string} path
* @returns {'daily' | 'weekly' | 'monthly'}
*/
function changefreqFor(path) {
if (path === '/' || path === '/blog' || path === '/topics') return 'daily';
if (isPaginatedListing(path)) return 'weekly';
if (path.startsWith('/blog/') || path.startsWith('/authors/')) return 'monthly';
return 'weekly';
}
// Astro 7+: remark/rehype plugins live on a shared markdown processor that MDX
// inherits, rather than on mdx({ remarkPlugins, rehypePlugins }) (removed in v8).
/** @type {[typeof rehypeAutolinkHeadings, import('rehype-autolink-headings').Options]} */
const autolinkHeadings = [
rehypeAutolinkHeadings,
{
behavior: 'append',
properties: { className: ['heading-anchor'], ariaLabel: 'Copy link to section' },
content: {
type: 'element',
tagName: 'svg',
properties: {
width: 16,
height: 16,
viewBox: '0 0 24 24',
fill: 'none',
stroke: 'currentColor',
strokeWidth: 2,
strokeLinecap: 'round',
strokeLinejoin: 'round',
'aria-hidden': 'true',
},
children: [
{
type: 'element',
tagName: 'path',
properties: { d: 'M9 17H7A5 5 0 0 1 7 7h2' },
children: [],
},
{
type: 'element',
tagName: 'path',
properties: { d: 'M15 7h2a5 5 0 0 1 0 10h-2' },
children: [],
},
{
type: 'element',
tagName: 'line',
properties: { x1: 8, y1: 12, x2: 16, y2: 12 },
children: [],
},
],
},
},
];
export default defineConfig({
site: 'https://mlsystems.dev',
trailingSlash: 'never',
markdown: {
shikiConfig: { theme: 'github-dark' },
processor: unified({
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeSlug, autolinkHeadings, rehypeKatex],
}),
},
integrations: [
react(),
mdx(),
// Forum media is downloaded into public/forum-media during the page build
// (see lib/forum.ts) — after Astro has already copied public/ to dist/. This
// copies it into the final output so re-hosted images are actually served.
{
name: 'forum-media-copy',
hooks: {
'astro:build:done': ({ dir }) => {
const src = 'public/forum-media';
if (existsSync(src))
cpSync(src, fileURLToPath(new URL('forum-media', dir)), { recursive: true });
},
},
},
sitemap({
changefreq: 'weekly',
priority: 0.5,
filter: (page) => {
try {
const url = new URL(page);
const p = url.pathname.replace(/\/$/, '') || '/';
if (draftPaths.has(p)) return false;
return !SKIP_PATTERNS.some((skip) => p === skip || p.startsWith(skip));
} catch {
return true;
}
},
serialize(item) {
try {
const url = new URL(item.url);
const path = url.pathname.replace(/\/$/, '') || '/';
item.priority = priorityFor(path);
// @ts-expect-error sitemap types use EnumChangefreq; the literal strings have matching values at runtime
item.changefreq = changefreqFor(path);
const realDate = postLastmod.get(path);
item.lastmod = (realDate ?? new Date()).toISOString();
} catch (err) {
console.warn(
`[sitemap] serialize failed for ${item.url}:`,
err instanceof Error ? err.message : err,
);
}
return item;
},
}),
],
output: 'static',
build: {
// 'auto' inlines only small styles; the shared design-system sheet is emitted
// as one cacheable /_astro/*.css instead of being duplicated into every page.
inlineStylesheets: 'auto',
// Flat files (blog/x.html) instead of blog/x/index.html, so URLs stay clean
// with no trailing slash (pairs with trailingSlash: 'never'). Avoids
// Cloudflare's directory-style 308 redirect that appended the slash.
format: 'file',
},
vite: {
define: {
__OG_VERSION__: JSON.stringify(ogVersion),
},
resolve: {
dedupe: ['react', 'react-dom'],
},
ssr: {
external: ['@resvg/resvg-js', 'satori'],
},
optimizeDeps: {
exclude: ['@resvg/resvg-js', 'satori'],
include: [
'react',
'react-dom',
'react/jsx-runtime',
'react/jsx-dev-runtime',
'@blocknote/core',
'@blocknote/react',
'@blocknote/mantine',
'@blocknote/code-block',
'mermaid',
],
},
build: {
rollupOptions: {
external: [/^\/_pagefind\//],
},
},
},
});