Skip to content

Commit

Permalink
feat(slugs): allow filters in template strings
Browse files Browse the repository at this point in the history
Process template filters first, then strip `'` and `.` characters from
slug strings.

Fixes #4783
  • Loading branch information
stormwarning committed Mar 8, 2023
1 parent ffbfce1 commit e21d3b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
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);
}

return replacement;
Expand Down

0 comments on commit e21d3b9

Please sign in to comment.