Added Taxonomy Collections & Readers

This commit is contained in:
2025-11-12 14:47:52 +01:00
parent 9cecb48d34
commit 168f3de243
8 changed files with 82 additions and 2 deletions

View File

@@ -1,8 +1,12 @@
import { config } from '@keystatic/core';
import taxonomyCollections from '@/keystatic/collections/taxonomy';
export default config({
storage: {
kind: 'local',
},
collections: {},
collections: {
...taxonomyCollections,
},
});

View File

@@ -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,

View File

@@ -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,
}),
},
});

View File

@@ -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;

View File

@@ -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,
}),
},
});

7
src/lib/readers/base.ts Normal file
View File

@@ -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 };

View File

@@ -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;
});

View File

@@ -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;
});