From 168f3de2430981355890580c5cbaec28b70b1e8a Mon Sep 17 00:00:00 2001 From: Dave Date: Wed, 12 Nov 2025 14:47:52 +0100 Subject: [PATCH] Added Taxonomy Collections & Readers --- keystatic.config.ts | 6 ++++- .../keystatic}/[...params]/route.ts | 2 +- src/keystatic/collections/taxonomy/authors.ts | 23 +++++++++++++++++++ src/keystatic/collections/taxonomy/index.ts | 9 ++++++++ src/keystatic/collections/taxonomy/tags.ts | 21 +++++++++++++++++ src/lib/readers/base.ts | 7 ++++++ src/lib/readers/taxonomy/authors.ts | 8 +++++++ src/lib/readers/taxonomy/tags.ts | 8 +++++++ 8 files changed, 82 insertions(+), 2 deletions(-) rename src/app/(cms)/{keystatic/api => api/keystatic}/[...params]/route.ts (74%) create mode 100644 src/keystatic/collections/taxonomy/authors.ts create mode 100644 src/keystatic/collections/taxonomy/index.ts create mode 100644 src/keystatic/collections/taxonomy/tags.ts create mode 100644 src/lib/readers/base.ts create mode 100644 src/lib/readers/taxonomy/authors.ts create mode 100644 src/lib/readers/taxonomy/tags.ts diff --git a/keystatic.config.ts b/keystatic.config.ts index aa6a5db..644d236 100644 --- a/keystatic.config.ts +++ b/keystatic.config.ts @@ -1,8 +1,12 @@ import { config } from '@keystatic/core'; +import taxonomyCollections from '@/keystatic/collections/taxonomy'; + export default config({ storage: { kind: 'local', }, - collections: {}, + collections: { + ...taxonomyCollections, + }, }); diff --git a/src/app/(cms)/keystatic/api/[...params]/route.ts b/src/app/(cms)/api/keystatic/[...params]/route.ts similarity index 74% rename from src/app/(cms)/keystatic/api/[...params]/route.ts rename to src/app/(cms)/api/keystatic/[...params]/route.ts index 37e7b2e..92d8871 100644 --- a/src/app/(cms)/keystatic/api/[...params]/route.ts +++ b/src/app/(cms)/api/keystatic/[...params]/route.ts @@ -1,5 +1,5 @@ import { makeRouteHandler } from '@keystatic/next/route-handler'; -import { config } from '~/keystatic.config'; +import config from '~/keystatic.config'; export const { POST, GET } = makeRouteHandler({ config, diff --git a/src/keystatic/collections/taxonomy/authors.ts b/src/keystatic/collections/taxonomy/authors.ts new file mode 100644 index 0000000..2f1a9ee --- /dev/null +++ b/src/keystatic/collections/taxonomy/authors.ts @@ -0,0 +1,23 @@ +import { collection, fields } from '@keystatic/core'; + +export default collection({ + label: 'Authors', + slugField: 'name', + path: 'content/taxonomy/authors/*', + schema: { + name: fields.slug({ + name: { + label: 'Name', + }, + }), + avatar: fields.image({ + label: 'Avatar', + directory: 'public/images/authors', + publicPath: '/images/authors', + }), + description: fields.text({ + label: 'Description', + multiline: true, + }), + }, +}); diff --git a/src/keystatic/collections/taxonomy/index.ts b/src/keystatic/collections/taxonomy/index.ts new file mode 100644 index 0000000..07ac356 --- /dev/null +++ b/src/keystatic/collections/taxonomy/index.ts @@ -0,0 +1,9 @@ +import tags from '@/keystatic/collections/taxonomy/tags'; +import authors from '@/keystatic/collections/taxonomy/authors'; + +const taxonomyCollection = { + tags: tags, + authors: authors, +}; + +export default taxonomyCollection; diff --git a/src/keystatic/collections/taxonomy/tags.ts b/src/keystatic/collections/taxonomy/tags.ts new file mode 100644 index 0000000..46b9b04 --- /dev/null +++ b/src/keystatic/collections/taxonomy/tags.ts @@ -0,0 +1,21 @@ +import { collection, fields } from '@keystatic/core'; + +export default collection({ + label: 'Tags', + slugField: 'name', + path: 'content/taxonomy/tags/*', + format: { + data: 'json', + }, + schema: { + name: fields.slug({ + name: { + label: 'Name', + }, + }), + description: fields.text({ + label: 'Description', + multiline: true, + }), + }, +}); diff --git a/src/lib/readers/base.ts b/src/lib/readers/base.ts new file mode 100644 index 0000000..cd67996 --- /dev/null +++ b/src/lib/readers/base.ts @@ -0,0 +1,7 @@ +import { createReader } from '@keystatic/core/reader'; +import { cache } from 'react'; +import keystaticConfig from '~/keystatic.config'; + +export const reader = createReader(process.cwd(), keystaticConfig); + +export { cache }; diff --git a/src/lib/readers/taxonomy/authors.ts b/src/lib/readers/taxonomy/authors.ts new file mode 100644 index 0000000..2f26a3e --- /dev/null +++ b/src/lib/readers/taxonomy/authors.ts @@ -0,0 +1,8 @@ +import { cache, reader } from '@/lib/readers/base'; + +export const getAuthors = cache(async () => reader.collections.authors.all()); + +export const getAuthorBySlug = cache(async (slug: string) => { + const author = await reader.collections.authors.read(slug); + return author ? { ...author, slug } : null; +}); diff --git a/src/lib/readers/taxonomy/tags.ts b/src/lib/readers/taxonomy/tags.ts new file mode 100644 index 0000000..0455683 --- /dev/null +++ b/src/lib/readers/taxonomy/tags.ts @@ -0,0 +1,8 @@ +import { cache, reader } from '@/lib/readers/base'; + +export const getTags = cache(async () => reader.collections.tags.all()); + +export const getTagBySlug = cache(async (slug: string) => { + const tag = await reader.collections.tags.read(slug); + return tag ? { ...tag, slug } : null; +});