feat(core): Add {{dirname}} to summary and preview_path (#4279)

This commit is contained in:
Keegan Lillo
2020-09-28 23:20:07 +10:00
committed by GitHub
parent 8d00d17ad8
commit 576e4f0f1a
6 changed files with 149 additions and 10 deletions

View File

@ -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));
});