diff --git a/src/lib/astro/astro.test.ts b/src/lib/astro/astro.test.ts index 4d0d0ecdf..4d49429ee 100644 --- a/src/lib/astro/astro.test.ts +++ b/src/lib/astro/astro.test.ts @@ -1,6 +1,17 @@ -import { test, expect } from 'vitest' +import { test, expect, afterEach, beforeEach, vi } from 'vitest' import { getPostsByTag, getAllTags } from '.' import { sortPosts } from './sortPosts' +import * as indexModule from './index' + +let getAllPostsSpy: any + +beforeEach(() => { + getAllPostsSpy = vi.spyOn(indexModule, 'getAllPosts') +}) + +afterEach(() => { + getAllPostsSpy.mockRestore() +}) test('sortPosts sorts posts by date in descending order', () => { const posts = [ @@ -8,6 +19,10 @@ test('sortPosts sorts posts by date in descending order', () => { { data: { date: '2022-01-03' } }, { data: { date: '2022-01-02' } } ] as any + + getAllPostsSpy.mockImplementationOnce(() => Promise.resolve(posts)) + + // getAllPostsSpy = vi.spyOn(getAllPosts, async () => posts) const sortedPosts = sortPosts(posts) expect(sortedPosts[0].data.date).toStrictEqual('2022-01-03') expect(sortedPosts[1].data.date).toStrictEqual('2022-01-02') @@ -20,22 +35,26 @@ test('sortPosts handles an empty array', () => { expect(sortedPosts).toEqual([]) }) -test('getPostsByTag filters posts by a given tag', () => { +test('getPostsByTag filters posts by a given tag', async () => { const posts = [ { data: { tags: ['tag1', 'tag2'] } }, { data: { tags: ['tag2', 'tag3'] } }, { data: { tags: ['tag1'] } } ] as any - const filteredPosts = getPostsByTag(posts, 'tag1') + + getAllPostsSpy.mockImplementationOnce(() => Promise.resolve(posts)) + const filteredPosts = await getPostsByTag('tag1') expect(filteredPosts.length).toBe(2) }) -test('getPostsByTag returns an empty array when no posts have the given tag', () => { +test('getPostsByTag returns an empty array when no posts have the given tag', async () => { const posts = [ { data: { tags: ['tag1', 'tag2'] } }, { data: { tags: ['tag2', 'tag3'] } } ] as any - const filteredPosts = getPostsByTag(posts, 'tag4') + + getAllPostsSpy.mockImplementationOnce(() => Promise.resolve(posts)) + const filteredPosts = await getPostsByTag('tag4') expect(filteredPosts).toEqual([]) }) @@ -45,7 +64,9 @@ test('getAllTags returns all unique tags along with their counts', async () => { { data: { tags: ['tag2', 'tag3'] } }, { data: { tags: ['tag1'] } } ] as any - const allTags = await getAllTags(posts) + + getAllPostsSpy.mockImplementationOnce(() => Promise.resolve(posts)) + const allTags = await getAllTags() expect(allTags).toEqual([ { name: 'tag1', count: 2 }, { name: 'tag2', count: 2 }, diff --git a/src/lib/astro/getAllPostsForSearch.ts b/src/lib/astro/getAllPostsForSearch.ts index dd0d9694d..9bb78903c 100644 --- a/src/lib/astro/getAllPostsForSearch.ts +++ b/src/lib/astro/getAllPostsForSearch.ts @@ -2,7 +2,6 @@ import { type CollectionEntry } from 'astro:content' import { getAllPosts } from './index' // helps to reduce DOM size - export async function getAllPostsForSearch() { const allPosts = await getAllPosts() diff --git a/src/lib/astro/getAllTags.ts b/src/lib/astro/getAllTags.ts index 0c4374b52..a80c63ff5 100644 --- a/src/lib/astro/getAllTags.ts +++ b/src/lib/astro/getAllTags.ts @@ -1,4 +1,4 @@ -import { type CollectionEntry } from 'astro:content' +import { getAllPosts } from './index' import { slugifyAll } from '../slugify' export type AllTags = { @@ -6,10 +6,9 @@ export type AllTags = { count: number }[] -export async function getAllTags( - posts: CollectionEntry<'articles' | 'links' | 'photos'>[] -): Promise { - const allTagsArray = posts +export async function getAllTags(): Promise { + const allPosts = await getAllPosts() + const allTagsArray = allPosts .filter((post) => post.data.tags) .map((post) => post.data.tags) .flat() as string[] diff --git a/src/lib/astro/getPostsByTag.ts b/src/lib/astro/getPostsByTag.ts index ea9ce14d4..ace4f73ac 100644 --- a/src/lib/astro/getPostsByTag.ts +++ b/src/lib/astro/getPostsByTag.ts @@ -1,9 +1,9 @@ -import { type CollectionEntry } from 'astro:content' +import { getAllPosts } from './index' import { slugifyAll } from '../slugify' -export function getPostsByTag( - posts: CollectionEntry<'articles' | 'links' | 'photos'>[], - tag: string -) { - return posts.filter((post) => slugifyAll(post.data.tags || []).includes(tag)) +export async function getPostsByTag(tag: string) { + const allPosts = await getAllPosts() + return allPosts.filter((post) => + slugifyAll(post.data.tags || []).includes(tag) + ) } diff --git a/src/lib/astro/loadAndFormatCollection.ts b/src/lib/astro/loadAndFormatCollection.ts index 206bb9e7b..d511ba8d7 100644 --- a/src/lib/astro/loadAndFormatCollection.ts +++ b/src/lib/astro/loadAndFormatCollection.ts @@ -1,5 +1,5 @@ import { getCollection, type CollectionEntry } from 'astro:content' -import { readOutExif } from '../exif' +import { readOutExif } from '@lib/exif' import path from 'path' import config from '@config/blog.config' import { sortPosts } from './sortPosts' @@ -10,7 +10,6 @@ import { sortPosts } from './sortPosts' // Astro's `getCollection()` is never called // from components, but this helper method instead. // - export async function loadAndFormatCollection( name: 'articles' | 'links' | 'photos' ) { @@ -22,12 +21,16 @@ export async function loadAndFormatCollection( } for await (const post of postsCollection) { + // // use date from frontmatter, or grab from folder path + // const date = post.data.date ? post.data.date : new Date(post.id.split('/')[0].substring(0, 10)) + // // remove date from slug + // let slug = post.id.split('/')[0].substring(11) as CollectionEntry< 'articles' | 'photos' | 'links' >['slug'] @@ -42,11 +45,16 @@ export async function loadAndFormatCollection( const githubLink = `${config.repoContentPath}/${post.collection}/${post.id}` + post.slug = slug + post.data.date = date + post.data.githubLink = githubLink + + // // extract exif & iptc data from photos + // if (post.collection === 'photos') { const isProd = import.meta.env.PROD - // // Get the absolute image path from post.data.image // to read exif from // @@ -54,7 +62,6 @@ export async function loadAndFormatCollection( // `/_astro/filename.hash.jpg` // development image.src: // `/@fs/absolute/system/path/project/src/content/photos/postSlug/filename.jpg?origWidth=3873&origHeight=2796&origFormat=jpg` - // const imagePath = isProd ? path.join( 'content', @@ -66,10 +73,6 @@ export async function loadAndFormatCollection( const exif = await readOutExif(imagePath) post.data.exif = exif } - - post.slug = slug - post.data.date = date - post.data.githubLink = githubLink } const posts = sortPosts(postsCollection) diff --git a/src/lib/astro/sortPosts.ts b/src/lib/astro/sortPosts.ts index 547ecb6bf..5e4858070 100644 --- a/src/lib/astro/sortPosts.ts +++ b/src/lib/astro/sortPosts.ts @@ -1,5 +1,8 @@ import { type CollectionEntry } from 'astro:content' +// +// Sort posts by date, newest first +// export function sortPosts( posts: CollectionEntry<'articles' | 'links' | 'photos'>[] ): CollectionEntry<'articles' | 'links' | 'photos'>[] {