Yamlify page objects
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import fs from 'fs';
|
||||
import { getAllPosts } from '../lib/slug';
|
||||
import { getAllPosts } from './slug';
|
||||
import { join } from 'path';
|
||||
|
||||
const publicDir = join(process.cwd(), 'public');
|
||||
|
||||
47
util/slug.ts
Normal file
47
util/slug.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import fs from 'fs'
|
||||
import matter from 'gray-matter';
|
||||
import { join } from 'path';
|
||||
|
||||
const postsDirectory = join(process.cwd(), 'posts');
|
||||
|
||||
export function getPost(rawslug: string, filter: Array<any> = []) {
|
||||
const slug = rawslug.replace(/\.md$/, '');
|
||||
const path = join(postsDirectory, `${slug}.md`);
|
||||
const file = fs.readFileSync(path, 'utf-8');
|
||||
const { data, content } = matter(file);
|
||||
|
||||
if (data['last_updated'] === undefined)
|
||||
data['last_updated'] = data['created_at'];
|
||||
|
||||
if (filter.length === 0)
|
||||
return { ...data, content, slug, rawslug };
|
||||
|
||||
let post: { slug?: string, rawslug?: string, content?: string, title?: string } | any = {};
|
||||
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;
|
||||
}
|
||||
|
||||
export function getAllPosts(filter: Array<any> = []) {
|
||||
const files = fs.readdirSync(postsDirectory);
|
||||
|
||||
return files
|
||||
.filter(c => !c.match(/^\./))
|
||||
.map(file => {
|
||||
return getPost(file, filter)
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user