From 12444ca761d5cd40d766847f117e55b0e0ca6e9e Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Wed, 4 Mar 2020 11:08:15 +0100 Subject: [PATCH] feat: support filename and extension vars in summary (#3375) --- .../src/lib/__tests__/formatters.spec.js | 13 +++++++++++++ packages/netlify-cms-core/src/lib/formatters.ts | 4 +++- website/content/docs/configuration-options.md | 11 +++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/netlify-cms-core/src/lib/__tests__/formatters.spec.js b/packages/netlify-cms-core/src/lib/__tests__/formatters.spec.js index 85cecce4..42a54644 100644 --- a/packages/netlify-cms-core/src/lib/__tests__/formatters.spec.js +++ b/packages/netlify-cms-core/src/lib/__tests__/formatters.spec.js @@ -358,6 +358,19 @@ describe('formatters', () => { expect(summaryFormatter('{{title}}-{{year}}', entry, collection)).toBe('title-2020'); }); + + it('should handle filename and extension variables', () => { + const { selectInferedField } = require('../../reducers/collections'); + selectInferedField.mockReturnValue('date'); + + const date = new Date('2020-01-02T13:28:27.679Z'); + const entry = fromJS({ path: 'post.md', data: { date, title: 'title' } }); + const collection = fromJS({ fields: [{ name: 'date', widget: 'date' }] }); + + expect( + summaryFormatter('{{title}}-{{year}}-{{filename}}.{{extension}}', entry, collection), + ).toBe('title-2020-post.md'); + }); }); describe('folderFormatter', () => { diff --git a/packages/netlify-cms-core/src/lib/formatters.ts b/packages/netlify-cms-core/src/lib/formatters.ts index ccbac198..d30f5c80 100644 --- a/packages/netlify-cms-core/src/lib/formatters.ts +++ b/packages/netlify-cms-core/src/lib/formatters.ts @@ -200,9 +200,11 @@ export const summaryFormatter = ( entry: EntryMap, collection: Collection, ) => { - const entryData = entry.get('data'); + let entryData = entry.get('data'); const date = parseDateFromEntry(entry, collection) || null; const identifier = entryData.getIn(keyToPathArray(selectIdentifier(collection) as string)); + + entryData = addFileTemplateFields(entry.get('path'), entryData); const summary = compileStringTemplate(summaryTemplate, date, identifier, entryData); return summary; }; diff --git a/website/content/docs/configuration-options.md b/website/content/docs/configuration-options.md index e0b8726c..6cce3270 100644 --- a/website/content/docs/configuration-options.md +++ b/website/content/docs/configuration-options.md @@ -284,7 +284,7 @@ A string representing the path where content in this collection can be found on Template tags are the same as those for [slug](#slug), with the following exceptions: -* `{{slug}}` is the entire slug for the current entry (not just the url-safe identifier, as is the case with [`slug` configuration](#slug) +* `{{slug}}` is the entire slug for the current entry (not just the url-safe identifier, as is the case with [`slug` configuration](#slug)) * The date based template tags, such as `{{year}}` and `{{month}}`, are pulled from a date field in your entry, and may require additional configuration - see [`preview_path_date_field`](#preview_path_date_field) for details. If a date template tag is used and no date can be found, `preview_path` will be ignored. * `{{filename}}` The file name without the extension part. * `{{extension}}` The file extension. @@ -358,7 +358,14 @@ This setting changes options for the editor view of the collection. It has one o ### `summary` -This setting allows the customisation of the collection list view. Similar to the `slug` field, a string with templates can be used to include values of different fields, e.g. `{{title}}`. This option over-rides the default of `title` field and `identifier_field`. +This setting allows the customization of the collection list view. Similar to the `slug` field, a string with templates can be used to include values of different fields, e.g. `{{title}}`. This option over-rides the default of `title` field and `identifier_field`. + +**Available template tags:** + +Template tags are the same as those for [slug](#slug), with the following additions: + +* `{{filename}}` The file name without the extension part. +* `{{extension}}` The file extension. **Example**