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 16:37:12 +00:00
|
|
|
const notesDir = join(process.cwd(), 'notes');
|
2022-04-27 08:03:21 +00:00
|
|
|
const postsDir = join(process.cwd(), 'posts');
|
2022-04-28 01:55:18 +00:00
|
|
|
const cacheDir = join(process.cwd(), '.cache');
|
2022-04-28 16:37:12 +00:00
|
|
|
const postsCacheFile = join(cacheDir, 'posts.meta.json');
|
|
|
|
const notesCacheFile = join(cacheDir, 'notes.meta.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-04-27 08:03:21 +00:00
|
|
|
const files = fs.readdirSync(postsDir);
|
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-04-28 16:37:12 +00:00
|
|
|
return get(postsDir, file, filter)
|
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 = []) {
|
|
|
|
const files = fs.readdirSync(notesDir);
|
|
|
|
|
|
|
|
return files
|
|
|
|
.filter(c => (!c.match(/^\.]/) && c.match(/\.md$/)))
|
|
|
|
.map(file => {
|
|
|
|
return get(notesDir, file, filter)
|
|
|
|
})
|
|
|
|
.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
|
|
|
|
|
|
|
function cachePostsMeta() { // public access cache
|
|
|
|
const posts = getAllPosts(['title', 'slug', 'created_at', 'last_updated']);
|
2022-04-28 01:55:18 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(cacheDir)) {
|
|
|
|
fs.mkdirSync(cacheDir);
|
|
|
|
}
|
|
|
|
|
2022-04-28 16:37:12 +00:00
|
|
|
fs.writeFile(postsCacheFile, JSON.stringify(posts), (e) => {
|
2022-04-27 08:03:21 +00:00
|
|
|
if (e)
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
return posts;
|
|
|
|
}
|
|
|
|
|
2022-04-28 16:37:12 +00:00
|
|
|
function cacheNotesMeta() {
|
|
|
|
const notes = getAllNotes(['title', 'slug', 'last_updated']);
|
|
|
|
|
|
|
|
if (!fs.existsSync(cacheDir)) {
|
|
|
|
fs.mkdirSync(cacheDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFile(notesCacheFile, JSON.stringify(notes), (e) => {
|
|
|
|
if (e)
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
return notes;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMetaFromFile(name) {
|
2022-04-28 01:55:18 +00:00
|
|
|
try {
|
2022-04-28 16:37:12 +00:00
|
|
|
const file = fs.readFileSync(name, 'utf-8');
|
2022-04-28 01:55:18 +00:00
|
|
|
return JSON.parse(file);
|
|
|
|
} catch (e) {
|
2022-04-28 16:37:12 +00:00
|
|
|
if (name)
|
2022-04-27 08:03:21 +00:00
|
|
|
return cachePostsMeta();
|
|
|
|
}
|
2022-04-28 01:55:18 +00:00
|
|
|
}
|
2022-04-27 08:03:21 +00:00
|
|
|
|
2022-04-28 01:55:18 +00:00
|
|
|
function cache() {
|
|
|
|
cachePostsMeta();
|
2022-04-28 16:37:12 +00:00
|
|
|
cacheNotesMeta();
|
2022-04-27 08:03:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 16:37:12 +00:00
|
|
|
const getPostsMeta = () => {
|
|
|
|
try {
|
|
|
|
const file = fs.readFileSync(postsCacheFile, 'utf-8');
|
|
|
|
return JSON.parse(file);
|
|
|
|
} catch (e) {
|
|
|
|
return cachePostsMeta();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getNotesMeta = () => {
|
|
|
|
try {
|
|
|
|
const file = fs.readFileSync(notesCacheFile, 'utf-8');
|
|
|
|
return JSON.parse(file);
|
|
|
|
} catch (e) {
|
|
|
|
return cacheNotesMeta();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getPost = (s, f) => {return get(postsDir, s, f)};
|
|
|
|
const getNote = (s, f) => {return get(notesDir, s, f)};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getAllPosts,
|
|
|
|
getAllNotes,
|
|
|
|
getPostsMeta,
|
|
|
|
getNotesMeta,
|
|
|
|
getPost,
|
|
|
|
getNote,
|
|
|
|
cache
|
|
|
|
};
|