-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathastro.config.ts
More file actions
198 lines (184 loc) · 6.23 KB
/
Copy pathastro.config.ts
File metadata and controls
198 lines (184 loc) · 6.23 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
import { defineConfig, envField } from "astro/config";
import starlight from "@astrojs/starlight";
import sitemap from "@astrojs/sitemap";
import tailwindcss from "@tailwindcss/vite";
import react from "@astrojs/react";
import mdx from "@astrojs/mdx";
import icon from "astro-icon";
import { readdir, readFile } from "node:fs/promises";
import { join, extname, basename } from "node:path";
import { siteConfig } from "./src/config/site.config";
async function collectFiles(dir: string, extensions: string[]): Promise<string[]> {
const results: string[] = [];
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const path = join(dir, entry.name);
if (entry.isDirectory()) {
results.push(...(await collectFiles(path, extensions)));
continue;
}
if (extensions.includes(extname(entry.name))) {
results.push(path);
}
}
return results;
}
function parseFrontmatter(source: string) {
const match = source.match(/^---\s*\r?\n([\s\S]*?)\r?\n---/);
if (!match) return {};
return match[1].split(/\r?\n/).reduce<Record<string, string>>((acc, line) => {
const pair = line.match(/^\s*([A-Za-z0-9_-]+):\s*(.*)\s*$/);
if (!pair) return acc;
acc[pair[1]] = pair[2].replace(/^["']|["']$/g, "");
return acc;
}, {});
}
function validateDuplicates(entries: Array<{ id: string; data?: { uid?: string; locale?: string } }>, supportedLocales: string[]) {
const seenIds = new Set<string>();
const seenUids = new Map<string, string>();
for (const entry of entries) {
if (seenIds.has(entry.id)) {
console.warn(`[content-validation] Duplicate slug detected: "${entry.id}"`);
} else {
seenIds.add(entry.id);
}
const uid = entry.data?.uid;
if (uid) {
const previous = seenUids.get(uid);
if (previous) {
console.warn(`[content-validation] Duplicate uid detected: "${uid}" (${previous} and ${entry.id})`);
} else {
seenUids.set(uid, entry.id);
}
}
const locale = entry.data?.locale;
if (locale && !supportedLocales.includes(locale)) {
console.warn(`[content-validation] Unsupported locale "${locale}" on entry "${entry.id}"`);
}
}
}
function contentValidationIntegration() {
return {
name: "content-validation",
hooks: {
"astro:build:start": async () => {
const contentBase = join(process.cwd(), "src", "content");
const collections = [
{ dir: join(contentBase, "blog"), extensions: [".md", ".mdx"] },
{ dir: join(contentBase, "services"), extensions: [".md", ".mdx"] },
{ dir: join(contentBase, "pages"), extensions: [".md"] },
{ dir: join(contentBase, "faqs"), extensions: [".json"] },
{ dir: join(contentBase, "stack"), extensions: [".md", ".mdx"] },
];
const entries = await Promise.all(
collections.map(async ({ dir, extensions }) =>
Promise.all(
(await collectFiles(dir, extensions)).map(async (file) => ({
id: basename(file).replace(/\.[^/.]+$/, ""),
data: parseFrontmatter(await readFile(file, "utf8")),
})),
),
),
);
const supportedLocales = ["en"];
for (const collection of entries) {
validateDuplicates(collection, supportedLocales);
}
},
},
};
}
export default defineConfig({
site: siteConfig.url,
i18n: {
defaultLocale: "en",
locales: ["en"],
prefixDefaultLocale: false,
},
prefetch: {
prefetchAll: true,
defaultStrategy: "hover",
},
integrations: [
starlight({
title: siteConfig.name,
customCss: ["./src/styles/starlight.css"],
components: {
SiteTitle: "./src/components/docs/SiteTitle.astro",
},
editLink: {
baseUrl: "https://github.com/milzamsz/astro-cloudflare-starter/edit/main",
},
social: [
{ icon: "github", label: "GitHub", href: "https://github.com/milzamsz/astro-cloudflare-starter" },
],
sidebar: [
{
label: "Getting Started",
items: [
{ label: "Overview", slug: "docs/getting-started/overview" },
{ label: "Quick Start", slug: "docs/getting-started/quick-start" },
{ label: "Project Structure", slug: "docs/getting-started/project-structure" },
],
},
{
label: "Guides",
items: [
{ label: "Content Management", slug: "docs/guides/content-management" },
{ label: "Internationalization", slug: "docs/guides/internationalization" },
{ label: "Customization", slug: "docs/guides/customization" },
{ label: "AI-assisted development", slug: "docs/guides/ai-assisted-development" },
],
},
{
label: "Deployment",
items: [
{ label: "Cloudflare Pages", slug: "docs/deployment/cloudflare-pages" },
{ label: "Environment Variables", slug: "docs/deployment/environment-variables" },
],
},
],
}),
mdx(),
contentValidationIntegration(),
sitemap({
i18n: {
defaultLocale: "en",
locales: {
en: "en-US",
},
},
}),
react(),
icon(),
],
env: {
schema: {
SITE_URL: envField.string({ context: "server", access: "public", default: "http://localhost:4321" }),
GOOGLE_SITE_VERIFICATION: envField.string({ context: "server", access: "public", optional: true }),
BING_SITE_VERIFICATION: envField.string({ context: "server", access: "public", optional: true }),
PUBLIC_GA_MEASUREMENT_ID: envField.string({ context: "client", access: "public", optional: true }),
PUBLIC_GTM_ID: envField.string({ context: "client", access: "public", optional: true }),
PUBLIC_CONSENT_ENABLED: envField.boolean({ context: "client", access: "public", optional: true, default: false }),
PUBLIC_PRIVACY_POLICY_URL: envField.string({ context: "client", access: "public", optional: true }),
},
},
vite: {
plugins: [tailwindcss()],
},
build: {
format: "directory",
},
markdown: {
shikiConfig: {
theme: "github-dark",
wrap: true,
},
},
image: {
layout: "constrained",
},
security: {
checkOrigin: true,
},
});