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
20 changes: 16 additions & 4 deletions packages/decap-cms-core/src/lib/__tests__/formatters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ describe('formatters', () => {
};

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

const { selectIdentifier } = require('../../reducers/collections');

Expand Down Expand Up @@ -333,10 +333,22 @@ 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: new Date(date) }),
slugConfig,
),
).toBe('01-01');
});

it('should see date filters applied to date from entry if it exists', () => {
const { selectInferredField } = require('../../reducers/collections');
selectInferredField.mockReturnValue('date');
const entryDate = new Date('2026-10-20');
const entryDate = new Date('2026-10-20T13:28:27.679Z');

expect(
slugFormatter(
Expand All @@ -350,7 +362,7 @@ describe('formatters', () => {
it('should see date filters applied to publishDate from entry if it exists', () => {
const { selectInferredField } = require('../../reducers/collections');
selectInferredField.mockReturnValue('publishDate');
const entryDate = new Date('2026-10-20');
const entryDate = new Date('2026-10-20T13:28:27.679Z');

expect(
slugFormatter(
Expand Down
12 changes: 6 additions & 6 deletions packages/decap-cms-lib-widgets/src/stringTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,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 +206 to +212
Copy link
Copy Markdown
Contributor 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
Loading