Update default path and string manipulation in keystatic.config.ts

A default path for createKeyStaticConfig function is added and conditional code block entered to append '/' to the path, if it doesn't end with it. This prevents potential path issues in future codes. Also, the default value of NEXT_PUBLIC_KEYSTATIC_CONTENT_PATH is changed to an empty string from 'content'.
This commit is contained in:
giancarlo
2024-05-07 19:51:05 +07:00
parent ce993ad120
commit 951acd38a5

View File

@@ -40,7 +40,7 @@ const storage = z.union([local, cloud, github]).parse({
}); });
const keyStaticConfig = createKeyStaticConfig( const keyStaticConfig = createKeyStaticConfig(
process.env.NEXT_PUBLIC_KEYSTATIC_CONTENT_PATH ?? 'content', process.env.NEXT_PUBLIC_KEYSTATIC_CONTENT_PATH ?? '',
); );
export default keyStaticConfig; export default keyStaticConfig;
@@ -77,14 +77,18 @@ export type PostEntryProps = Entry<
(typeof keyStaticConfig)['collections']['posts'] (typeof keyStaticConfig)['collections']['posts']
>; >;
function createKeyStaticConfig(path: string) { function createKeyStaticConfig(path = '') {
if (path && !path.endsWith('/')) {
path += '/';
}
return config({ return config({
storage, storage,
collections: { collections: {
posts: collection({ posts: collection({
label: 'Posts', label: 'Posts',
slugField: 'title', slugField: 'title',
path: `${path}/posts/*`, path: `${path}posts/*`,
format: { contentField: 'content' }, format: { contentField: 'content' },
schema: { schema: {
title: fields.slug({ name: { label: 'Title' } }), title: fields.slug({ name: { label: 'Title' } }),
@@ -109,7 +113,7 @@ function createKeyStaticConfig(path: string) {
documentation: collection({ documentation: collection({
label: 'Documentation', label: 'Documentation',
slugField: 'title', slugField: 'title',
path: `${path}/documentation/**`, path: `${path}documentation/**`,
format: { contentField: 'content' }, format: { contentField: 'content' },
schema: { schema: {
title: fields.slug({ name: { label: 'Title' } }), title: fields.slug({ name: { label: 'Title' } }),