Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(slugs): allow filters in template strings #6690

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
14 changes: 13 additions & 1 deletion packages/netlify-cms-core/src/lib/__tests__/formatters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe('formatters', () => {
};

describe('slugFormatter', () => {
const date = new Date('2020-01-01');
const date = new Date('2020-01-01T13:28:27.679Z');
jest.spyOn(global, 'Date').mockImplementation(() => date);

const { selectIdentifier } = require('../../reducers/collections');
Expand Down Expand Up @@ -312,6 +312,18 @@ describe('formatters', () => {
).toBe('entry-slug');
});

it('should allow filters in slug templates', () => {
selectIdentifier.mockReturnValueOnce('published');

expect(
slugFormatter(
Map({ slug: "{{published | date('MM-DD')}}" }),
Map({ title: 'Post Title', published: date }),
slugConfig,
),
).toBe('01-01');
});

it('should return slug', () => {
selectIdentifier.mockReturnValueOnce('title');

Expand Down
12 changes: 6 additions & 6 deletions packages/netlify-cms-lib-widgets/src/stringTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ export function compileStringTemplate(
replacement = data.getIn(keyToPathArray(key), '') as string;
}

const filterFunction = getFilterFunction(filter);
if (filterFunction) {
replacement = filterFunction(replacement);
}

if (processor) {
return processor(replacement);
} else {
const filterFunction = getFilterFunction(filter);
if (filterFunction) {
replacement = filterFunction(replacement);
}
replacement = processor(replacement);
Comment on lines +193 to +199
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the the early return if the processor arg is passed in, check for filters first and apply them.

Moved the processor call after filters since it looks like that is used to make the slugs safer for URL/filenames.

}

return replacement;
Expand Down