-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-plugin-content.js
More file actions
108 lines (96 loc) · 3.66 KB
/
Copy pathvite-plugin-content.js
File metadata and controls
108 lines (96 loc) · 3.66 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
import path from 'path';
import { fileURLToPath } from 'url';
import { getBlogEntries, generateManifest, generateBlogHTML } from './scripts/blog-generator.tsx';
import { getProjects, generateProjectManifest, generateProjectHTML } from './scripts/project-generator.tsx';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const BLOGS_DIR = path.resolve(__dirname, 'public/raw-blogs');
const PROJECTS_DIR = path.resolve(__dirname, 'public/projects');
export default function blogPlugin() {
return {
name: 'vite-plugin-blog',
configureServer(server) {
server.watcher.add(BLOGS_DIR);
server.watcher.add(PROJECTS_DIR);
const handleFileChange = (file) => {
if ((file.startsWith(BLOGS_DIR) && file.endsWith('.md')) ||
(file.startsWith(PROJECTS_DIR))) {
server.ws.send({
type: 'full-reload',
path: '*'
});
}
};
server.watcher.on('change', handleFileChange);
server.watcher.on('add', handleFileChange);
server.watcher.on('unlink', handleFileChange);
server.middlewares.use(async (req, res, next) => {
// Blog Manifest
if (req.url === '/blogs.manifest.json') {
try {
const entries = getBlogEntries(BLOGS_DIR);
const manifest = generateManifest(entries);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(manifest));
return;
} catch (e) {
console.error('Error generating manifest:', e);
next();
return;
}
}
// Projects Manifest
if (req.url === '/projects.manifest.json') {
try {
const projects = getProjects(PROJECTS_DIR);
const manifest = generateProjectManifest(projects);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(manifest));
return;
} catch (e) {
console.error('Error generating projects manifest:', e);
next();
return;
}
}
// Blog HTML
const blogMatch = req.url.match(/^\/blog\/(.+)\.html$/);
if (blogMatch) {
const slug = blogMatch[1];
try {
const entries = getBlogEntries(BLOGS_DIR);
const entry = entries.find(e => e.slug === slug);
if (entry) {
const fullHtml = generateBlogHTML(entry, ['/src/index.css']);
const transformedHtml = await server.transformIndexHtml(req.url, fullHtml);
res.setHeader('Content-Type', 'text/html');
res.end(transformedHtml);
return;
}
} catch (e) {
console.error('Error generating blog HTML:', e);
}
}
// Devlogs
const projectMatch = req.url.match(/^\/project\/(.+)\.html$/);
if (projectMatch) {
const slug = projectMatch[1];
try {
const projects = getProjects(PROJECTS_DIR);
const project = projects.find(p => p.slug === slug);
if (project) {
const fullHtml = generateProjectHTML(project, ['/src/index.css']);
const transformedHtml = await server.transformIndexHtml(req.url, fullHtml);
res.setHeader('Content-Type', 'text/html');
res.end(transformedHtml);
return;
}
} catch (e) {
console.error('Error generating project HTML:', e);
}
}
next();
});
}
};
}