From 28823ed3346a1298973bd519d1e83067dfbbc5ce Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Sun, 1 Jun 2025 00:22:11 -0500 Subject: [PATCH] Add caption, semantic description conditionally to photo meta --- __tests__/photo.test.ts | 34 +++++++++++++++++++ app/film/[film]/[photoId]/page.tsx | 3 +- app/focal/[focal]/[photoId]/page.tsx | 3 +- app/lens/[make]/[model]/[photoId]/page.tsx | 3 +- app/p/[photoId]/page.tsx | 3 +- app/recipe/[recipe]/[photoId]/page.tsx | 3 +- app/shot-on/[make]/[model]/[photoId]/page.tsx | 3 +- app/tag/[tag]/[photoId]/page.tsx | 3 +- app/tag/hidden/[photoId]/page.tsx | 3 +- src/photo/index.ts | 7 +++- 10 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 __tests__/photo.test.ts diff --git a/__tests__/photo.test.ts b/__tests__/photo.test.ts new file mode 100644 index 00000000..8ced00bf --- /dev/null +++ b/__tests__/photo.test.ts @@ -0,0 +1,34 @@ +import { descriptionForPhoto, Photo } from '@/photo'; + +const PHOTO: Partial = { + takenAt: new Date('2025-01-01 12:00:00'), +}; + +const PHOTO_SEMANTIC: Partial = { + ...PHOTO, + semanticDescription: 'Semantic Description', +}; + +const PHOTO_CAPTION: Partial = { + ...PHOTO_SEMANTIC, + caption: 'Caption', +}; + +describe('Should generate photo description', () => { + it('with caption', () => { + expect(descriptionForPhoto(PHOTO_CAPTION as Photo)) + .toBe('Caption'); + }); + it('with semantic description (disabled)', () => { + expect(descriptionForPhoto(PHOTO_SEMANTIC as Photo)) + .toBe('01 JAN 2025 12:00PM'); + }); + it('with semantic description (enabled)', () => { + expect(descriptionForPhoto(PHOTO_SEMANTIC as Photo, true)) + .toBe('Semantic Description'); + }); + it('with date', () => { + expect(descriptionForPhoto(PHOTO as Photo)) + .toBe('01 JAN 2025 12:00PM'); + }); +}); diff --git a/app/film/[film]/[photoId]/page.tsx b/app/film/[film]/[photoId]/page.tsx index a81d9619..b6f00cf4 100644 --- a/app/film/[film]/[photoId]/page.tsx +++ b/app/film/[film]/[photoId]/page.tsx @@ -41,12 +41,13 @@ export async function generateMetadata({ const title = titleForPhoto(photo); const description = descriptionForPhoto(photo); + const descriptionHtml = descriptionForPhoto(photo, true); const images = absolutePathForPhotoImage(photo); const url = absolutePathForPhoto({ photo, film: film }); return { title, - description, + description: descriptionHtml, openGraph: { title, images, diff --git a/app/focal/[focal]/[photoId]/page.tsx b/app/focal/[focal]/[photoId]/page.tsx index 69894b02..6bb9d457 100644 --- a/app/focal/[focal]/[photoId]/page.tsx +++ b/app/focal/[focal]/[photoId]/page.tsx @@ -38,12 +38,13 @@ export async function generateMetadata({ const title = titleForPhoto(photo); const description = descriptionForPhoto(photo); + const descriptionHtml = descriptionForPhoto(photo, true); const images = absolutePathForPhotoImage(photo); const url = absolutePathForPhoto({ photo, focal }); return { title, - description, + description: descriptionHtml, openGraph: { title, images, diff --git a/app/lens/[make]/[model]/[photoId]/page.tsx b/app/lens/[make]/[model]/[photoId]/page.tsx index 496ea394..226a8ccc 100644 --- a/app/lens/[make]/[model]/[photoId]/page.tsx +++ b/app/lens/[make]/[model]/[photoId]/page.tsx @@ -46,6 +46,7 @@ export async function generateMetadata({ const title = titleForPhoto(photo); const description = descriptionForPhoto(photo); + const descriptionHtml = descriptionForPhoto(photo, true); const images = absolutePathForPhotoImage(photo); const url = absolutePathForPhoto({ photo, @@ -54,7 +55,7 @@ export async function generateMetadata({ return { title, - description, + description: descriptionHtml, openGraph: { title, images, diff --git a/app/p/[photoId]/page.tsx b/app/p/[photoId]/page.tsx index eff3e204..8ac6b4a6 100644 --- a/app/p/[photoId]/page.tsx +++ b/app/p/[photoId]/page.tsx @@ -38,12 +38,13 @@ export async function generateMetadata({ const title = titleForPhoto(photo); const description = descriptionForPhoto(photo); + const descriptionHtml = descriptionForPhoto(photo, true); const images = absolutePathForPhotoImage(photo); const url = absolutePathForPhoto({ photo }); return { title, - description, + description: descriptionHtml, openGraph: { title, images, diff --git a/app/recipe/[recipe]/[photoId]/page.tsx b/app/recipe/[recipe]/[photoId]/page.tsx index 7caec133..b6c1eff6 100644 --- a/app/recipe/[recipe]/[photoId]/page.tsx +++ b/app/recipe/[recipe]/[photoId]/page.tsx @@ -40,12 +40,13 @@ export async function generateMetadata({ const title = titleForPhoto(photo); const description = descriptionForPhoto(photo); + const descriptionHtml = descriptionForPhoto(photo, true); const images = absolutePathForPhotoImage(photo); const url = absolutePathForPhoto({ photo, recipe }); return { title, - description, + description: descriptionHtml, openGraph: { title, images, diff --git a/app/shot-on/[make]/[model]/[photoId]/page.tsx b/app/shot-on/[make]/[model]/[photoId]/page.tsx index a1923645..c2917a55 100644 --- a/app/shot-on/[make]/[model]/[photoId]/page.tsx +++ b/app/shot-on/[make]/[model]/[photoId]/page.tsx @@ -45,6 +45,7 @@ export async function generateMetadata({ const title = titleForPhoto(photo); const description = descriptionForPhoto(photo); + const descriptionHtml = descriptionForPhoto(photo, true); const images = absolutePathForPhotoImage(photo); const url = absolutePathForPhoto({ photo, @@ -53,7 +54,7 @@ export async function generateMetadata({ return { title, - description, + description: descriptionHtml, openGraph: { title, images, diff --git a/app/tag/[tag]/[photoId]/page.tsx b/app/tag/[tag]/[photoId]/page.tsx index d54d2ffc..86055cad 100644 --- a/app/tag/[tag]/[photoId]/page.tsx +++ b/app/tag/[tag]/[photoId]/page.tsx @@ -37,12 +37,13 @@ export async function generateMetadata({ const title = titleForPhoto(photo); const description = descriptionForPhoto(photo); + const descriptionHtml = descriptionForPhoto(photo, true); const images = absolutePathForPhotoImage(photo); const url = absolutePathForPhoto({ photo, tag }); return { title, - description, + description: descriptionHtml, openGraph: { title, images, diff --git a/app/tag/hidden/[photoId]/page.tsx b/app/tag/hidden/[photoId]/page.tsx index 02f61cab..827379a2 100644 --- a/app/tag/hidden/[photoId]/page.tsx +++ b/app/tag/hidden/[photoId]/page.tsx @@ -35,11 +35,12 @@ export async function generateMetadata({ const title = titleForPhoto(photo); const description = descriptionForPhoto(photo); + const descriptionHtml = descriptionForPhoto(photo, true); const url = absolutePathForPhoto({ photo, tag: TAG_HIDDEN }); return { title, - description, + description: descriptionHtml, openGraph: { title, description, diff --git a/src/photo/index.ts b/src/photo/index.ts index df16ce0f..5c6cd0e2 100644 --- a/src/photo/index.ts +++ b/src/photo/index.ts @@ -171,7 +171,12 @@ export const photoStatsAsString = (photo: Photo) => [ photo.isoFormatted, ].join(' '); -export const descriptionForPhoto = (photo: Photo) => +export const descriptionForPhoto = ( + photo: Photo, + includeSemanticDescription?: boolean, +) => + photo.caption || + (includeSemanticDescription && photo.semanticDescription) || formatDate({ date: photo.takenAt }).toLocaleUpperCase(); export const getPreviousPhoto = (photo: Photo, photos: Photo[]) => {