From 65d4339b9f906fa73225e69b73ba6e3ef235c8f2 Mon Sep 17 00:00:00 2001 From: Firq-ow Date: Tue, 9 Jan 2024 14:30:28 +0100 Subject: [PATCH] Fixed content collections --- src/components/Card.astro | 65 ------------------- src/components/questListing.astro | 5 +- src/content/config.ts | 28 ++++++++ src/content/{data => taInfoData}/albion.json | 0 src/content/{data => taInfoData}/beast4l.json | 0 .../{data => taInfoData}/cernunnos.json | 0 .../devilish-bodhisattva.json | 0 src/content/{data => taInfoData}/morgan.json | 0 src/layouts/taShowcaseLayout.astro | 22 +++---- src/pages/database/[slug].astro | 13 ++-- src/pages/database/index.astro | 26 +++----- src/utils/tools.ts | 19 ++++-- 12 files changed, 67 insertions(+), 111 deletions(-) delete mode 100644 src/components/Card.astro create mode 100644 src/content/config.ts rename src/content/{data => taInfoData}/albion.json (100%) rename src/content/{data => taInfoData}/beast4l.json (100%) rename src/content/{data => taInfoData}/cernunnos.json (100%) rename src/content/{data => taInfoData}/devilish-bodhisattva.json (100%) rename src/content/{data => taInfoData}/morgan.json (100%) diff --git a/src/components/Card.astro b/src/components/Card.astro deleted file mode 100644 index 251d6e8..0000000 --- a/src/components/Card.astro +++ /dev/null @@ -1,65 +0,0 @@ ---- -interface Props { - title: string - body: string - href: string -} - -const { href, title, body } = Astro.props ---- - - - diff --git a/src/components/questListing.astro b/src/components/questListing.astro index 1de04b1..64ff216 100644 --- a/src/components/questListing.astro +++ b/src/components/questListing.astro @@ -1,6 +1,6 @@ --- export interface Props { - url: string + slug: string title: string questReleaseDate: string shortdescription: string @@ -11,7 +11,8 @@ const options_date: Intl.DateTimeFormatOptions = { month: 'long', day: '2-digit', } -const { shortdescription, questReleaseDate, url, title } = Astro.props +const { shortdescription, questReleaseDate, slug, title } = Astro.props +const url = `/database/${slug}` const render_date = new Date(questReleaseDate).toLocaleDateString( 'en-GB', options_date diff --git a/src/content/config.ts b/src/content/config.ts new file mode 100644 index 0000000..7d8cf2d --- /dev/null +++ b/src/content/config.ts @@ -0,0 +1,28 @@ +import { z, defineCollection } from 'astro:content' + +const taInfo = defineCollection({ + type: 'data', + schema: z.object({ + info: z.object({ + title: z.string(), + questReleaseDate: z.string(), + shortdescription: z.string(), + description: z.string(), + fightNumber: z.number().default(1), + }), + data: z.array( + z.object({ + title: z.string(), + link: z.string().url(), + date: z.string(), + servant: z.string(), + turns: z.string(), + runner: z.string(), + }) + ), + }), +}) + +export const collections = { + taInfoData: taInfo, +} diff --git a/src/content/data/albion.json b/src/content/taInfoData/albion.json similarity index 100% rename from src/content/data/albion.json rename to src/content/taInfoData/albion.json diff --git a/src/content/data/beast4l.json b/src/content/taInfoData/beast4l.json similarity index 100% rename from src/content/data/beast4l.json rename to src/content/taInfoData/beast4l.json diff --git a/src/content/data/cernunnos.json b/src/content/taInfoData/cernunnos.json similarity index 100% rename from src/content/data/cernunnos.json rename to src/content/taInfoData/cernunnos.json diff --git a/src/content/data/devilish-bodhisattva.json b/src/content/taInfoData/devilish-bodhisattva.json similarity index 100% rename from src/content/data/devilish-bodhisattva.json rename to src/content/taInfoData/devilish-bodhisattva.json diff --git a/src/content/data/morgan.json b/src/content/taInfoData/morgan.json similarity index 100% rename from src/content/data/morgan.json rename to src/content/taInfoData/morgan.json diff --git a/src/layouts/taShowcaseLayout.astro b/src/layouts/taShowcaseLayout.astro index d213cdd..9b97566 100644 --- a/src/layouts/taShowcaseLayout.astro +++ b/src/layouts/taShowcaseLayout.astro @@ -2,31 +2,29 @@ import Layout from '../layouts/Layout.astro' import BaseSection from '../layouts/baseSection.astro' import TACard from '../components/taCard.astro' -import type { FileData } from '../types/ta' -import type { GlobAny } from '../types/generic' +import { plsLoadTAEntry } from '../utils/tools' export interface Props { datafile: string } const { datafile } = Astro.props -const fulldata = import.meta.glob(`/src/content/data/*.json`) -const filecontent: FileData = ( - await fulldata[`/src/content/data/${datafile}.json`]() -)['default'] - -const pagetitle = `${filecontent.info.title} - FGO TA` -filecontent.data.sort((a, b) => Date.parse(b.date) - Date.parse(a.date)) +const taEntry = await plsLoadTAEntry(datafile) +const pagetitle = `${taEntry.info.title} - FGO TA` +taEntry.data.sort((a, b) => Date.parse(b.date) - Date.parse(a.date)) --- << Back to database - - {filecontent.data.map((item) => )} + + {taEntry.data.map((item) => )}
diff --git a/src/pages/database/[slug].astro b/src/pages/database/[slug].astro index 1d7b432..990e982 100644 --- a/src/pages/database/[slug].astro +++ b/src/pages/database/[slug].astro @@ -1,15 +1,10 @@ --- +import { getCollection } from 'astro:content'; import TaShowcaseLayout from '../../layouts/taShowcaseLayout.astro' -import type { GlobAny } from '../../types/generic' -import { findSlug } from '../../utils/tools' -export function getStaticPaths() { - const fulldata = import.meta.glob( - `/src/content/data/*.json` - ) - const keylist = Object.keys(fulldata).map((item) => findSlug(item)!) - - return keylist.map((slug) => ({ params: { slug } })) +export async function getStaticPaths() { + const fulldata = (await getCollection('taInfoData')).map((data) => data.id) + return fulldata.map((slug) => ({ params: { slug } })) } const { slug } = Astro.params diff --git a/src/pages/database/index.astro b/src/pages/database/index.astro index e27c5d0..503d19f 100644 --- a/src/pages/database/index.astro +++ b/src/pages/database/index.astro @@ -1,28 +1,20 @@ --- +import { getCollection } from 'astro:content'; import Layout from '../../layouts/Layout.astro' import QuestListing from '../../components/questListing.astro' import DatabaseSection from '../../layouts/databaseSection.astro' -import { findSlug } from '../../utils/tools' -import type { GlobFiledata } from '../../types/generic' const description = 'FGO NA TA Database' +const fulldata = await getCollection('taInfoData') -const questInfo = [] -const fulldata = import.meta.glob( - `/src/content/data/*.json` +fulldata.sort( + (a, b) => Date.parse(b.data.info.questReleaseDate) - Date.parse(a.data.info.questReleaseDate) || b.data.info.fightNumber - a.data.info.fightNumber ) +const infodata = fulldata.map((quest) => ({ + ...quest.data.info, + slug: quest.id +})) -for (const [key, value] of Object.entries(fulldata)) { - const url = `/database/${findSlug(key)}` - questInfo.push({ - ...(await value())['default'].info, - url: url, - }) -} - -questInfo.sort( - (a, b) => Date.parse(b.questReleaseDate) - Date.parse(a.questReleaseDate) || b.fightNumber - a.fightNumber -) --- - {questInfo.map((quest) => )} + {infodata.map((quest) => )} diff --git a/src/utils/tools.ts b/src/utils/tools.ts index 5a77d01..891468a 100644 --- a/src/utils/tools.ts +++ b/src/utils/tools.ts @@ -1,9 +1,16 @@ -export function findSlug(filepath: string) { - return filepath.match(/(?:.*[\\/])(.+)(?:\.json)/)?.[1] +import { getEntry } from 'astro:content' + +export function plsLoadImage( + record: Record Promise<{ default: ImageMetadata }>>, + path: string +) { + const loadedImage = record[path]?.() + if (!loadedImage) throw new Error('Asset was not found:' + path) + return loadedImage } -export function plsLoadImage(record: Record Promise<{default: ImageMetadata}>>, path: string) { - const loadedImage = record[path]?.(); - if (!loadedImage) throw new Error("Asset was not found:" + path); - return loadedImage; +export async function plsLoadTAEntry(key: string) { + const filecontent = (await getEntry('taInfoData', key))?.data + if (!filecontent) throw new Error(`Datafile ${key} is missing!`) + return filecontent }