Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "astro preview",
"astro": "astro",
"format": "prettier -w .",
"lint:eslint": "eslint .",
"lint": "eslint .",
"subfont": "subfont -ir --no-fallbacks --silent --root dist"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/MetaTags.astro
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const image =
typeof _image === 'string'
? new URL(_image, Astro.site)
: _image && typeof _image['src'] !== 'undefined'
? // @ts-ignore
? // @ts-expect-error - TODO: Need to improve default image handling and types
new URL(getRelativeUrlByFilePath(_image.src), Astro.site)
: null;
---
Expand Down
3 changes: 3 additions & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />
/// <reference types="vite/client" />
/// <reference types="../vendor/integration/types.d.ts" />
27 changes: 12 additions & 15 deletions src/utils/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ const load = async function (): Promise<Array<Post>> {

let _posts: Array<Post>;

/** */
export const isBlogEnabled = true;
export const isBlogListRouteEnabled = true;
export const isBlogPostRouteEnabled = true;
Expand All @@ -119,7 +118,6 @@ export const isBlogTagRouteEnabled = true;

export const blogPostsPerPage = 6;

/** */
export const fetchPosts = async (): Promise<Array<Post>> => {
if (!_posts) {
_posts = await load();
Expand All @@ -128,7 +126,6 @@ export const fetchPosts = async (): Promise<Array<Post>> => {
return _posts;
};

/** */
export const findPostsBySlugs = async (slugs: Array<string>): Promise<Array<Post>> => {
if (!Array.isArray(slugs)) return [];

Expand All @@ -142,7 +139,6 @@ export const findPostsBySlugs = async (slugs: Array<string>): Promise<Array<Post
}, []);
};

/** */
export const findPostsByIds = async (ids: Array<string>): Promise<Array<Post>> => {
if (!Array.isArray(ids)) return [];

Expand All @@ -156,15 +152,13 @@ export const findPostsByIds = async (ids: Array<string>): Promise<Array<Post>> =
}, []);
};

/** */
export const findLatestPosts = async ({ count }: { count?: number }): Promise<Array<Post>> => {
const _count = count ?? 4;
const posts = await fetchPosts();

return posts ? posts.slice(0, _count) : [];
};

/** */
export const getStaticPathsBlogList = async ({ paginate }: { paginate: PaginateFunction }) => {
if (!isBlogEnabled || !isBlogListRouteEnabled) return [];
return paginate(await fetchPosts(), {
Expand All @@ -173,7 +167,6 @@ export const getStaticPathsBlogList = async ({ paginate }: { paginate: PaginateF
});
};

/** */
export const getStaticPathsBlogPost = async () => {
if (!isBlogEnabled || !isBlogPostRouteEnabled) return [];
return (await fetchPosts()).flatMap((post) => ({
Expand All @@ -184,15 +177,16 @@ export const getStaticPathsBlogPost = async () => {
}));
};

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

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

return Array.from(categories).flatMap((category: string) =>
paginate(
Expand All @@ -209,15 +203,18 @@ export const getStaticPathsBlogCategory = async ({ paginate }: { paginate: Pagin
);
};

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

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

return Array.from(tags).flatMap((tag: string) =>
paginate(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/images-optimization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { HTMLAttributes } from 'astro/types';

type Layout = 'fixed' | 'constrained' | 'fullWidth' | 'cover' | 'responsive' | 'contained';

export interface AttributesProps extends HTMLAttributes<'img'> {}
export type AttributesProps = HTMLAttributes<'img'>;

export interface ImageProps extends Omit<HTMLAttributes<'img'>, 'src'> {
src?: string | ImageMetadata | null;
Expand Down
21 changes: 10 additions & 11 deletions src/utils/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ const load = async function () {
'~/assets/images/**/*.{jpeg,jpg,png,tiff,webp,gif,svg,JPEG,JPG,PNG,TIFF,WEBP,GIF,SVG}'
);
} catch (e) {
// continue regardless of error
console.error('Error loading images:', e);
}
return images;
};

let _images: Record<string, () => Promise<unknown>> | undefined = undefined;

/** */
export const fetchLocalImages = async () => {
_images = _images || (await load());
return _images;
};

/** */
export const findImage = async (
imagePath?: string | ImageMetadata | null
): Promise<string | ImageMetadata | undefined | null> => {
Expand Down Expand Up @@ -53,7 +51,6 @@ export const findImage = async (
: null;
};

/** */
export const adaptOpenGraphImages = async (
openGraph: OpenGraph = {},
astroSite: URL | undefined = new URL('')
Expand All @@ -66,14 +63,15 @@ export const adaptOpenGraphImages = async (
const defaultWidth = 1200;
const defaultHeight = 626;

// TODO: Refactor
const adaptedImages = await Promise.all(
images.map(async (image: OpenGraphMedia) => {
if (image?.url) {
const resolvedImage = (await findImage(image.url)) as ImageMetadata | undefined;
const resolvedImage = await findImage(image.url);
if (!resolvedImage) {
return {
url: '',
};
} satisfies OpenGraphMedia;
}

const _image = await getImage({
Expand All @@ -86,18 +84,19 @@ export const adaptOpenGraphImages = async (
if (typeof _image === 'object') {
return {
url: typeof _image.src === 'string' ? String(new URL(_image.src, astroSite)) : 'pepe',
width: typeof _image.options.width === 'number' ? _image.options.width : undefined,
height: typeof _image.options.height === 'number' ? _image.options.height : undefined,
};
width: typeof _image.options.width === 'number' ? _image.options.width : defaultWidth,
height:
typeof _image.options.height === 'number' ? _image.options.height : defaultHeight,
} satisfies OpenGraphMedia;
}
return {
url: '',
};
} satisfies OpenGraphMedia;
}

return {
url: '',
};
} satisfies OpenGraphMedia;
})
);

Expand Down
8 changes: 1 addition & 7 deletions src/utils/permalinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ export const BLOG_BASE = cleanSlug('blog');
export const CATEGORY_BASE = cleanSlug('category');
export const TAG_BASE = cleanSlug('tag') || 'tag';

export const POST_PERMALINK_PATTERN = trimSlash('/blog/%slug%' || `${BLOG_BASE}/%slug%`);
export const POST_PERMALINK_PATTERN = trimSlash(`${BLOG_BASE}/%slug%`);

/** */
export const getCanonical = (path = ''): string | URL => new URL(path, SITE.origin);

/** */
export const getPermalink = (slug = '', type = 'page'): string => {
let permalink: string;

Expand All @@ -55,19 +53,15 @@ export const getPermalink = (slug = '', type = 'page'): string => {
return definitivePermalink(permalink);
};

/** */
export const getHomePermalink = (): string => getPermalink('/');

/** */
export const getBlogPermalink = (): string => getPermalink(BLOG_BASE);

/** */
export const getAsset = (path: string): string =>
'/' +
[BASE_PATHNAME, path]
.map((el) => trimSlash(el))
.filter((el) => !!el)
.join('/');

/** */
const definitivePermalink = (permalink: string): string => createPath(BASE_PATHNAME, permalink);
9 changes: 5 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"extends": "astro/tsconfigs/strictest",
"compilerOptions": {
"jsx": "react-jsx",
"allowJs": true,
"checkJs": true,
"baseUrl": ".",
"strictNullChecks": true,
"paths": {
"~/*": ["src/*"]
},
"jsxImportSource": "react"
}
}
},
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist/"]
}