Start building the post file service
This commit is contained in:
parent
5eb143ab4a
commit
87fd7b129b
@ -1,6 +1,6 @@
|
|||||||
export type PostMeta = {
|
export type PostMeta = {
|
||||||
title: string,
|
title: string,
|
||||||
slug?: string,
|
slug: string,
|
||||||
description?: string,
|
description?: string,
|
||||||
html?: string,
|
html?: string,
|
||||||
date: Date,
|
date: Date,
|
||||||
|
@ -4,47 +4,63 @@ import type { Post } from '@blog/models/Post';
|
|||||||
import * as yamlFront from 'yaml-front-matter';
|
import * as yamlFront from 'yaml-front-matter';
|
||||||
import { marked } from 'marked';
|
import { marked } from 'marked';
|
||||||
|
|
||||||
export class PostFileService {
|
export async function readPostFile(fileName: string): Promise<Post> {
|
||||||
private postFiles: string[];
|
const file = Bun.file(POST_PATH + '/' + fileName);
|
||||||
private posts: Record<string, Post>;
|
const fileContent = await file.text();
|
||||||
|
const parsedData = yamlFront.loadFront(fileContent);
|
||||||
|
const postHtml = await marked.parse(parsedData.__content);
|
||||||
|
|
||||||
public constructor(postFiles: string[]) {
|
return {
|
||||||
this.postFiles = postFiles;
|
meta: {
|
||||||
this.posts = new Record<string, Post>();
|
title: parsedData.title,
|
||||||
|
description: parsedData.description,
|
||||||
|
date: new Date(parsedData.date),
|
||||||
|
draft: parsedData?.draft || false,
|
||||||
|
tags: parsedData.tags,
|
||||||
|
slug: POST_ROUTE_PREFIX + '/' + fileName,
|
||||||
|
},
|
||||||
|
content: parsedData.__content,
|
||||||
|
html: postHtml
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class PostFileService {
|
||||||
|
private posts: Map<string, Post>;
|
||||||
|
|
||||||
|
public constructor(posts: Map<string, Post>) {
|
||||||
|
this.posts = posts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async create() {
|
public static async create() {
|
||||||
const posts = new Record<string, Post>();
|
const posts = new Map<string, Post>();
|
||||||
const postFiles: string[] = await readdir(POST_PATH);
|
const postFiles: string[] = await readdir(POST_PATH);
|
||||||
|
|
||||||
for (const postFile in postFiles) {
|
for (const postFile in postFiles) {
|
||||||
const post = await this.readPostFile(postFile);
|
const post = await readPostFile(postFile);
|
||||||
posts.add(postFile, post);
|
const key = post.meta?.slug || postFile.slice(0, -3);
|
||||||
|
posts.set(key, post);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PostFileService(postFiles);
|
return new PostFileService(posts);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async openFileAsText(fileName: string): Promise<string> {
|
public getPostHtml(slug: string): string {
|
||||||
const file = Bun.file(POST_PATH + '/' + fileName);
|
const html = this.posts.get(slug)?.html;
|
||||||
return file.text();
|
|
||||||
|
if (!html?.length) {
|
||||||
|
throw new Error("Post does not exist.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return html;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async readPostFile(fileName: string): Promise<Post> {
|
public getPostRawContent(slug: string): string {
|
||||||
const fileContent = await this.openFileAsText(fileName);
|
const content = this.posts.get(slug)?.content;
|
||||||
const parsedData = yamlFront.loadFront(fileContent);
|
|
||||||
const postHtml = await marked.parse(parsedData.__content);
|
|
||||||
|
|
||||||
return {
|
if (!content?.length) {
|
||||||
meta: {
|
throw new Error("Post does not exist.");
|
||||||
title: parsedData.title,
|
}
|
||||||
description: parsedData.description,
|
|
||||||
date: new Date(parsedData.date),
|
return content;
|
||||||
draft: parsedData?.draft || false,
|
|
||||||
tags: parsedData.tags,
|
|
||||||
slug: POST_ROUTE_PREFIX + '/' + fileName,
|
|
||||||
},
|
|
||||||
content: parsedData.__content,
|
|
||||||
html: postHtml
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user