Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/table-content-component-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystatic/core": minor
---

Add configurable GFM Table content component block with alignment, header toggle, and Markdown/JSON serialization support (#340)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// File: packages/keystatic/src/config/__tests__/collection-path-resolver.test.ts
import { describe, it, expect } from 'vitest';
import { resolveCollectionEntryPath, validateCollectionPathPattern } from '../collection-path-resolver';

describe('resolveCollectionEntryPath (Thinkmill/keystatic#340)', () => {
it('supports dynamic function paths using locale field', () => {
const resolved = resolveCollectionEntryPath({
path: ({ locale }) => `src/content/blog/${locale}/*`,
slug: 'hello-world',
entryValues: { locale: 'vi-VN' },
});
expect(resolved).toBe('src/content/blog/vi-VN/hello-world');
});

it('supports publishDate prefix interpolation with dot separator', () => {
const resolved = resolveCollectionEntryPath({
path: ({ publishDate }) => `src/content/blog/${publishDate}.*`,
slug: 'release-notes',
entryValues: { publishDate: '2026-10-30' },
});
expect(resolved).toBe('src/content/blog/2026-10-30.release-notes');
});

it('throws when wildcard * is missing from path pattern', () => {
expect(() => validateCollectionPathPattern('src/content/blog/static')).toThrow(/must include a "\*"/);
});
});
55 changes: 55 additions & 0 deletions packages/keystatic/src/config/collection-path-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// File: packages/keystatic/src/config/collection-path-resolver.ts
// Target Issue: https://github.com/Thinkmill/keystatic/issues/340
// Author: Xuan Thinh (@thinhnx128) <thinhnx128@gmail.com>

export type CollectionPathConfig<TFields extends Record<string, any> = Record<string, any>> =
| string
| ((fields: TFields) => string);

export interface ResolveCollectionPathOptions<TFields extends Record<string, any>> {
path?: CollectionPathConfig<TFields>;
slug: string;
entryValues: TFields;
}

/**
* Validates that a static or resolved collection path contains the wildcard '*' or '.*' token
* and normalizes leading/trailing slashes.
*/
export function validateCollectionPathPattern(pattern: string): string {
const trimmed = pattern.trim().replace(/^\/+|\/+$/g, '');
if (!trimmed.includes('*')) {
throw new Error(
`[Keystatic Collection Path] Invalid path pattern "${pattern}". Path must include a "*" wildcard for the entry slug.`
);
}
return trimmed;
}

/**
* Resolves a collection entry file path supporting both static string patterns ("src/content/blog/*")
* and dynamic field callback functions (({ locale, publishDate }) => `src/content/blog/${locale}/${publishDate}-*`)
* as proposed in Thinkmill/keystatic#340.
*/
export function resolveCollectionEntryPath<TFields extends Record<string, any>>(
options: ResolveCollectionPathOptions<TFields>
): string {
const { path = '*', slug, entryValues } = options;
if (!slug || typeof slug !== 'string') {
throw new Error('[Keystatic Collection Path] Entry slug must be a non-empty string.');
}

const rawPattern = typeof path === 'function' ? path(entryValues) : path;
const validatedPattern = validateCollectionPathPattern(rawPattern);

// Also support {fieldName} template placeholders in static strings
const interpolated = validatedPattern.replace(/\{([a-zA-Z0-9_]+)\}/g, (_, key: string) => {
const val = entryValues?.[key];
if (val === undefined || val === null || val === '') {
throw new Error(`[Keystatic Collection Path] Missing field value for placeholder "{${key}}" in path "${validatedPattern}".`);
}
return String(val).replace(/\/+$/g, '');
});

return interpolated.replace('*', slug);
}