Skip to content

Commit

Permalink
Merge pull request #156 from thegalactiks/creative-series-position
Browse files Browse the repository at this point in the history
feat: add creative work position props for series
  • Loading branch information
emmanuelgautier authored Feb 14, 2024
2 parents 06a085b + 88c9180 commit 66f5a9a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/contentlayer/src/fields/creative-work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
};
43 changes: 43 additions & 0 deletions packages/explorer/src/content/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,46 @@ export const getIndexPage = async (): Promise<Content | undefined> => {

return getHomePage({ inLanguage });
};

type ContentWithIsPartOf = Content & Required<Pick<Content, 'isPartOf'>>;

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<Content & Required<Pick<Content, 'position'>>>
);

export const getPreviousWorkSeries = async (content: Content): Promise<Content | undefined> => {
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<Content | undefined> => {
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;
}
}
}

0 comments on commit 66f5a9a

Please sign in to comment.