Migrate to typescript
This commit is contained in:
@ -4,7 +4,7 @@ import { fileURLToPath } from 'url';
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
/** */
|
||||
export const getProjectRootDir = () => {
|
||||
export const getProjectRootDir = (): string => {
|
||||
const mode = import.meta.env.MODE;
|
||||
|
||||
return mode === 'production' ? path.join(__dirname, '../') : path.join(__dirname, '../../');
|
||||
@ -13,10 +13,6 @@ export const getProjectRootDir = () => {
|
||||
const __srcFolder = path.join(getProjectRootDir(), '/src');
|
||||
|
||||
/** */
|
||||
export const getRelativeUrlByFilePath = (filepath) => {
|
||||
if (filepath) {
|
||||
return filepath.replace(__srcFolder, '');
|
||||
}
|
||||
|
||||
return null;
|
||||
export const getRelativeUrlByFilePath = (filepath: string): string | URL => {
|
||||
return filepath.replace(__srcFolder, '');
|
||||
};
|
@ -1,5 +1,5 @@
|
||||
const load = async function () {
|
||||
let images = [];
|
||||
let images: Record<string, () => Promise<unknown>> | undefined = undefined;
|
||||
try {
|
||||
images = import.meta.glob('~/assets/images/**');
|
||||
} catch (e) {
|
||||
@ -17,7 +17,7 @@ export const fetchLocalImages = async () => {
|
||||
};
|
||||
|
||||
/** */
|
||||
export const findImage = async (imagePath) => {
|
||||
export const findImage = async (imagePath?: string) => {
|
||||
if (typeof imagePath !== 'string') {
|
||||
return null;
|
||||
}
|
@ -2,7 +2,7 @@ import slugify from 'limax';
|
||||
|
||||
import { SITE, BLOG } from '~/config.mjs';
|
||||
|
||||
const trim = (str = "", ch) => {
|
||||
const trim = (str = '', ch?: string) => {
|
||||
let start = 0,
|
||||
end = str.length || 0;
|
||||
while (start < end && str[start] === ch) ++start;
|
||||
@ -18,7 +18,7 @@ const createPath = (...params) => {
|
||||
|
||||
const basePathname = trimSlash(SITE.basePathname);
|
||||
|
||||
export const cleanSlug = (text) => slugify(trimSlash(text));
|
||||
export const cleanSlug = (text: string) => slugify(trimSlash(text));
|
||||
|
||||
export const BLOG_BASE = cleanSlug(BLOG?.blog?.pathname);
|
||||
export const POST_BASE = cleanSlug(BLOG?.post?.pathname);
|
||||
@ -29,7 +29,7 @@ export const TAG_BASE = cleanSlug(BLOG?.tag?.pathname);
|
||||
export const getCanonical = (path = '') => new URL(path, SITE.origin);
|
||||
|
||||
/** */
|
||||
export const getPermalink = (slug = '', type = 'page') => {
|
||||
export const getPermalink = (slug = '', type = 'page'): string => {
|
||||
const _slug = cleanSlug(slug);
|
||||
|
||||
switch (type) {
|
||||
@ -49,15 +49,15 @@ export const getPermalink = (slug = '', type = 'page') => {
|
||||
};
|
||||
|
||||
/** */
|
||||
export const getHomePermalink = () => {
|
||||
export const getHomePermalink = (): string => {
|
||||
const permalink = getPermalink();
|
||||
return permalink !== '/' ? permalink + '/' : permalink;
|
||||
};
|
||||
|
||||
/** */
|
||||
export const getRelativeLink = (link = "") => {
|
||||
export const getRelativeLink = (link = ''): string => {
|
||||
return createPath(basePathname, trimSlash(link));
|
||||
}
|
||||
};
|
||||
|
||||
/** */
|
||||
export const getBlogPermalink = () => getPermalink(BLOG_BASE);
|
||||
export const getBlogPermalink = (): string => getPermalink(BLOG_BASE);
|
@ -1,6 +1,32 @@
|
||||
import { getCollection, getEntry } from 'astro:content';
|
||||
import type { CollectionEntry } from 'astro:content';
|
||||
|
||||
const getNormalizedPost = async (post) => {
|
||||
export interface Post {
|
||||
id: string;
|
||||
slug: string;
|
||||
|
||||
publishDate: Date;
|
||||
title: string;
|
||||
description?: string;
|
||||
|
||||
image?: string;
|
||||
|
||||
canonical?: string;
|
||||
permalink?: string;
|
||||
|
||||
draft?: boolean;
|
||||
|
||||
excerpt?: string;
|
||||
category?: string;
|
||||
tags?: Array<string>;
|
||||
authors?: Array<string>;
|
||||
|
||||
Content: unknown;
|
||||
content?: string;
|
||||
readingTime: number;
|
||||
}
|
||||
|
||||
const getNormalizedPost = async (post: CollectionEntry<'blog'>): Promise<Post> => {
|
||||
const { id, slug, data } = post;
|
||||
const { Content, injectedFrontmatter } = await post.render();
|
||||
|
||||
@ -16,7 +42,7 @@ const getNormalizedPost = async (post) => {
|
||||
};
|
||||
};
|
||||
|
||||
const load = async function () {
|
||||
const load = async function (): Promise<Array<Post>> {
|
||||
const posts = await getCollection('blog');
|
||||
const normalizedPosts = posts.map(async (post) => await getNormalizedPost(post));
|
||||
|
||||
@ -27,23 +53,25 @@ const load = async function () {
|
||||
return results;
|
||||
};
|
||||
|
||||
let _posts;
|
||||
let _posts: Array<Post>;
|
||||
|
||||
/** */
|
||||
export const fetchPosts = async () => {
|
||||
_posts = _posts || load();
|
||||
export const fetchPosts = async (): Promise<Array<Post>> => {
|
||||
if (!_posts) {
|
||||
_posts = await load();
|
||||
}
|
||||
|
||||
return await _posts;
|
||||
return _posts;
|
||||
};
|
||||
|
||||
/** */
|
||||
export const findPostsBySlugs = async (slugs) => {
|
||||
export const findPostsBySlugs = async (slugs: Array<string>): Promise<Array<Post>> => {
|
||||
if (!Array.isArray(slugs)) return [];
|
||||
|
||||
const posts = await fetchPosts();
|
||||
|
||||
return slugs.reduce(function (r, slug) {
|
||||
posts.some(function (post) {
|
||||
return slugs.reduce(function (r: Array<Post>, slug: string) {
|
||||
posts.some(function (post: Post) {
|
||||
return slug === post.slug && r.push(post);
|
||||
});
|
||||
return r;
|
||||
@ -51,11 +79,11 @@ export const findPostsBySlugs = async (slugs) => {
|
||||
};
|
||||
|
||||
/** */
|
||||
export const findPostsByIds = async (ids) => {
|
||||
export const findPostsByIds = async (ids: Array<string>): Promise<Array<Post>> => {
|
||||
if (!Array.isArray(ids)) return [];
|
||||
|
||||
return await Promise.all(
|
||||
ids.map(async (id) => {
|
||||
ids.map(async (id: never) => {
|
||||
const post = await getEntry('blog', id);
|
||||
return await getNormalizedPost(post);
|
||||
})
|
||||
@ -63,7 +91,7 @@ export const findPostsByIds = async (ids) => {
|
||||
};
|
||||
|
||||
/** */
|
||||
export const findLatestPosts = async ({ count }) => {
|
||||
export const findLatestPosts = async ({ count }: { count?: number }): Promise<Array<Post>> => {
|
||||
const _count = count || 4;
|
||||
const posts = await fetchPosts();
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-mixed-spaces-and-tabs */
|
||||
/** */
|
||||
export const getFormattedDate = (date) =>
|
||||
date
|
||||
@ -6,4 +7,4 @@ export const getFormattedDate = (date) =>
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
})
|
||||
: '';
|
||||
: '';
|
Reference in New Issue
Block a user