-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastro.config.mjs
More file actions
142 lines (133 loc) · 4.07 KB
/
astro.config.mjs
File metadata and controls
142 lines (133 loc) · 4.07 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
// @ts-check
import { defineConfig } from 'astro/config';
import { readdirSync, readFileSync } from 'fs';
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));
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 draftMatch = fm[1].match(/^draft:\s*(true|false)\s*$/m);
if (draftMatch && draftMatch[1] === 'true') continue;
if (dateMatch) {
const slug = file.replace(/\.(mdx?|md)$/, '');
postLastmod.set(`/blog/${slug}`, new Date(dateMatch[1]));
}
} 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);
}
}
/** @type {string[]} */
const SKIP_PATTERNS = [];
/**
* @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') return 0.8;
if (path.startsWith('/blog/')) return 0.7;
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 (path.startsWith('/blog/') || path.startsWith('/authors/')) return 'monthly';
return 'weekly';
}
export default defineConfig({
site: 'https://mlsystems.dev',
trailingSlash: 'ignore',
integrations: [
react(),
mdx({
remarkPlugins: [remarkMath],
rehypePlugins: [
rehypeSlug,
[
rehypeAutolinkHeadings,
{
behavior: 'append',
properties: { className: ['heading-anchor'], ariaLabel: 'Anchor' },
content: { type: 'text', value: '#' },
},
],
rehypeKatex,
],
}),
sitemap({
changefreq: 'weekly',
priority: 0.5,
filter: (page) => {
try {
const url = new URL(page);
const p = url.pathname.replace(/\/$/, '') || '/';
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: {
inlineStylesheets: 'always',
},
vite: {
ssr: {
external: ['@resvg/resvg-js', 'satori'],
},
optimizeDeps: {
exclude: ['@resvg/resvg-js', 'satori'],
},
build: {
rollupOptions: {
external: [/^\/_pagefind\//],
},
},
},
});