feat(core): Add {{dirname}} to summary and preview_path (#4279)
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import moment from 'moment';
|
||||
import { Map } from 'immutable';
|
||||
import { basename, extname } from 'path';
|
||||
import { basename, extname, dirname } from 'path';
|
||||
import { get, trimEnd } from 'lodash';
|
||||
|
||||
const FIELD_PREFIX = 'fields.';
|
||||
@ -169,14 +169,28 @@ export function extractTemplateVars(template: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export const addFileTemplateFields = (entryPath: string, fields: Map<string, string>) => {
|
||||
/**
|
||||
* Appends `dirname`, `filename` and `extension` to the provided `fields` map.
|
||||
* @param entryPath
|
||||
* @param fields
|
||||
* @param folder - optionally include a folder that the dirname will be relative to.
|
||||
* eg: `addFileTemplateFields('foo/bar/baz.ext', fields, 'foo')`
|
||||
* will result in: `{ dirname: 'bar', filename: 'baz', extension: 'ext' }`
|
||||
*/
|
||||
export const addFileTemplateFields = (
|
||||
entryPath: string,
|
||||
fields: Map<string, string>,
|
||||
folder = '',
|
||||
) => {
|
||||
if (!entryPath) {
|
||||
return fields;
|
||||
}
|
||||
|
||||
const extension = extname(entryPath);
|
||||
const filename = basename(entryPath, extension);
|
||||
const dirnameExcludingFolder = dirname(entryPath).replace(new RegExp(`^(/?)${folder}/?`), '$1');
|
||||
fields = fields.withMutations(map => {
|
||||
map.set('dirname', dirnameExcludingFolder);
|
||||
map.set('filename', filename);
|
||||
map.set('extension', extension === '' ? extension : extension.substr(1));
|
||||
});
|
||||
|
Reference in New Issue
Block a user