-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.config.js
More file actions
118 lines (95 loc) · 2.88 KB
/
Copy pathpost.config.js
File metadata and controls
118 lines (95 loc) · 2.88 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
import fs from 'fs';
import path from 'path';
const postsDir = './public/posts';
const distDir = './dist';
const allPosts = [];
const years = fs.readdirSync(postsDir);
years.forEach(year => {
const yearPath = path.join(postsDir, year);
if (fs.lstatSync(yearPath).isDirectory()) {
const files = fs.readdirSync(yearPath);
files.forEach(file => {
if (file.endsWith('.md')) {
const filePath = path.join(yearPath, file);
const content = fs.readFileSync(filePath, 'utf8');
const tagsMatch = content.match(/^tags:\s*(.*)$/m);
let tags = [];
if (tagsMatch) {
tags = tagsMatch[1]
.split(',')
.map(t => t.trim())
.filter(Boolean);
}
const fileName = file.replace('.md', '');
const parts = fileName.split('-');
const y = parts[0];
const m = parts[1];
const d = parts[2];
const slug = parts.slice(3).join('-');
allPosts.push({
year : y,
month : m,
day : d,
slug,
tags,
originalName : fileName,
title : slug.replace(/-/g, ' '),
date : `${y}-${m}-${d}`
});
}
});
}
});
allPosts.sort((a, b) => {
const dateCompare = b.date.localeCompare(a.date);
if (dateCompare === 0) {
return b.originalName.localeCompare(a.originalName);
}
return dateCompare;
});
const postsData = JSON.stringify(allPosts, null, 2);
fs.writeFileSync('./public/posts.json', postsData);
if (fs.existsSync(path.join(distDir, 'index.html'))) {
fs.writeFileSync(path.join(distDir, 'posts.json'), postsData);
allPosts.forEach(post => {
const targetDir = path.join(distDir, post.year, post.month, post.day);
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, {
recursive: true
});
}
fs.copyFileSync(
path.join(distDir , 'index.html'),
path.join(targetDir, `${post.slug}.html`)
);
});
const allTags = [...new Set(allPosts.flatMap(p => p.tags))];
allTags.forEach(tag => {
const tagDir = path.join(distDir, 'tag', tag);
if (!fs.existsSync(tagDir)) {
fs.mkdirSync(tagDir, {
recursive: true
});
}
fs.copyFileSync(
path.join(distDir, 'index.html'),
path.join(tagDir, 'index.html')
);
});
const tagsPageDir = path.join(distDir, 'tags');
if (!fs.existsSync(tagsPageDir)) {
fs.mkdirSync(tagsPageDir, { recursive: true });
}
fs.copyFileSync(
path.join(distDir, 'index.html'),
path.join(tagsPageDir, 'index.html')
);
const PagesComponents = ['404.html', 'about.html', 'tags.html'];
PagesComponents.forEach(page => {
fs.copyFileSync(
path.join(distDir, 'index.html'),
path.join(distDir, page)
);
});
}
console.log(`✅ Build ${allPosts.length} Posts Successfully`);