Skip to content
Merged
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/flat-zoos-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blinkk/root-cms': patch
---

feat: add slugRegex config option for collections (#538)
1 change: 1 addition & 0 deletions packages/root-cms/core/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ function serializeCollection(collection: Collection): Partial<Collection> {
url: collection.url,
previewUrl: collection.previewUrl,
preview: collection.preview,
slugRegex: collection.slugRegex,
};
}

Expand Down
5 changes: 5 additions & 0 deletions packages/root-cms/core/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ export type Collection = Schema & {
src: string;
};
};
/**
* Regular expression used to validate document slugs. Should be provided as a
* string so it can be serialized to the CMS UI.
*/
slugRegex?: string;
};

export function defineCollection(
Expand Down
1 change: 1 addition & 0 deletions packages/root-cms/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"**/*.ts",
"*.tsx",
"**/*.tsx",
"../shared/*.ts"
]
}
40 changes: 40 additions & 0 deletions packages/root-cms/shared/slug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {describe, it, expect} from 'vitest';
import {isSlugValid, normalizeSlug} from './slug.js';

describe('isSlugValid', () => {
it('validates good slugs', () => {
expect(isSlugValid('1')).toBe(true);
expect(isSlugValid('a')).toBe(true);
expect(isSlugValid('foo')).toBe(true);
expect(isSlugValid('foo-bar')).toBe(true);
expect(isSlugValid('foo--bar')).toBe(true);
expect(isSlugValid('foo-bar-123')).toBe(true);
expect(isSlugValid('foo--bar--123')).toBe(true);
expect(isSlugValid('foo_bar')).toBe(true);
expect(isSlugValid('foo_bar-123')).toBe(true);
expect(isSlugValid('_foo_bar-123')).toBe(true);
});

it('invalidates bad slugs', () => {
expect(isSlugValid('Foo')).toBe(false);
expect(isSlugValid('-asdf-')).toBe(false);
expect(isSlugValid('-a!!')).toBe(false);
expect(isSlugValid('!!a')).toBe(false);
expect(isSlugValid('/foo')).toBe(false);
expect(isSlugValid('--foo--bar')).toBe(false);
});
});

