2024-07-03 02:35:01 +00:00
|
|
|
import { Hono } from 'hono';
|
|
|
|
import { jsxRenderer, useRequestContext } from 'hono/jsx-renderer';
|
|
|
|
import { Page } from '@blog/templates/Page';
|
|
|
|
import { Home } from '@blog/templates/Pages/Home';
|
2024-07-03 02:52:56 +00:00
|
|
|
import { FourOhFour } from '@blog/templates/Pages/FourOhFour';
|
2024-07-03 02:59:25 +00:00
|
|
|
import { Post } from '@blog/templates/Pages/Post';
|
2024-07-03 02:35:01 +00:00
|
|
|
import { readdir } from 'node:fs/promises';
|
2024-07-03 02:59:25 +00:00
|
|
|
import { readPostMarkdown } from '@blog/util/readPostMarkdown';
|
2024-07-03 02:35:01 +00:00
|
|
|
|
|
|
|
const app = new Hono();
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'*',
|
|
|
|
jsxRenderer(
|
|
|
|
({ children }) => {
|
|
|
|
return (<Page>{children}</Page>);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
docType: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// read all posts
|
|
|
|
// create listing of posts
|
|
|
|
|
|
|
|
app.get('/', async (c) => {
|
|
|
|
const files = await readdir('../posts', { recursive: true });
|
|
|
|
const posts = files.filter(f => f === '.' || f === '..');
|
|
|
|
return c.render(<Home posts={posts}/>);
|
|
|
|
});
|
|
|
|
|
2024-07-03 02:52:56 +00:00
|
|
|
app.get('/posts/:slug', async (c) => {
|
2024-07-03 02:35:01 +00:00
|
|
|
const postSlug: string = c.req.param("slug");
|
2024-07-03 02:52:56 +00:00
|
|
|
const fileName: string = postSlug + '.md';
|
|
|
|
const files = await readdir(__dirname + "/../posts");
|
|
|
|
|
|
|
|
const postFile = files.find(f => f === fileName);
|
|
|
|
if (!postFile) {
|
|
|
|
c.status(404);
|
|
|
|
return c.render(<FourOhFour />);
|
|
|
|
}
|
2024-07-03 02:35:01 +00:00
|
|
|
|
2024-07-03 02:59:25 +00:00
|
|
|
const post = await readPostMarkdown(__dirname + "/../posts/" + postFile);
|
|
|
|
return c.render(<Post post={post} />);
|
2024-07-03 02:35:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default app;
|