Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kremalicious committed Sep 15, 2023
1 parent 4bc3fad commit 4e65838
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
33 changes: 27 additions & 6 deletions src/lib/astro/astro.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
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 = [
{ data: { date: '2022-01-01' } },
{ 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')
Expand All @@ -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([])
})

Expand All @@ -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 },
Expand Down
1 change: 0 additions & 1 deletion src/lib/astro/getAllPostsForSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 4 additions & 5 deletions src/lib/astro/getAllTags.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { type CollectionEntry } from 'astro:content'
import { getAllPosts } from './index'
import { slugifyAll } from '../slugify'

export type AllTags = {
name: string
count: number
}[]

export async function getAllTags(
posts: CollectionEntry<'articles' | 'links' | 'photos'>[]
): Promise<AllTags> {
const allTagsArray = posts
export async function getAllTags(): Promise<AllTags> {
const allPosts = await getAllPosts()
const allTagsArray = allPosts
.filter((post) => post.data.tags)
.map((post) => post.data.tags)
.flat() as string[]
Expand Down
12 changes: 6 additions & 6 deletions src/lib/astro/getPostsByTag.ts
Original file line number Diff line number Diff line change
@@ -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)
)
}
19 changes: 11 additions & 8 deletions src/lib/astro/loadAndFormatCollection.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
) {
Expand All @@ -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']
Expand All @@ -42,19 +45,23 @@ 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
//
// production image.src:
// `/_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',
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/lib/astro/sortPosts.ts
Original file line number Diff line number Diff line change
@@ -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'>[] {
Expand Down

0 comments on commit 4e65838

Please sign in to comment.