mirror of
https://github.com/dzeiocom/markblog.git
synced 2025-04-22 10:52:12 +00:00
67 lines
1.4 KiB
TypeScript
67 lines
1.4 KiB
TypeScript
import matter from 'gray-matter'
|
|
|
|
|
|
interface PostInterface {
|
|
slug: string
|
|
title: string
|
|
content: any
|
|
}
|
|
|
|
export interface PostHeader {
|
|
title: string
|
|
id: string
|
|
category: string
|
|
image?: string
|
|
imageAlt?: string
|
|
date: Date
|
|
url?: string
|
|
tags?: string[]
|
|
modifiedDate?: Date
|
|
short?: string
|
|
}
|
|
|
|
export default class Post implements PostInterface {
|
|
|
|
public slug: string
|
|
public title: string
|
|
public content: string
|
|
public isStarted = false
|
|
|
|
public header: PostHeader
|
|
|
|
constructor(slug: string) {
|
|
this.slug = slug
|
|
}
|
|
|
|
public async fetch() {
|
|
if (!this.slug.endsWith(".md")) this.slug = "portfolio/" + this.slug + ".md"
|
|
const content = await import(`../posts/${this.slug}`)
|
|
const md = matter(content.default)
|
|
this.title = md.data.title
|
|
this.header = (md.data as PostHeader)
|
|
this.content = md.content
|
|
}
|
|
|
|
public fetchSync() {
|
|
if (!this.slug.endsWith(".md")) this.slug = "portfolio/" + this.slug + ".md"
|
|
const content = require(`../posts/${this.slug}`)
|
|
const md = matter(content.default)
|
|
this.title = md.data.title
|
|
this.header = (md.data as PostHeader)
|
|
this.content = md.content
|
|
}
|
|
|
|
public static async fetchAll(): Promise<Post[]> {
|
|
const files: string[] = ((require as any).context('../posts', true, /\.md$/)).keys()
|
|
const posts: Post[] = []
|
|
for (const file of files) {
|
|
posts.push(
|
|
new Post(
|
|
file.replace("./", '')
|
|
)
|
|
)
|
|
}
|
|
return posts
|
|
}
|
|
}
|