refactor: improve typings

This commit is contained in:
dusk 2024-08-08 03:42:37 +03:00
parent 188dc0124c
commit 29e5af56eb
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
3 changed files with 16 additions and 6 deletions

View File

@ -1,13 +1,17 @@
<script lang="ts">
import type { PostData } from './+page.js';
export let data;
let posts: PostData[] = data.posts as PostData[];
</script>
<div>
{#each data.posts as post}
{#each posts as post}
<div>
<a href="/posts/{post.path}" data-sveltekit-preload-data="hover">
<p>{post.title}</p>
<p>{post.excerpt}</p>
<p>{post.metadata.title}</p>
<p>{post.metadata.excerpt}</p>
</a>
<div>
<time datetime="2020-03-16">{post.published}</time>

View File

@ -1,13 +1,20 @@
import convertDate from "$lib/convertDate";
export interface PostData {
path: string,
published: string,
metadata: Record<string, string>,
}
export async function load({ params }) {
const allPostFiles: Record<string, any> = import.meta.glob('./*/+page.md', {eager: true});
const allPosts: any[] = Object.entries(allPostFiles).map(([path, post]) => {
const allPosts: PostData[] = Object.entries(allPostFiles).map(([path, post]) => {
const postPath = path.slice(2, -8);
return { ...post.metadata, path: postPath, published: convertDate(post.metadata.date) };
return { metadata: post.metadata, path: postPath, published: convertDate(post.metadata.date) };
});
if (!allPosts.length) {
return { status: 404 };
}
//console.log(allPosts);
return { posts: allPosts };
}

View File

@ -2,7 +2,6 @@
title: Hello World
author: Jeff
date: 2022-05-27
layout: blog
excerpt: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi eget massa sit amet arcu varius lacinia nec quis lacus.
---