feat: extracts some repeated logic out into lib/ files
This commit is contained in:
parent
38653f2aa1
commit
99c1539aad
22 changed files with 200 additions and 336 deletions
|
|
@ -5,6 +5,8 @@ import { getSession } from 'auth-astro/server';
|
|||
import { getCollection, render } from 'astro:content';
|
||||
import { isAdmin } from '../../lib/auth';
|
||||
import Layout from '../../layouts/Layout.astro';
|
||||
import { formatDate } from '../../lib/format';
|
||||
import { getSlug } from '../../utils';
|
||||
|
||||
let session;
|
||||
try {
|
||||
|
|
@ -22,21 +24,14 @@ if (!isAdmin(session.user?.id)) {
|
|||
}
|
||||
|
||||
const slug = Astro.params.slug;
|
||||
const posts = await getCollection('posts', ({ data }) => data.draft === true);
|
||||
const post = posts.find(p => p.id === slug);
|
||||
const posts = await getCollection('md', ({ data }) => data.draft === true);
|
||||
const post = posts.find(p => getSlug(p.id) === slug);
|
||||
|
||||
if (!post) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
const { Content } = await render(post);
|
||||
|
||||
function formatDate(date: Date): string {
|
||||
const d = String(date.getDate()).padStart(2, '0');
|
||||
const m = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const y = String(date.getFullYear()).slice(-2);
|
||||
return `${d}/${m}/${y}`;
|
||||
}
|
||||
---
|
||||
<Layout title={`${post.data.title} - lewis m.w.`}>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue