import matter from "gray-matter"; import moment from "moment"; import * as showdown from "showdown"; import * as path from "path"; import * as fs from "fs"; export class Post { id: string; title: string; content: string; data: any; posted: string; postedSeconds: number; constructor(sourceFile: string, private allPosts: Post[]) { const { data, content } = matter(fs.readFileSync(sourceFile, "utf8")); this.id = path.parse(sourceFile).name; this.title = data.title; this.content = new showdown.Converter().makeHtml(content); this.posted = moment(data.posted).format("D MMM YYYY"); this.postedSeconds = data.posted.getTime(); this.data = data; } get posts() { return this.allPosts.map(p => ({ self: p === this, other: p !== this, id: p.id, posted: p.posted, title: p.data.title, data: p.data })); } } export function readPosts(postsDir: string) { const posts: Post[] = []; posts.push(...fs.readdirSync(postsDir) .filter(f => f[0] !== '.') .map(f => path.join(postsDir, f)) .filter(f => !fs.statSync(f).isDirectory()) .map(f => new Post(f, posts))); posts.sort((a, b) => b.postedSeconds - a.postedSeconds); return posts; }