Repository navigation
Expand file tree
/
Copy pathconfig.ts
More file actions
122 lines (108 loc) · 3.4 KB
/
Copy pathconfig.ts
File metadata and controls
122 lines (108 loc) · 3.4 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
import { defineCollection, z } from "astro:content";
import { glob, type ParseDataOptions } from "astro/loaders";
// Helper function to generate title from filename
const generateTitleFromFilename = (path?: string) =>
path?.split("/").pop()?.replace(".md", "");
// Helper function to extract image path from markdown link or wiki-link syntax
const extractImagePath = (
markdownLink: string | undefined,
): string | undefined => {
if (!markdownLink) return undefined;
// Try wiki-link format: [[filename]]
const wikiMatch = markdownLink.match(/\[\[(.+?)]]/);
if (wikiMatch?.[1]) {
return `./attachments/${decodeURIComponent(wikiMatch[1])}`;
}
// Try Markdown link format: [text](path)
const mdMatch = markdownLink.match(/\[.*?]\((.*?)\)/);
if (mdMatch?.[1]) {
// Decode URL-encoded characters (e.g., %20 -> space)
return decodeURIComponent(mdMatch[1]);
}
return undefined;
};
// Custom glob loader that adds title from filename if missing and extracts image paths
function globWithTitleAndImageFallback(options: Parameters<typeof glob>[0]) {
const loader = glob(options);
const originalLoad = loader.load;
loader.load = async ({ parseData, ...rest }) =>
originalLoad({
parseData: async <TData extends Record<string, unknown>>(
entry: ParseDataOptions<TData>,
) => {
// Add title from file name if not present
const data = entry.data as Record<string, unknown>;
if (!data.title) {
data.title = generateTitleFromFilename(entry.filePath);
}
// Extract image path from Markdown link format if needed
const frontmatterImage = data.image as string | undefined;
if (frontmatterImage) {
const imagePath =
extractImagePath(frontmatterImage) || frontmatterImage;
// Convert the path to be relative to the Markdown file
// Astro's image() helper will handle the actual resolution
data.image =
imagePath || (data.assetImports as string[] | undefined)?.[0];
}
return parseData(entry);
},
...rest,
});
return loader;
}
const games = defineCollection({
loader: globWithTitleAndImageFallback({
pattern: "**/*.md",
base: "./src/content/Games",
}),
schema: ({ image }) =>
z.object({
title: z.string(),
created: z.string(),
changed: z.string(),
publish: z.boolean(),
published: z.date(),
summary: z.string().optional(),
related: z.string().optional(),
image: image().optional(),
}),
});
const projects = defineCollection({
loader: globWithTitleAndImageFallback({
pattern: "**/*.md",
base: "./src/content/Projects",
}),
schema: ({ image }) =>
z.object({
title: z.string(),
created: z.string(),
changed: z.string(),
publish: z.boolean(),
published: z.date(),
summary: z.string().optional(),
url: z.string().url().optional(),
image: image().optional(),
}),
});
const content = defineCollection({
loader: globWithTitleAndImageFallback({
pattern: "**/*.md",
base: "./src/content",
}),
schema: ({ image }) =>
z.object({
title: z.string(),
created: z.string(),
changed: z.string(),
publish: z.boolean(),
published: z.date(),
image: image().optional(),
summary: z.string().optional(),
}),
});
export const collections = {
games,
projects,
content,
};