refactor: update imports and frontmatter types

This commit is contained in:
satnaing
2023-01-28 00:47:21 +06:30
parent d67e64e02d
commit c19f3a0e5a
4 changed files with 12 additions and 14 deletions
+3 -4
View File
@@ -1,8 +1,7 @@
import { slufigyAll } from "./slugify";
import type { MarkdownInstance } from "astro";
import type { Frontmatter } from "../types";
import type { CollectionEntry } from "astro:content";
const getPostsByTag = (posts: MarkdownInstance<Frontmatter>[], tag: string) =>
posts.filter(post => slufigyAll(post.frontmatter.tags).includes(tag));
const getPostsByTag = (posts: CollectionEntry<"blog">[], tag: string) =>
posts.filter(post => slufigyAll(post.data.tags).includes(tag));
export default getPostsByTag;
+2 -2
View File
@@ -5,8 +5,8 @@ const getSortedPosts = (posts: CollectionEntry<"blog">[]) =>
.filter(({ data }) => !data.draft)
.sort(
(a, b) =>
Math.floor(new Date(b.data.publishDatetime).getTime() / 1000) -
Math.floor(new Date(a.data.publishDatetime).getTime() / 1000)
Math.floor(new Date(b.data.pubDatetime).getTime() / 1000) -
Math.floor(new Date(a.data.pubDatetime).getTime() / 1000)
);
export default getSortedPosts;
+4 -5
View File
@@ -1,12 +1,11 @@
import { slugifyStr } from "./slugify";
import type { MarkdownInstance } from "astro";
import type { Frontmatter } from "../types";
import type { CollectionEntry } from "astro:content";
const getUniqueTags = (posts: MarkdownInstance<Frontmatter>[]) => {
const getUniqueTags = (posts: CollectionEntry<"blog">[]) => {
let tags: string[] = [];
const filteredPosts = posts.filter(({ frontmatter }) => !frontmatter.draft);
const filteredPosts = posts.filter(({ data }) => !data.draft);
filteredPosts.forEach(post => {
tags = [...tags, ...post.frontmatter.tags]
tags = [...tags, ...post.data.tags]
.map(tag => slugifyStr(tag))
.filter(
(value: string, index: number, self: string[]) =>
+3 -3
View File
@@ -1,10 +1,10 @@
import type { CollectionEntry } from "astro:content";
import { slug as slugger } from "github-slugger";
import type { BlogFrontmatter } from "@content/_schemas";
export const slugifyStr = (str: string) => slugger(str);
const slugify = (post: CollectionEntry<"blog">) =>
post.data.slug ? slugger(post.data.slug) : post.slug;
const slugify = (post: BlogFrontmatter) =>
post.postSlug ? slugger(post.postSlug) : slugger(post.title);
export const slufigyAll = (arr: string[]) => arr.map(str => slugifyStr(str));