feat: support filename and extension vars in summary (#3375)

This commit is contained in:
Erez Rokah 2020-03-04 11:08:15 +01:00 committed by GitHub
parent b7b4bcb609
commit 12444ca761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 3 deletions

View File

@ -358,6 +358,19 @@ describe('formatters', () => {
expect(summaryFormatter('{{title}}-{{year}}', entry, collection)).toBe('title-2020'); 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', () => { describe('folderFormatter', () => {

View File

@ -200,9 +200,11 @@ export const summaryFormatter = (
entry: EntryMap, entry: EntryMap,
collection: Collection, collection: Collection,
) => { ) => {
const entryData = entry.get('data'); let entryData = entry.get('data');
const date = parseDateFromEntry(entry, collection) || null; const date = parseDateFromEntry(entry, collection) || null;
const identifier = entryData.getIn(keyToPathArray(selectIdentifier(collection) as string)); const identifier = entryData.getIn(keyToPathArray(selectIdentifier(collection) as string));
entryData = addFileTemplateFields(entry.get('path'), entryData);
const summary = compileStringTemplate(summaryTemplate, date, identifier, entryData); const summary = compileStringTemplate(summaryTemplate, date, identifier, entryData);
return summary; return summary;
}; };

View File

@ -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: 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. * 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. * `{{filename}}` The file name without the extension part.
* `{{extension}}` The file extension. * `{{extension}}` The file extension.
@ -358,7 +358,14 @@ This setting changes options for the editor view of the collection. It has one o
### `summary` ### `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** **Example**