-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastro.config.mjs
More file actions
141 lines (119 loc) · 3.43 KB
/
astro.config.mjs
File metadata and controls
141 lines (119 loc) · 3.43 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
import { existsSync } from "node:fs";
import { fileURLToPath } from "node:url";
import path from "node:path";
import { defineConfig } from "astro/config";
import vercel from "@astrojs/vercel";
import { remarkWikilinks, rehypeWikilinks } from "./src/remark-wikilinks.mjs";
const IMAGE_EXTENSIONS_REGEX =
/\.(avif|bmp|gif|ico|jpe?g|png|svg|tiff?|webp)(?:[?#].*)?$/i;
function normalizeMarkdownImageUrl(url) {
if (typeof url !== "string") {
return url;
}
const trimmed = url.trim();
if (!trimmed) {
return trimmed;
}
if (
trimmed.startsWith("../assets/") ||
trimmed.startsWith("./assets/") ||
trimmed.startsWith("assets/")
) {
return trimmed.replace(/^(?:\.\/)?assets\//, "../assets/");
}
if (trimmed.startsWith("asset://localhost/")) {
const decoded = decodeURIComponent(
trimmed.replace("asset://localhost/", "/"),
);
const filename = decoded.split("/").pop();
return filename ? `../assets/${filename}` : trimmed;
}
if (/^(?:[a-z][a-z0-9+.-]*:|\/\/|\/|#)/i.test(trimmed)) {
return trimmed;
}
if (trimmed.startsWith("./") || trimmed.startsWith("../")) {
return trimmed;
}
if (!trimmed.includes("/") && IMAGE_EXTENSIONS_REGEX.test(trimmed)) {
return `../assets/${trimmed}`;
}
return trimmed;
}
function rewriteMarkdownImageUrls(source) {
return source.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (full, alt, rawUrl) => {
const normalizedUrl = normalizeMarkdownImageUrl(rawUrl);
if (normalizedUrl === rawUrl) {
return full;
}
return ``;
});
}
function resolvePartOfMyBrainAssetUrl(source, importer) {
if (typeof source !== "string" || typeof importer !== "string") {
return null;
}
const sourcePath = source.split("?")[0]?.trim();
if (
!sourcePath ||
sourcePath.includes("/") ||
sourcePath.includes("\\") ||
!IMAGE_EXTENSIONS_REGEX.test(sourcePath)
) {
return null;
}
const importerPath = importer.startsWith("file://")
? fileURLToPath(importer)
: importer;
const normalizedImporterPath = importerPath.replaceAll("\\", "/");
const vaultMarker = "/part-of-my-brain/";
const markerIndex = normalizedImporterPath.lastIndexOf(vaultMarker);
if (markerIndex === -1) {
return null;
}
const vaultRoot = normalizedImporterPath.slice(
0,
markerIndex + vaultMarker.length,
);
const candidatePath = path.posix.join(vaultRoot, "assets", sourcePath);
return existsSync(candidatePath) ? candidatePath : null;
}
function contentImageFallbackPlugin() {
return {
name: "content-image-fallback-plugin",
enforce: "pre",
resolveId(source, importer) {
const resolvedAsset = resolvePartOfMyBrainAssetUrl(source, importer);
return resolvedAsset ?? null;
},
};
}
function markdownAssetsPathPlugin() {
return {
name: "markdown-assets-path-plugin",
enforce: "pre",
transform(code, id) {
if (!id.endsWith(".md") || !id.includes("/part-of-my-brain/")) {
return null;
}
const rewritten = rewriteMarkdownImageUrls(code);
if (rewritten === code) {
return null;
}
return {
code: rewritten,
map: null,
};
},
};
}
export default defineConfig({
adapter: vercel(),
site: "https://johnjeong.com",
vite: {
plugins: [contentImageFallbackPlugin(), markdownAssetsPathPlugin()],
},
markdown: {
remarkPlugins: [remarkWikilinks],
rehypePlugins: [rehypeWikilinks],
},
});