-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateRecentUpdates.js
104 lines (88 loc) · 3.72 KB
/
generateRecentUpdates.js
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
const fs = require('fs');
const path = require('path');
const matter = require('gray-matter');
async function generateRecentUpdates() {
const blogDirectory = path.join(__dirname, './blog');
const researchDirectory = path.join(__dirname, './research');
const blogFiles = fs.readdirSync(blogDirectory);
const researchFiles = fs.readdirSync(researchDirectory);
const recentPosts = [];
const blogPosts = generatePosts(blogFiles, blogDirectory, 'blog')
const researchPosts = generatePosts(researchFiles, researchDirectory, 'research')
const combinedPosts = [...blogPosts, ...researchPosts];
const sortedPosts = combinedPosts.sort((a, b) => {
const dateA = new Date(a.date);
const dateB = new Date(b.date);
return dateB.getTime() - dateA.getTime();
})
const recentCombinedPosts = sortedPosts.slice(0, 5);
recentCombinedPosts.map((post) => post.id = generateId())
recentPosts.push(...recentCombinedPosts);
fs.writeFileSync('recentUpdates.json', JSON.stringify(recentPosts, null, 2));
}
function generatePosts(files, directory, type) {
return files
// Filter out only directories (assuming each post is in its own directory)
.filter(file => fs.statSync(path.join(directory, file)).isDirectory())
// Sort files based on the date in the filename
.sort((a, b) => {
const dateA = getDateFromFileName(a) || '1970-01-01';
const dateB = getDateFromFileName(b) || '1970-01-01';
return new Date(dateB) - new Date(dateA);
})
.reduce((posts, subdirectory) => {
const filePath = path.join(directory, subdirectory);
const filesInSubdirectory = fs.readdirSync(filePath);
const mdFile = filesInSubdirectory.find(file => file.endsWith('.md'));
if (mdFile) {
const fullFilePath = path.join(filePath, mdFile);
const title = getTitleFromFrontMatter(fullFilePath);
const date = getDateFromFileName(mdFile);
const image = getImageFromFrontMatter(fullFilePath);
const link = getLinkFromFrontMatter(fullFilePath, type);
const description = getDescriptionFromFrontMatter(fullFilePath);
posts.push({ title, date, image, link, description });
}
return posts;
}, [])
// Get the first 5 most recent posts
.slice(0, 5);
}
function getTitleFromFrontMatter(file) {
const { data: frontMatter } = matter(fs.readFileSync(file, 'utf8'));
return frontMatter.title;
}
function getDateFromFileName(file) {
const match = file.match(/^(\d{4}-\d{2}-\d{2})/);
return match ? match[1] : null;
}
function getImageFromFrontMatter(file) {
const { data: frontMatter } = matter(fs.readFileSync(file, 'utf8'));
return frontMatter.image;
}
let postId = 0;
function generateId() {
return ++postId;
}
function getLinkFromFrontMatter(file, type) {
const { data: frontMatter } = matter(fs.readFileSync(file, 'utf8'));
const slug = frontMatter.slug;
return `https://panoptic.xyz/${type}/${slug}`
}
function getDescriptionFromFrontMatter(file) {
const { data: frontMatter } = matter(fs.readFileSync(file, 'utf8'));
return frontMatter.description;
}
function getShortDescription(file) {
const content = fs.readFileSync(file, 'utf8');
const withoutFrontMatter = content.replace(/---[\s\S]*?---/, '');
const withoutImages = withoutFrontMatter.replace(/!\[[^\]]*\]\([^)]*\)/g, '');
const withoutIframes = withoutImages.replace(/<iframe[^>]*>[^<]*<\/iframe>/g, '');
const withoutComments = withoutIframes.replace(/<!--[\s\S]*?-->/g, '');
const withoutNewLines = withoutComments.replace(/^\s+|\n/g, '');
const withSpacesAfterPeriods = withoutNewLines.replace(/\.([A-Z]|$)/g, '. $1');
const words = withSpacesAfterPeriods.split(' ');
const shortWords = words.slice(0, 43);
return shortWords.join(' ');
}
generateRecentUpdates();