More changes to structure and UI

This commit is contained in:
2022-04-27 21:55:18 -04:00
parent 3d4b1b4f54
commit 9d761d0d3d
22 changed files with 257 additions and 218 deletions
+23 -8
View File
@@ -3,6 +3,7 @@ const matter = require('gray-matter');
const { join } = require('path');
const postsDir = join(process.cwd(), 'posts');
const cacheDir = join(process.cwd(), '.cache');
function getPost(rawslug, filter = []) {
const slug = rawslug.replace(/\.md$/, '');
@@ -11,7 +12,7 @@ function getPost(rawslug, filter = []) {
const { data, content } = matter(file);
if (data['last_updated'] === undefined)
data['last_updated'] = data['created_at'];
data['last_updated'] = '';
if (filter.length === 0)
return { ...data, content, slug, rawslug };
@@ -41,13 +42,24 @@ function getAllPosts(filter = []) {
.filter(c => (!c.match(/^\.]/) && c.match(/\.md$/)))
.map(file => {
return getPost(file, filter)
})
.sort((a, b) => {
const dA = new Date(a['created_at']);
const dB = new Date(b['created_at']);
return dB - dA;
});
}
const postMetaCacheFile = join(cacheDir, 'posts.meta.json');
function cachePostsMeta() { // public access cache
const posts = getAllPosts(['title', 'slug', 'created_at', 'last_updated']);
fs.writeFile(join(postsDir, 'meta.json'), JSON.stringify(posts), (e) => {
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir);
}
fs.writeFile(postMetaCacheFile, JSON.stringify(posts), (e) => {
if (e)
console.error(e);
});
@@ -55,13 +67,16 @@ function cachePostsMeta() { // public access cache
}
function getPostsMeta() {
const file = fs.readFileSync(join(postsDir, 'meta.json'), 'utf-8');
if (!file) {
try {
const file = fs.readFileSync(postMetaCacheFile, 'utf-8');
return JSON.parse(file);
} catch (e) {
return cachePostsMeta();
}
return JSON.parse(file);
}
module.exports = { getAllPosts, getPost, getPostsMeta, cachePostsMeta };
function cache() {
cachePostsMeta();
}
module.exports = { getAllPosts, getPost, getPostsMeta, cache };