describe('normalizeSlug', () => {
it('converts / to --', () => {
expect(normalizeSlug('foo')).toEqual('foo');
expect(normalizeSlug('foo/bar')).toEqual('foo--bar');
expect(normalizeSlug('foo/bar/baz')).toEqual('foo--bar--baz');
});

it('removes whitespace', () => {
expect(normalizeSlug(' foo ')).toEqual('foo');
expect(normalizeSlug(' foo/bar ')).toEqual('foo--bar');
expect(normalizeSlug(' foo/bar/baz ')).toEqual('foo--bar--baz');
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export function isSlugValid(slug: string): boolean {
return Boolean(slug && slug.match(/^[a-z0-9]+(?:--?[a-z0-9]+)*$/));
const DEFAULT_SLUG_PATTERN = /^[a-z0-9_]+(?:--?[a-z0-9_]+)*$/;

export function isSlugValid(slug: string, pattern?: string | RegExp): boolean {
if (!pattern) {
pattern = DEFAULT_SLUG_PATTERN;
}
const re = typeof pattern === 'string' ? new RegExp(pattern) : pattern;
return Boolean(slug && re.test(slug));
}

/**
Expand All @@ -12,14 +18,12 @@ export function isSlugValid(slug: string): boolean {
* Transformations include:
* Remove leading and trailing space
* Remove leading and trailing slash
* Lower case
* Replace '/' with '--', e.g. 'foo/bar' -> 'foo--bar'
*/
export function normalizeSlug(slug: string): string {
return slug
.replace(/^[\s/]*/g, '')
.replace(/[\s/]*$/g, '')
.replace(/^\/+|\/+$/g, '')
.toLowerCase()
.replaceAll('/', '--');
}
5 changes: 3 additions & 2 deletions packages/root-cms/ui/components/CopyDocModal/CopyDocModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {ContextModalProps, useModals} from '@mantine/modals';
import {showNotification} from '@mantine/notifications';
import {useState} from 'preact/hooks';
import {route} from 'preact-router';
import {isSlugValid, normalizeSlug} from '../../../shared/slug.js';
import {useModalTheme} from '../../hooks/useModalTheme.js';
import {cmsCopyDoc} from '../../utils/doc.js';
import {isSlugValid, normalizeSlug} from '../../utils/slug.js';
import {SlugInput} from '../SlugInput/SlugInput.js';
import {Text} from '../Text/Text.js';
import './CopyDocModal.css';
Expand Down Expand Up @@ -52,7 +52,8 @@ export function CopyDocModal(modalProps: ContextModalProps<CopyDocModalProps>) {
return;
}
const cleanSlug = normalizeSlug(toSlug);
if (!isSlugValid(cleanSlug)) {
const slugRegex = window.__ROOT_CTX.collections[toCollectionId]?.slugRegex;
if (!isSlugValid(cleanSlug, slugRegex)) {
setError('Please enter a valid slug (e.g. "foo-bar-123").');
setLoading(false);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import {showNotification} from '@mantine/notifications';
import {useEffect, useRef, useState} from 'preact/hooks';
import {route} from 'preact-router';
import {isSlugValid} from '../../../shared/slug.js';
import {useGapiClient} from '../../hooks/useGapiClient.js';
import {
DataSource,
Expand All @@ -20,7 +21,6 @@ import {
} from '../../utils/data-source.js';
import {parseSpreadsheetUrl} from '../../utils/gsheets.js';
import {notifyErrors} from '../../utils/notifications.js';
import {isSlugValid} from '../../utils/slug.js';
import './DataSourceForm.css';

const HTTP_URL_HELP = 'Enter the URL to make the HTTP request.';
Expand Down
33 changes: 4 additions & 29 deletions packages/root-cms/ui/components/NewDocModal/NewDocModal.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
import {Button, Modal, useMantineTheme} from '@mantine/core';
import {useState} from 'preact/hooks';
import {route} from 'preact-router';
import {isSlugValid, normalizeSlug} from '../../../shared/slug.js';
import {useCollectionSchema} from '../../hooks/useCollectionSchema.js';
import {cmsCreateDoc} from '../../utils/doc.js';
import {getDefaultFieldValue} from '../../utils/fields.js';
import {SlugInput} from '../SlugInput/SlugInput.js';
import './NewDocModal.css';
import {logAction} from '../../utils/actions.js';
import {useCollectionSchema} from '../../hooks/useCollectionSchema.js';

interface NewDocModalProps {
collection: string;
opened?: boolean;
onClose?: () => void;
}

function isSlugValid(slug: string): boolean {
return Boolean(slug && slug.match(/^[a-z0-9]+(?:--?[a-z0-9]+)*$/));
}

/**
* Normalizes a user-entered slug value into one appropriate for the CMS.
*
* In order to keep the slugs "flat" within firestore, nested paths use a double
* dash separator. For example, a URL like "/about/foo" should have a slug like
* "about--foo".
*
* Transformations include:
* Remove leading and trailing space
* Remove leading and trailing slash
* Lower case
* Replace '/' with '--', e.g. 'foo/bar' -> 'foo--bar'
*/
function normalizeSlug(slug: string): string {
return slug
.replace(/^[\s/]*/g, '')
.replace(/[\s/]*$/g, '')
.replace(/^\/+|\/+$/g, '')
.toLowerCase()
.replaceAll('/', '--');
}

export function NewDocModal(props: NewDocModalProps) {
const [slug, setSlug] = useState('');
const [rpcLoading, setRpcLoading] = useState(false);
Expand All @@ -64,7 +38,8 @@ export function NewDocModal(props: NewDocModalProps) {
setSlugError('');

const cleanSlug = normalizeSlug(slug);
if (!isSlugValid(cleanSlug)) {
const slugRegex = rootCollection.slugRegex;
if (!isSlugValid(cleanSlug, slugRegex)) {
setSlugError('Please enter a valid slug (e.g. "foo-bar-123").');
setRpcLoading(false);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {showNotification} from '@mantine/notifications';
import {IconArrowUpRight, IconTrash} from '@tabler/icons-preact';
import {useEffect, useRef, useState} from 'preact/hooks';
import {route} from 'preact-router';
import {isSlugValid} from '../../../shared/slug.js';
import {notifyErrors} from '../../utils/notifications.js';
import {
Release,
addRelease,
getRelease,
updateRelease,
} from '../../utils/release.js';
import {isSlugValid} from '../../utils/slug.js';
import {DocPreviewCard} from '../DocPreviewCard/DocPreviewCard.js';
import {useDocSelectModal} from '../DocSelectModal/DocSelectModal.js';
import './ReleaseForm.css';
Expand Down
9 changes: 5 additions & 4 deletions packages/root-cms/ui/components/SlugInput/SlugInput.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Select, TextInput} from '@mantine/core';
import {ChangeEvent} from 'preact/compat';
import {useRef, useState} from 'preact/hooks';
import {isSlugValid, normalizeSlug} from '../../../shared/slug.js';
import {Text} from '../../components/Text/Text.js';
import {joinClassNames} from '../../utils/classes.js';
import {getDocServingUrl} from '../../utils/doc-urls.js';
import {isSlugValid, normalizeSlug} from '../../utils/slug.js';
import './SlugInput.css';

export interface SlugInputProps {
Expand Down Expand Up @@ -39,17 +39,18 @@ export function SlugInput(props: SlugInputProps) {
if (rootCollection?.url) {
if (slug) {
const cleanSlug = normalizeSlug(slug);
if (isSlugValid(cleanSlug)) {
const slugRegex = rootCollection?.slugRegex;
if (isSlugValid(cleanSlug, slugRegex)) {
urlHelp = getDocServingUrl({
collectionId: props.collectionId!,
collectionId: collectionId,
slug: cleanSlug,
});
} else {
urlHelp = 'INVALID SLUG';
}
} else {
urlHelp = getDocServingUrl({
collectionId: props.collectionId!,
collectionId: collectionId,
slug: '[slug]',
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/root-cms/ui/components/Viewers/Viewers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
updateDoc,
} from 'firebase/firestore';
import {useEffect, useState} from 'preact/hooks';
import {normalizeSlug} from '../../../shared/slug.js';
import {joinClassNames} from '../../utils/classes.js';
import {EventListener} from '../../utils/events.js';
import {normalizeSlug} from '../../utils/slug.js';
import {throttle} from '../../utils/throttle.js';
import {TIME_UNITS} from '../../utils/time.js';
import {Timer} from '../../utils/timer.js';
Expand Down
1 change: 1 addition & 0 deletions packages/root-cms/ui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
"**/*.ts",
"*.tsx",
"**/*.tsx",
"../shared/*.ts",
]
}