refactor: Post type

This commit is contained in:
Lewis Wynne 2026-03-27 18:29:00 +00:00
parent 5cc122bf39
commit c647fd62c3
3 changed files with 5 additions and 5 deletions

View file

@ -2,7 +2,7 @@ import type { CollectionEntry } from 'astro:content';
import { DEFAULT_CATEGORY } from './consts'; import { DEFAULT_CATEGORY } from './consts';
import { sortEntries } from './format'; import { sortEntries } from './format';
type Post = CollectionEntry<'md'>; export type Post = CollectionEntry<'md'> & { body?: string };
export function getSlug(postId: string): string { export function getSlug(postId: string): string {
const parts = postId.split('/'); const parts = postId.split('/');

View file

@ -2,7 +2,7 @@
import { getCollection, render } from 'astro:content'; import { getCollection, render } from 'astro:content';
import Layout from '../layouts/Layout.astro'; import Layout from '../layouts/Layout.astro';
import { formatDate, formatListItem, excerpt } from '../lib/format'; import { formatDate, formatListItem, excerpt } from '../lib/format';
import { getSlug, resolveRelatedPosts } from '../lib/md'; import { getSlug, resolveRelatedPosts, type Post } from '../lib/md';
export async function getStaticPaths() { export async function getStaticPaths() {
const allPosts = await getCollection('md'); const allPosts = await getCollection('md');
@ -15,7 +15,7 @@ export async function getStaticPaths() {
const { post, allPosts } = Astro.props; const { post, allPosts } = Astro.props;
const { Content } = await render(post); const { Content } = await render(post);
const related = post.data.related ? resolveRelatedPosts(post.data.related, allPosts) : []; const related = post.data.related ? resolveRelatedPosts(post.data.related, allPosts) : [];
const description = excerpt((post as any).body) || undefined; const description = excerpt((post as Post).body) || undefined;
--- ---
<Layout title={`${post.data.title} - lewis m.w.`} description={description}> <Layout title={`${post.data.title} - lewis m.w.`} description={description}>

View file

@ -1,7 +1,7 @@
import rss from '@astrojs/rss'; import rss from '@astrojs/rss';
import { getCollection } from 'astro:content'; import { getCollection } from 'astro:content';
import type { APIContext } from 'astro'; import type { APIContext } from 'astro';
import { getSlug } from '../lib/md'; import { getSlug, type Post } from '../lib/md';
import { getTxtFiles } from '../lib/txt'; import { getTxtFiles } from '../lib/txt';
import { excerpt } from '../lib/format'; import { excerpt } from '../lib/format';
@ -14,7 +14,7 @@ export async function GET(context: APIContext) {
title: post.data.title, title: post.data.title,
pubDate: post.data.date, pubDate: post.data.date,
link: `/${getSlug(post.id)}`, link: `/${getSlug(post.id)}`,
description: excerpt((post as any).body) || post.data.title, description: excerpt((post as Post).body) || post.data.title,
})), })),
...txtFiles.map(txt => ({ ...txtFiles.map(txt => ({
title: txt.name, title: txt.name,