2022-04-24 04:27:51 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const matter = require('gray-matter');
|
|
|
|
const { join } = require('path');
|
2022-02-14 20:32:58 +00:00
|
|
|
|
2022-04-28 01:55:18 +00:00
|
|
|
const cacheDir = join(process.cwd(), '.cache');
|
2022-10-05 03:41:59 +00:00
|
|
|
|
|
|
|
function getDir(name) {
|
|
|
|
return join(process.cwd(), name);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCacheFileName(name) {
|
|
|
|
return join(cacheDir, `${name}.cache.json`)
|
|
|
|
}
|
2022-02-14 20:32:58 +00:00
|
|
|
|
2022-04-28 16:37:12 +00:00
|
|
|
function get(dir, rawslug, filter = []) {
|
2022-02-14 20:32:58 +00:00
|
|
|
const slug = rawslug.replace(/\.md$/, '');
|
2022-04-28 16:37:12 +00:00
|
|
|
const path = join(dir, `${slug}.md`);
|
2022-02-14 20:32:58 +00:00
|
|
|
const file = fs.readFileSync(path, 'utf-8');
|
|
|
|
const { data, content } = matter(file);
|
|
|
|
|
|
|
|
if (data['last_updated'] === undefined)
|
2022-04-28 01:55:18 +00:00
|
|
|
data['last_updated'] = '';
|
2022-02-14 20:32:58 +00:00
|
|
|
|
|
|
|
if (filter.length === 0)
|
|
|
|
return { ...data, content, slug, rawslug };
|
|
|
|
|
2022-04-24 04:27:51 +00:00
|
|
|
let post = {};
|
2022-02-14 20:32:58 +00:00
|
|
|
for (const [_, entry] of filter.entries()) {
|
|
|
|
if (entry === 'slug')
|
|
|
|
post[entry] = slug;
|
|
|
|
|
|
|
|
if (entry === 'rawslug')
|
|
|
|
post[entry] = rawslug;
|
|
|
|
|
|
|
|
if (entry === 'content')
|
|
|
|
post[entry] = content;
|
|
|
|
|
|
|
|
if (typeof data[entry] !== 'undefined') {
|
|
|
|
post[entry] = data[entry]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
|
2022-04-24 04:27:51 +00:00
|
|
|
function getAllPosts(filter = []) {
|
2022-10-05 03:41:59 +00:00
|
|
|
const files = fs.readdirSync(getDir('posts'));
|
2022-02-14 20:32:58 +00:00
|
|
|
|
2022-04-23 23:03:43 +00:00
|
|
|
return files
|
2022-04-27 10:28:15 +00:00
|
|
|
.filter(c => (!c.match(/^\.]/) && c.match(/\.md$/)))
|
2022-04-24 04:27:51 +00:00
|
|
|
.map(file => {
|
2022-10-05 03:41:59 +00:00
|
|
|
return get(getDir('posts'), file, filter)
|
2022-04-28 01:55:18 +00:00
|
|
|
})
|
2022-10-05 03:41:59 +00:00
|
|
|
.filter(c => (c.title && c.slug && c.created_at && (new Date(c.created_at)).toString() !== 'Invalid Date'))
|
2022-04-28 01:55:18 +00:00
|
|
|
.sort((a, b) => {
|
|
|
|
const dA = new Date(a['created_at']);
|
|
|
|
const dB = new Date(b['created_at']);
|
|
|
|
return dB - dA;
|
2022-04-24 04:27:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-28 16:37:12 +00:00
|
|
|
function getAllNotes(filter = []) {
|
2022-10-05 03:41:59 +00:00
|
|
|
const files = fs.readdirSync(getDir('notes'));
|
2022-04-28 16:37:12 +00:00
|
|
|
|
|
|
|
return files
|
|
|
|
.filter(c => (!c.match(/^\.]/) && c.match(/\.md$/)))
|
|
|
|
.map(file => {
|
2022-10-05 03:41:59 +00:00
|
|
|
return get(getDir('notes'), file, filter)
|
2022-04-28 16:37:12 +00:00
|
|
|
})
|
2022-10-05 03:41:59 +00:00
|
|
|
.filter(c => (c.title && c.slug && c.last_updated && (new Date(c.last_updated)).toString() !== 'Invalid Date'))
|
2022-04-28 16:37:12 +00:00
|
|
|
.sort((a, b) => {
|
|
|
|
const dA = new Date(a['last_updated']);
|
|
|
|
const dB = new Date(b['last_updated']);
|
|
|
|
return dB - dA;
|
|
|
|
});
|
|
|
|
}
|
2022-04-27 08:03:21 +00:00
|
|
|
|
2022-10-05 03:41:59 +00:00
|
|
|
const cats = {
|
|
|
|
notes: {
|
|
|
|
name: 'notes',
|
|
|
|
getAll: getAllNotes,
|
|
|
|
filter: ['title', 'slug', 'last_updated'],
|
|
|
|
},
|
|
|
|
posts: {
|
|
|
|
name: 'posts',
|
|
|
|
getAll: getAllPosts,
|
|
|
|
filter: ['title', 'slug', 'created_at', 'last_updated'],
|
2022-04-28 01:55:18 +00:00
|
|
|
}
|
2022-10-05 03:41:59 +00:00
|
|
|
};
|
2022-04-28 01:55:18 +00:00
|
|
|
|
2022-10-05 03:41:59 +00:00
|
|
|
function cacheMeta({name, getAll, filter}) {
|
|
|
|
const items = getAll(filter);
|
2022-04-28 16:37:12 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(cacheDir)) {
|
|
|
|
fs.mkdirSync(cacheDir);
|
|
|
|
}
|
|
|
|
|
2022-10-05 03:41:59 +00:00
|
|
|
fs.writeFile(getCacheFileName(name), JSON.stringify(items), (e) => {
|
2022-04-28 16:37:12 +00:00
|
|
|
if (e)
|
|
|
|
console.error(e);
|
|
|
|
});
|
2022-10-05 03:41:59 +00:00
|
|
|
return items;
|
2022-04-28 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 03:41:59 +00:00
|
|
|
function getMeta(cat) {
|
2022-04-28 01:55:18 +00:00
|
|
|
try {
|
2022-10-05 03:41:59 +00:00
|
|
|
const file = fs.readFileSync(getCacheFileName(cat.name), 'utf-8');
|
2022-04-28 01:55:18 +00:00
|
|
|
return JSON.parse(file);
|
|
|
|
} catch (e) {
|
2022-10-05 03:41:59 +00:00
|
|
|
if (cat.name)
|
|
|
|
return cacheMeta(cat);
|
2022-04-27 08:03:21 +00:00
|
|
|
}
|
2022-04-28 01:55:18 +00:00
|
|
|
}
|
2022-04-27 08:03:21 +00:00
|
|
|
|
2022-10-05 03:41:59 +00:00
|
|
|
function getPostsMeta() {
|
|
|
|
return getMeta(cats.posts);
|
2022-04-28 16:37:12 +00:00
|
|
|
};
|
|
|
|
|
2022-10-05 03:41:59 +00:00
|
|
|
function getNotesMeta() {
|
|
|
|
return getMeta(cats.notes);
|
2022-04-28 16:37:12 +00:00
|
|
|
};
|
|
|
|
|
2022-10-05 03:41:59 +00:00
|
|
|
function cache() {
|
|
|
|
Object.entries(cats).map(([_, v]) => {
|
|
|
|
return cacheMeta(v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const getPost = (s, f) => {return get(getDir('posts'), s, f)};
|
|
|
|
const getNote = (s, f) => {return get(getDir('notes'), s, f)};
|
2022-04-28 16:37:12 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getAllPosts,
|
|
|
|
getAllNotes,
|
|
|
|
getPostsMeta,
|
|
|
|
getNotesMeta,
|
|
|
|
getPost,
|
|
|
|
getNote,
|
|
|
|
cache
|
|
|
|
};
|