-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
50 lines (46 loc) · 1.49 KB
/
vite.config.ts
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
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { fileURLToPath } from "url";
import fs from "node:fs";
import path from "node:path";
import matter from "gray-matter";
import { format } from "date-fns";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export default defineConfig({
base: "/REPO_NAME/", // Add this line
plugins: [
react(),
{
name: "virtual-posts",
resolveId(id) {
if (id === "virtual:posts") {
return "\0virtual:posts";
}
},
load(id) {
if (id === "\0virtual:posts") {
const postsDirectory = path.join(__dirname, "src/content");
const posts = fs
.readdirSync(postsDirectory)
.filter((file) => file.endsWith(".md"))
.map((file) => {
const fullPath = path.join(postsDirectory, file);
const fileContents = fs.readFileSync(fullPath, "utf8");
const { data, content } = matter(fileContents);
return {
slug: file.replace(".md", ""),
title: data.title,
date: format(new Date(data.date), "MMMM dd, yyyy"),
description: data.description,
content: content,
};
})
.sort(
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime(),
);
return `export default ${JSON.stringify(posts)}`;
}
},
},
],
});