New notes
This commit is contained in:
13
util/slug.d.ts
vendored
13
util/slug.d.ts
vendored
@@ -5,6 +5,12 @@ interface Post {
|
||||
title?: string;
|
||||
};
|
||||
|
||||
interface NoteMeta {
|
||||
title: string;
|
||||
slug: string;
|
||||
last_updated: string;
|
||||
};
|
||||
|
||||
interface PostMeta {
|
||||
title: string;
|
||||
slug: string;
|
||||
@@ -13,5 +19,10 @@ interface PostMeta {
|
||||
};
|
||||
|
||||
export function getAllPosts(filter: Array<any> = []): Post[];
|
||||
export function getAllNotes(filter: Array<any> = []): Note[];
|
||||
|
||||
export function getPost(rawslug: string, filter: Array<any> = []): Post;
|
||||
export function getPostsMeta(): PostMeta[];
|
||||
export function getNote(rawslug: string, filter: Array<any> = []): Note;
|
||||
|
||||
export function getPostsMeta(): PostMeta[];
|
||||
export function getNotesMeta(): NoteMeta[];
|
||||
77
util/slug.js
77
util/slug.js
@@ -2,12 +2,15 @@ const fs = require('fs');
|
||||
const matter = require('gray-matter');
|
||||
const { join } = require('path');
|
||||
|
||||
const notesDir = join(process.cwd(), 'notes');
|
||||
const postsDir = join(process.cwd(), 'posts');
|
||||
const cacheDir = join(process.cwd(), '.cache');
|
||||
const postsCacheFile = join(cacheDir, 'posts.meta.json');
|
||||
const notesCacheFile = join(cacheDir, 'notes.meta.json');
|
||||
|
||||
function getPost(rawslug, filter = []) {
|
||||
function get(dir, rawslug, filter = []) {
|
||||
const slug = rawslug.replace(/\.md$/, '');
|
||||
const path = join(postsDir, `${slug}.md`);
|
||||
const path = join(dir, `${slug}.md`);
|
||||
const file = fs.readFileSync(path, 'utf-8');
|
||||
const { data, content } = matter(file);
|
||||
|
||||
@@ -41,7 +44,7 @@ function getAllPosts(filter = []) {
|
||||
return files
|
||||
.filter(c => (!c.match(/^\.]/) && c.match(/\.md$/)))
|
||||
.map(file => {
|
||||
return getPost(file, filter)
|
||||
return get(postsDir, file, filter)
|
||||
})
|
||||
.sort((a, b) => {
|
||||
const dA = new Date(a['created_at']);
|
||||
@@ -50,7 +53,20 @@ function getAllPosts(filter = []) {
|
||||
});
|
||||
}
|
||||
|
||||
const postMetaCacheFile = join(cacheDir, 'posts.meta.json');
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
function cachePostsMeta() { // public access cache
|
||||
const posts = getAllPosts(['title', 'slug', 'created_at', 'last_updated']);
|
||||
@@ -59,24 +75,69 @@ function cachePostsMeta() { // public access cache
|
||||
fs.mkdirSync(cacheDir);
|
||||
}
|
||||
|
||||
fs.writeFile(postMetaCacheFile, JSON.stringify(posts), (e) => {
|
||||
fs.writeFile(postsCacheFile, JSON.stringify(posts), (e) => {
|
||||
if (e)
|
||||
console.error(e);
|
||||
});
|
||||
return posts;
|
||||
}
|
||||
|
||||
function getPostsMeta() {
|
||||
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) {
|
||||
try {
|
||||
const file = fs.readFileSync(postMetaCacheFile, 'utf-8');
|
||||
const file = fs.readFileSync(name, 'utf-8');
|
||||
return JSON.parse(file);
|
||||
} catch (e) {
|
||||
if (name)
|
||||
return cachePostsMeta();
|
||||
}
|
||||
}
|
||||
|
||||
function cache() {
|
||||
cachePostsMeta();
|
||||
cacheNotesMeta();
|
||||
}
|
||||
|
||||
module.exports = { getAllPosts, getPost, getPostsMeta, cache };
|
||||
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
|
||||
};
|
||||
Reference in New Issue
Block a user