-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulateSearchData.js
More file actions
36 lines (28 loc) · 958 Bytes
/
Copy pathpopulateSearchData.js
File metadata and controls
36 lines (28 loc) · 958 Bytes
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
import path from "path";
import fs from "fs";
const filterOutTitle = (title) => {
const newRegExp = /(\d{4}-\d{2}-\d{2})(.+)/;
const [_, postDate, postTitle] = newRegExp.exec(title) || [];
return {
date: postDate,
title: postTitle?.replaceAll("-", " ")?.trim() || title
}
}
const getPostsDirectory = () => path.join(process.cwd(), 'src/pages/blog');
const filterOtherFiles = item => item !== ".DS_Store";
const formatPosts = fileName => ({
id: fileName,
...filterOutTitle(fileName)
})
function getPosts() {
const fileNames = fs.readdirSync(getPostsDirectory()).filter(filterOtherFiles);
return JSON.stringify(fileNames.map(formatPosts));
}
const fileContents = `export const posts = ${getPosts()};`;
try {
fs.readdirSync("src/cache");
} catch ({ }) {
fs.mkdirSync("src/cache");
}
fs.writeFileSync("src/cache/searchData.js", fileContents);
console.log("Posts cached in src/cache/searchData.js");