strict typescript

This commit is contained in:
2024-02-18 20:30:10 -05:00
parent 06fce5508a
commit b4fd4e3403
6 changed files with 37 additions and 26 deletions
-2
View File
@@ -15,8 +15,6 @@ const { title, date, slug } = Astro.props;
{slug ? <a href={`/posts/${slug}`}>{title}</a> : title} {slug ? <a href={`/posts/${slug}`}>{title}</a> : title}
</h2> </h2>
<h3> <h3>
<!-- <img class="svg-icon" src={calendarIcon} alt="" /> -->
🗓️ <FormattedDate date={date} /> 🗓️ <FormattedDate date={date} />
</h3> </h3>
</div> </div>
+3 -1
View File
@@ -1,4 +1,6 @@
--- ---
import { Tag } from 'src/content/config';
import BlogHeader from '@components/BlogHeader.astro'; import BlogHeader from '@components/BlogHeader.astro';
import Tags from '@components/Tags.astro'; import Tags from '@components/Tags.astro';
@@ -6,7 +8,7 @@ interface Post {
data: { data: {
title: string; title: string;
pubDate: Date; pubDate: Date;
tags: string[]; tags: Tag[];
}; };
slug: string; slug: string;
} }
+4 -2
View File
@@ -1,6 +1,8 @@
--- ---
import { Tag } from 'src/content/config';
interface Props { interface Props {
tags: string[]; tags: Tag[];
} }
const { tags } = Astro.props; const { tags } = Astro.props;
@@ -8,7 +10,7 @@ const { tags } = Astro.props;
<div class="tag-container"> <div class="tag-container">
{ {
tags.sort().map((tag: string) => ( tags.sort().map((tag: Tag) => (
<a class="tag" href={`/archive/${tag}?page=1`}> <a class="tag" href={`/archive/${tag}?page=1`}>
#{tag} #{tag}
</a> </a>
+20 -18
View File
@@ -1,27 +1,29 @@
import { z, defineCollection } from 'astro:content'; import { z, defineCollection } from 'astro:content';
export enum Tag {
'Apps' = 'Apps',
'Digital Life' = 'Digital Life',
'Gaming' = 'Gaming',
'Learning' = 'Learning',
'Life' = 'Life',
'MacOS' = 'MacOS',
'Music' = 'Music',
'Making Stuff' = 'Making Stuff',
'Mental Health' = 'Mental Health',
'Movies & TV' = 'Movies & TV',
'Programming' = 'Programming',
'Tech' = 'Tech',
'Tutorial' = 'Tutorial',
'Web Dev' = 'Web Dev',
// only for filtering
all = 'all',
}
const blogCollection = defineCollection({ const blogCollection = defineCollection({
schema: z.object({ schema: z.object({
title: z.string(), title: z.string(),
pubDate: z.string().transform((str) => new Date(str)), pubDate: z.string().transform((str) => new Date(str)),
tags: z.array( tags: z.array(z.nativeEnum(Tag)),
z.enum([
'Apps',
'Digital Life',
'Gaming',
'Learning',
'Life',
'MacOS',
'Music',
'Making Stuff',
'Mental Health',
'Movies & TV',
'Programming',
'Tech',
'Tutorial',
'Web Dev',
])
),
}), }),
}); });
+9 -3
View File
@@ -1,19 +1,25 @@
--- ---
import { getCollection } from 'astro:content'; import { getCollection } from 'astro:content';
import { Tag as TagEnum } from '../../content/config';
import Layout from '@layouts/Layout.astro'; import Layout from '@layouts/Layout.astro';
import FormattedDate from '@components/FormattedDate.astro'; import FormattedDate from '@components/FormattedDate.astro';
const { tag } = Astro.params; const { tag }: { tag?: TagEnum } = Astro.params;
const page = Number.parseInt(Astro.url.searchParams.get('page')); const page = Number.parseInt(Astro.url.searchParams.get('page') || '1');
const prevPage = page - 1; const prevPage = page - 1;
const nextPage = page + 1; const nextPage = page + 1;
const posts = await getCollection('blog', ({ data }) => { const posts = await getCollection('blog', ({ data }) => {
if (tag === 'all') { if (!tag) {
return false;
}
if (tag === TagEnum.all) {
return true; return true;
} }
return data.tags.includes(tag); return data.tags.includes(tag);
}); });
+1
View File
@@ -3,6 +3,7 @@
"compilerOptions": { "compilerOptions": {
"baseUrl": ".", "baseUrl": ".",
"strictNullChecks": true, "strictNullChecks": true,
"strict": true,
"paths": { "paths": {
"@components/*": ["src/components/*"], "@components/*": ["src/components/*"],
"@layouts/*": ["src/layouts/*"], "@layouts/*": ["src/layouts/*"],