diff --git a/src/content/config.ts b/src/content/config.ts index 4242413..af921bf 100644 --- a/src/content/config.ts +++ b/src/content/config.ts @@ -1,5 +1,4 @@ -import { z, defineCollection } from "astro:content"; -import episodesLoader from "src/loaders/episodes"; +import { z, defineCollection, reference } from "astro:content"; if (!import.meta.env.GOOGLE_SHEET_APP_URL.startsWith('https')) { throw new Error("GOOGLE_SHEET_APP_URL not set at build time") @@ -25,7 +24,37 @@ const seasons = defineCollection({ const episodes = defineCollection({ type: 'content_layer', - loader: episodesLoader, + loader: async () => { + const references = await fetch(`${import.meta.env.GOOGLE_SHEET_APP_URL}?s=References`) + .then(r => r.json()) + .then(r => r.filter((e: any) => e.episode && e.season)); + + const episodes = await fetch(`${import.meta.env.GOOGLE_SHEET_APP_URL}?s=Episodes`) + .then(r => r.json()) + .then(r => r + .filter((e: any) => e.season && e.episode) + .sort((e: any) => e.season + e.episode) + ); + + return episodes.map((e: any) => ({ + id: `${e.season}:${e.episode}`, + ...e, + references: references.filter((r: any) => r.season === e.season && r.episode === e.episode) + })); + }, + schema: z.object({ + title: z.string(), + description: z.string(), + season: reference('seasons'), + episode: z.string(), + watchUrl: z.string().url(), + references: z.array(z.object({ + timeRange: z.string().optional(), + notes: z.string(), + passage: z.string(), + type: z.string(), + })) + }), }); export const collections = { diff --git a/src/loaders/episodes.ts b/src/loaders/episodes.ts deleted file mode 100644 index 8b17fc5..0000000 --- a/src/loaders/episodes.ts +++ /dev/null @@ -1,44 +0,0 @@ -import type { LoaderContext } from "astro/loaders"; -import { reference, z } from "astro:content"; - -export default { - name: 'episodes-loader', - load: async (context: LoaderContext) => { - const references = await fetch(`${import.meta.env.GOOGLE_SHEET_APP_URL}?s=References`) - .then(r => r.json()) - .then(r => r.filter((e: any) => e.episode && e.season)); - - const episodesResponse = await fetch(`${import.meta.env.GOOGLE_SHEET_APP_URL}?s=Episodes`); - const episodes = (await episodesResponse.json()).filter((e: any) => e.season && e.episode).sort((e: any) => e.season + e.episode); - - const data = episodes.map((e: any) => ({ - id: `${e.season}:${e.episode}`, - ...e, - references: references.filter((r: any) => r.season === e.season && r.episode === e.episode) - })) - - for (const item of data) { - const parsed = await context.parseData({ - id: item.id, - data: item, - }); - context.store.set({ - id: item.id, - data: parsed, - }); - } - }, - schema: z.object({ - title: z.string(), - description: z.string(), - season: reference('seasons'), - episode: z.string(), - watchUrl: z.string().url(), - references: z.array(z.object({ - timeRange: z.string().optional(), - notes: z.string(), - passage: z.string(), - type: z.string(), - })) - }), -} \ No newline at end of file diff --git a/src/pages/[season]/[episode].astro b/src/pages/[season]/[episode].astro index 871d8c3..f7c613b 100644 --- a/src/pages/[season]/[episode].astro +++ b/src/pages/[season]/[episode].astro @@ -20,8 +20,7 @@ export const getStaticPaths = (async () => { params: { episode: entryWithNextAndPrev.item.data.episode, //cast is necessary because of a bug in Astro - season: (entryWithNextAndPrev.item.data.season as { id: string }) - .id, + season: entryWithNextAndPrev.item.data.season.id, }, props: { entryWithNextAndPrev }, })); @@ -37,14 +36,12 @@ const { item: entry, next, prev } = entryWithNextAndPrev; const prevData = prev && { label: prev.data.title, - //cast is necessary because of a bug in Astro - url: `/${(prev.data.season as { id: string }).id}/${prev.data.episode}`, + url: `/${prev.data.season.id}/${prev.data.episode}`, }; const nextData = next && { label: next.data.title, - //cast is necessary because of a bug in Astro - url: `/${(next.data.season as { id: string }).id}/${next.data.episode}`, + url: `/${next.data.season.id}/${next.data.episode}`, }; type Reference = CollectionEntry<"episodes">["data"]["references"][number]; @@ -56,14 +53,9 @@ type Reference = CollectionEntry<"episodes">["data"]["references"][number];