refactor: improve typings
This commit is contained in:
parent
188dc0124c
commit
29e5af56eb
@ -1,13 +1,17 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import type { PostData } from './+page.js';
|
||||||
|
|
||||||
export let data;
|
export let data;
|
||||||
|
|
||||||
|
let posts: PostData[] = data.posts as PostData[];
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
{#each data.posts as post}
|
{#each posts as post}
|
||||||
<div>
|
<div>
|
||||||
<a href="/posts/{post.path}" data-sveltekit-preload-data="hover">
|
<a href="/posts/{post.path}" data-sveltekit-preload-data="hover">
|
||||||
<p>{post.title}</p>
|
<p>{post.metadata.title}</p>
|
||||||
<p>{post.excerpt}</p>
|
<p>{post.metadata.excerpt}</p>
|
||||||
</a>
|
</a>
|
||||||
<div>
|
<div>
|
||||||
<time datetime="2020-03-16">{post.published}</time>
|
<time datetime="2020-03-16">{post.published}</time>
|
||||||
|
@ -1,13 +1,20 @@
|
|||||||
import convertDate from "$lib/convertDate";
|
import convertDate from "$lib/convertDate";
|
||||||
|
|
||||||
|
export interface PostData {
|
||||||
|
path: string,
|
||||||
|
published: string,
|
||||||
|
metadata: Record<string, string>,
|
||||||
|
}
|
||||||
|
|
||||||
export async function load({ params }) {
|
export async function load({ params }) {
|
||||||
const allPostFiles: Record<string, any> = import.meta.glob('./*/+page.md', {eager: true});
|
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);
|
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) {
|
if (!allPosts.length) {
|
||||||
return { status: 404 };
|
return { status: 404 };
|
||||||
}
|
}
|
||||||
|
//console.log(allPosts);
|
||||||
return { posts: allPosts };
|
return { posts: allPosts };
|
||||||
}
|
}
|
@ -2,7 +2,6 @@
|
|||||||
title: Hello World
|
title: Hello World
|
||||||
author: Jeff
|
author: Jeff
|
||||||
date: 2022-05-27
|
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.
|
excerpt: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi eget massa sit amet arcu varius lacinia nec quis lacus.
|
||||||
---
|
---
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user