Skip to content

Commit

Permalink
Fix astro check errors on strict typescript, issue onwidget#293
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypa committed Dec 2, 2023
1 parent cf40b9d commit 788acd9
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/components/blog/Pagination.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { getPermalink } from '~/utils/permalinks';
import Button from '~/components/ui/Button.astro';
export interface Props {
prevUrl: string;
nextUrl: string;
prevUrl?: string;
nextUrl?: string;
prevText?: string;
nextText?: string;
}
Expand Down
10 changes: 7 additions & 3 deletions src/pages/[...blog]/[...page].astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
import Layout from '~/layouts/PageLayout.astro';
import BlogList from '~/components/blog/List.astro';
import Headline from '~/components/blog/Headline.astro';
Expand All @@ -9,11 +11,13 @@ import { blogListRobots, getStaticPathsBlogList } from '~/utils/blog';
export const prerender = true;
export async function getStaticPaths ({ paginate }) {
export const getStaticPaths = (async ({ paginate }) => {
return await getStaticPathsBlogList({ paginate });
}
}) satisfies GetStaticPaths;
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { page } = Astro.props;
const { page } = Astro.props as Props;
const currentPage = page.currentPage ?? 1;
// const allCategories = await findCategories();
Expand Down
9 changes: 6 additions & 3 deletions src/pages/[...blog]/[category]/[...page].astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
import { blogCategoryRobots, getStaticPathsBlogCategory } from '~/utils/blog';
import Layout from '~/layouts/PageLayout.astro';
Expand All @@ -8,11 +9,13 @@ import Pagination from '~/components/blog/Pagination.astro';
export const prerender = true;
export async function getStaticPaths ({ paginate }) {
export const getStaticPaths = (async ({ paginate }) => {
return await getStaticPathsBlogCategory({ paginate });
}
}) satisfies GetStaticPaths;
const { page, category } = Astro.props;
type Props = InferGetStaticPropsType<typeof getStaticPaths> & { category: string };
const { page, category } = Astro.props as Props;
const currentPage = page.currentPage ?? 1;
Expand Down
9 changes: 6 additions & 3 deletions src/pages/[...blog]/[tag]/[...page].astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
import { blogTagRobots, getStaticPathsBlogTag } from '~/utils/blog';
import Layout from '~/layouts/PageLayout.astro';
Expand All @@ -8,11 +9,13 @@ import Pagination from '~/components/blog/Pagination.astro';
export const prerender = true;
export async function getStaticPaths ({ paginate }) {
export const getStaticPaths = (async ({ paginate }) => {
return await getStaticPathsBlogTag({ paginate });
}
}) satisfies GetStaticPaths;
const { page, tag } = Astro.props;
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { page, tag } = Astro.props as Props;
const currentPage = page.currentPage ?? 1;
Expand Down
17 changes: 11 additions & 6 deletions src/pages/[...blog]/index.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
import merge from 'lodash.merge';
import type { ImageMetadata } from 'astro';
import Layout from '~/layouts/PageLayout.astro';
Expand All @@ -8,17 +10,20 @@ import ToBlogLink from '~/components/blog/ToBlogLink.astro';
import { getCanonical, getPermalink } from '~/utils/permalinks';
import { getStaticPathsBlogPost, blogPostRobots } from '~/utils/blog';
import { findImage } from '~/utils/images';
import type { MetaData } from '~/types';
export const prerender = true;
export async function getStaticPaths () {
export const getStaticPaths = (async () => {
return await getStaticPathsBlogPost();
}
}) satisfies GetStaticPaths;
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { post } = Astro.props;
const { post } = Astro.props as Props;
const url = getCanonical(getPermalink(post.permalink, 'post'));
const image = (await findImage(post.image)) as ImageMetadata | undefined;
const image = (await findImage(post.image)) as ImageMetadata | string | undefined;
const metadata = merge(
{
Expand All @@ -30,11 +35,11 @@ const metadata = merge(
},
openGraph: {
type: 'article',
...(image ? { images: [{ url: image, width: image?.width, height: image?.height }] } : {}),
...(image ? { images: [{ url: image, width: (image as ImageMetadata)?.width, height: (image as ImageMetadata)?.height }] } : {}),
},
},
{ ...(post?.metadata ? { ...post.metadata, canonical: post.metadata?.canonical || url } : {}) }
);
) as MetaData;
---

<Layout metadata={metadata}>
Expand Down
4 changes: 2 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AstroComponentFactory } from 'astro/runtime/server/index.js';
import type { HTMLAttributes } from 'astro/types';
import type { HTMLAttributes, ImageMetadata } from 'astro/types';

export interface Post {
/** A unique ID number that identifies a post. */
Expand All @@ -21,7 +21,7 @@ export interface Post {
/** Optional summary of post content. */
excerpt?: string;
/** */
image?: string;
image?: ImageMetadata | string;

/** */
category?: string;
Expand Down
15 changes: 8 additions & 7 deletions src/utils/blog.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PaginateFunction } from 'astro';
import { getCollection } from 'astro:content';
import type { CollectionEntry } from 'astro:content';
import type { Post } from '~/types';
Expand Down Expand Up @@ -162,7 +163,7 @@ export const findLatestPosts = async ({ count }: { count?: number }): Promise<Ar
};

/** */
export const getStaticPathsBlogList = async ({ paginate }) => {
export const getStaticPathsBlogList = async ({ paginate }: { paginate: PaginateFunction }) => {
if (!isBlogEnabled || !isBlogListRouteEnabled) return [];
return paginate(await fetchPosts(), {
params: { blog: BLOG_BASE || undefined },
Expand All @@ -182,16 +183,16 @@ export const getStaticPathsBlogPost = async () => {
};

/** */
export const getStaticPathsBlogCategory = async ({ paginate }) => {
export const getStaticPathsBlogCategory = async ({ paginate }: { paginate: PaginateFunction }) => {
if (!isBlogEnabled || !isBlogCategoryRouteEnabled) return [];

const posts = await fetchPosts();
const categories = new Set();
const categories = new Set<string>();
posts.map((post) => {
typeof post.category === 'string' && categories.add(post.category.toLowerCase());
});

return Array.from(categories).flatMap((category: string) =>
return Array.from(categories).flatMap((category) =>
paginate(
posts.filter((post) => typeof post.category === 'string' && category === post.category.toLowerCase()),
{
Expand All @@ -204,16 +205,16 @@ export const getStaticPathsBlogCategory = async ({ paginate }) => {
};

/** */
export const getStaticPathsBlogTag = async ({ paginate }) => {
export const getStaticPathsBlogTag = async ({ paginate }: { paginate: PaginateFunction }) => {
if (!isBlogEnabled || !isBlogTagRouteEnabled) return [];

const posts = await fetchPosts();
const tags = new Set();
const tags = new Set<string>();
posts.map((post) => {
Array.isArray(post.tags) && post.tags.map((tag) => tags.add(tag.toLowerCase()));
});

return Array.from(tags).flatMap((tag: string) =>
return Array.from(tags).flatMap((tag) =>
paginate(
posts.filter((post) => Array.isArray(post.tags) && post.tags.find((elem) => elem.toLowerCase() === tag)),
{
Expand Down

0 comments on commit 788acd9

Please sign in to comment.