Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add creative work position props for series #156

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading