-
diff --git a/src/content/config.js b/src/content/config.ts
similarity index 100%
rename from src/content/config.js
rename to src/content/config.ts
diff --git a/src/env.d.ts b/src/env.d.ts
new file mode 100644
index 0000000..f964fe0
--- /dev/null
+++ b/src/env.d.ts
@@ -0,0 +1 @@
+///
diff --git a/src/pages/[...categories]/[category]/[...page].astro b/src/pages/[...categories]/[category]/[...page].astro
index d736c63..8fc1399 100644
--- a/src/pages/[...categories]/[category]/[...page].astro
+++ b/src/pages/[...categories]/[category]/[...page].astro
@@ -18,7 +18,7 @@ export async function getStaticPaths({ paginate }) {
typeof post.category === 'string' && categories.add(post.category.toLowerCase());
});
- return Array.from(categories).map((category) =>
+ return Array.from(categories).map((category: string) =>
paginate(
posts.filter((post) => typeof post.category === 'string' && category === post.category.toLowerCase()),
{
diff --git a/src/pages/[...tags]/[tag]/[...page].astro b/src/pages/[...tags]/[tag]/[...page].astro
index d4c8094..446391b 100644
--- a/src/pages/[...tags]/[tag]/[...page].astro
+++ b/src/pages/[...tags]/[tag]/[...page].astro
@@ -18,7 +18,7 @@ export async function getStaticPaths({ paginate }) {
Array.isArray(post.tags) && post.tags.map((tag) => tags.add(tag.toLowerCase()));
});
- return Array.from(tags).map((tag) =>
+ return Array.from(tags).map((tag: string) =>
paginate(
posts.filter((post) => Array.isArray(post.tags) && post.tags.find((elem) => elem.toLowerCase() === tag)),
{
diff --git a/src/pages/rss.xml.js b/src/pages/rss.xml.ts
similarity index 100%
rename from src/pages/rss.xml.js
rename to src/pages/rss.xml.ts
diff --git a/src/utils/directories.js b/src/utils/directories.ts
similarity index 65%
rename from src/utils/directories.js
rename to src/utils/directories.ts
index 6d37296..bf0dfe5 100644
--- a/src/utils/directories.js
+++ b/src/utils/directories.ts
@@ -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, '');
};
diff --git a/src/utils/images.js b/src/utils/images.ts
similarity index 84%
rename from src/utils/images.js
rename to src/utils/images.ts
index 676820a..16dba2e 100644
--- a/src/utils/images.js
+++ b/src/utils/images.ts
@@ -1,5 +1,5 @@
const load = async function () {
- let images = [];
+ let images: Record
Promise> | 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;
}
diff --git a/src/utils/permalinks.js b/src/utils/permalinks.ts
similarity index 79%
rename from src/utils/permalinks.js
rename to src/utils/permalinks.ts
index 0882ad9..4ab1395 100644
--- a/src/utils/permalinks.js
+++ b/src/utils/permalinks.ts
@@ -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);
diff --git a/src/utils/posts.js b/src/utils/posts.ts
similarity index 50%
rename from src/utils/posts.js
rename to src/utils/posts.ts
index b361085..3f15fa5 100644
--- a/src/utils/posts.js
+++ b/src/utils/posts.ts
@@ -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;
+ authors?: Array;
+
+ Content: unknown;
+ content?: string;
+ readingTime: number;
+}
+
+const getNormalizedPost = async (post: CollectionEntry<'blog'>): Promise => {
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> {
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;
/** */
-export const fetchPosts = async () => {
- _posts = _posts || load();
+export const fetchPosts = async (): Promise> => {
+ if (!_posts) {
+ _posts = await load();
+ }
- return await _posts;
+ return _posts;
};
/** */
-export const findPostsBySlugs = async (slugs) => {
+export const findPostsBySlugs = async (slugs: Array): Promise> => {
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, 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): Promise> => {
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> => {
const _count = count || 4;
const posts = await fetchPosts();
diff --git a/src/utils/utils.js b/src/utils/utils.ts
similarity index 76%
rename from src/utils/utils.js
rename to src/utils/utils.ts
index 1fdf5c5..f0eaeff 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.ts
@@ -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',
})
- : '';
\ No newline at end of file
+ : '';