From 88c91805287e413015a4b21a736c10f084b88994 Mon Sep 17 00:00:00 2001 From: Emmanuel Gautier Date: Wed, 14 Feb 2024 15:43:48 +0100 Subject: [PATCH] feat: add creative work position props for series --- .../contentlayer/src/fields/creative-work.ts | 1 + .../src/content/repositories/index.ts | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/packages/contentlayer/src/fields/creative-work.ts b/packages/contentlayer/src/fields/creative-work.ts index a4cfce94..1361b2e0 100644 --- a/packages/contentlayer/src/fields/creative-work.ts +++ b/packages/contentlayer/src/fields/creative-work.ts @@ -10,4 +10,5 @@ export const creativeWorkFields: FieldDefs = { datePublished: { type: 'date', required: false }, isPartOf: { type: 'string', required: false }, keywords: { type: 'list', required: false, of: { type: 'string' } }, + position: { type: 'number', required: false }, }; diff --git a/packages/explorer/src/content/repositories/index.ts b/packages/explorer/src/content/repositories/index.ts index ba51b374..2b5702b0 100644 --- a/packages/explorer/src/content/repositories/index.ts +++ b/packages/explorer/src/content/repositories/index.ts @@ -91,3 +91,46 @@ export const getIndexPage = async (): Promise => { return getHomePage({ inLanguage }); }; + +type ContentWithIsPartOf = Content & Required>; + +export const getSerieWorks = async (content: ContentWithIsPartOf) => ( + ( + await getPagesPartOf(content.isPartOf, { + type: content.type, + inLanguage: content.inLanguage, + }) + ).filter(w => 'position' in w && typeof w.position === "number") + .sort((a, b) => (a.position as number) - (b.position as number)) as Array>> +); + +export const getPreviousWorkSeries = async (content: Content): Promise => { + if (!(content.isPartOf && typeof content.position === 'number')) { + return undefined; + } + + const serieWorks = await getSerieWorks(content as ContentWithIsPartOf); + let previousWork = undefined; + for (const work of serieWorks) { + if (work.position === content.position) { + return previousWork; + } + + previousWork = work; + } + + return previousWork; +} + +export const getNextWorkSeries = async (content: Content): Promise => { + if (!(content.isPartOf && typeof content.position === 'number')) { + return undefined; + } + + const serieWorks = await getSerieWorks(content as ContentWithIsPartOf); + for (const work of serieWorks) { + if (work.position > content.position) { + return work; + } + } +}