2022-10-20 11:57:30 -04:00
|
|
|
import flow from 'lodash/flow';
|
|
|
|
import get from 'lodash/get';
|
|
|
|
import partialRight from 'lodash/partialRight';
|
2021-05-31 16:46:41 +02:00
|
|
|
|
2022-09-28 20:04:00 -06:00
|
|
|
import { COMMIT_AUTHOR, COMMIT_DATE } from '../constants/commitProps';
|
2021-05-31 16:46:41 +02:00
|
|
|
import { sanitizeSlug } from './urlHelper';
|
2022-10-20 11:57:30 -04:00
|
|
|
import { selectIdentifier, selectInferedField } from './util/collection.util';
|
|
|
|
import { selectField } from './util/field.util';
|
|
|
|
import { set } from './util/object.util';
|
|
|
|
import {
|
|
|
|
addFileTemplateFields,
|
2020-01-22 20:42:24 +02:00
|
|
|
compileStringTemplate,
|
2020-02-12 08:30:44 +02:00
|
|
|
keyToPathArray,
|
2022-12-01 13:09:45 -05:00
|
|
|
parseDateFromEntry,
|
2022-10-20 11:57:30 -04:00
|
|
|
} from './widgets/stringTemplate';
|
|
|
|
|
|
|
|
import type { Collection, Config, Entry, EntryData, Slug } from '../interface';
|
2020-01-22 20:42:24 +02:00
|
|
|
|
2021-03-11 12:08:46 +02:00
|
|
|
const commitMessageTemplates = {
|
2020-01-22 20:42:24 +02:00
|
|
|
create: 'Create {{collection}} “{{slug}}”',
|
|
|
|
update: 'Update {{collection}} “{{slug}}”',
|
|
|
|
delete: 'Delete {{collection}} “{{slug}}”',
|
|
|
|
uploadMedia: 'Upload “{{path}}”',
|
|
|
|
deleteMedia: 'Delete “{{path}}”',
|
2021-03-11 12:08:46 +02:00
|
|
|
} as const;
|
2020-01-22 20:42:24 +02:00
|
|
|
|
|
|
|
const variableRegex = /\{\{([^}]+)\}\}/g;
|
|
|
|
|
|
|
|
type Options = {
|
|
|
|
slug?: string;
|
|
|
|
path?: string;
|
|
|
|
collection?: Collection;
|
|
|
|
authorLogin?: string;
|
|
|
|
authorName?: string;
|
|
|
|
};
|
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
export function commitMessageFormatter(
|
2021-03-11 12:08:46 +02:00
|
|
|
type: keyof typeof commitMessageTemplates,
|
2022-10-20 11:57:30 -04:00
|
|
|
config: Config,
|
2020-01-22 20:42:24 +02:00
|
|
|
{ slug, path, collection, authorLogin, authorName }: Options,
|
2021-02-08 20:01:21 +02:00
|
|
|
) {
|
2021-03-11 12:08:46 +02:00
|
|
|
const templates = { ...commitMessageTemplates, ...(config.backend.commit_messages || {}) };
|
2020-01-22 20:42:24 +02:00
|
|
|
|
2022-10-01 13:45:01 -04:00
|
|
|
return templates[type].replace(variableRegex, (_, variable) => {
|
2020-01-22 20:42:24 +02:00
|
|
|
switch (variable) {
|
|
|
|
case 'slug':
|
|
|
|
return slug || '';
|
|
|
|
case 'path':
|
|
|
|
return path || '';
|
|
|
|
case 'collection':
|
2022-10-20 11:57:30 -04:00
|
|
|
return collection ? collection.label_singular || collection.label : '';
|
2020-05-25 02:36:35 -04:00
|
|
|
case 'author-login':
|
|
|
|
return authorLogin || '';
|
|
|
|
case 'author-name':
|
|
|
|
return authorName || '';
|
2020-01-22 20:42:24 +02:00
|
|
|
default:
|
|
|
|
console.warn(`Ignoring unknown variable “${variable}” in commit message template.`);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
});
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-01-22 20:42:24 +02:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
export function prepareSlug(slug: string) {
|
2020-01-22 20:42:24 +02:00
|
|
|
return (
|
|
|
|
slug
|
|
|
|
.trim()
|
|
|
|
// Convert slug to lower-case
|
|
|
|
.toLocaleLowerCase()
|
|
|
|
|
|
|
|
// Remove single quotes.
|
|
|
|
.replace(/[']/g, '')
|
|
|
|
|
|
|
|
// Replace periods with dashes.
|
|
|
|
.replace(/[.]/g, '-')
|
|
|
|
);
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-01-22 20:42:24 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
export function getProcessSegment(slugConfig?: Slug, ignoreValues?: string[]) {
|
2020-09-28 23:20:07 +10:00
|
|
|
return (value: string) =>
|
2021-03-11 12:08:46 +02:00
|
|
|
ignoreValues && ignoreValues.includes(value)
|
2020-09-28 23:20:07 +10:00
|
|
|
? value
|
|
|
|
: flow([value => String(value), prepareSlug, partialRight(sanitizeSlug, slugConfig)])(value);
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-01-22 20:42:24 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
export function slugFormatter(collection: Collection, entryData: EntryData, slugConfig?: Slug) {
|
|
|
|
const slugTemplate = collection.slug || '{{slug}}';
|
2020-01-22 20:42:24 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
const identifier = get(entryData, keyToPathArray(selectIdentifier(collection)));
|
2020-01-22 20:42:24 +02:00
|
|
|
if (!identifier) {
|
|
|
|
throw new Error(
|
|
|
|
'Collection must have a field name that is a valid entry identifier, or must have `identifier_field` set',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const processSegment = getProcessSegment(slugConfig);
|
|
|
|
const date = new Date();
|
|
|
|
const slug = compileStringTemplate(slugTemplate, date, identifier, entryData, processSegment);
|
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
if (!('path' in collection)) {
|
2020-01-22 20:42:24 +02:00
|
|
|
return slug;
|
|
|
|
} else {
|
2022-10-20 11:57:30 -04:00
|
|
|
const pathTemplate = prepareSlug(collection.path as string);
|
2020-01-22 20:42:24 +02:00
|
|
|
return compileStringTemplate(pathTemplate, date, slug, entryData, (value: string) =>
|
|
|
|
value === slug ? value : processSegment(value),
|
|
|
|
);
|
|
|
|
}
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-01-22 20:42:24 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
export function summaryFormatter(summaryTemplate: string, entry: Entry, collection: Collection) {
|
|
|
|
let entryData = entry.data;
|
|
|
|
const date = parseDateFromEntry(entry, selectInferedField(collection, 'date')) || null;
|
|
|
|
const identifier = get(entryData, keyToPathArray(selectIdentifier(collection)));
|
2020-03-04 11:08:15 +01:00
|
|
|
|
2022-11-07 10:27:58 -05:00
|
|
|
entryData =
|
|
|
|
addFileTemplateFields(entry.path, entryData, 'folder' in collection ? collection.folder : '') ??
|
|
|
|
{};
|
2020-04-01 06:13:27 +03:00
|
|
|
// allow commit information in summary template
|
2022-10-20 11:57:30 -04:00
|
|
|
if (entry.author && !selectField(collection, COMMIT_AUTHOR)) {
|
|
|
|
entryData = set(entryData, COMMIT_AUTHOR, entry.author);
|
2020-04-01 06:13:27 +03:00
|
|
|
}
|
2022-10-20 11:57:30 -04:00
|
|
|
if (entry.updatedOn && !selectField(collection, COMMIT_DATE)) {
|
|
|
|
entryData = set(entryData, COMMIT_DATE, entry.updatedOn);
|
2020-04-01 06:13:27 +03:00
|
|
|
}
|
2020-01-22 20:42:24 +02:00
|
|
|
const summary = compileStringTemplate(summaryTemplate, date, identifier, entryData);
|
|
|
|
return summary;
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-01-22 20:42:24 +02:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
export function folderFormatter(
|
2020-01-22 20:42:24 +02:00
|
|
|
folderTemplate: string,
|
2023-01-10 12:49:12 -05:00
|
|
|
entry: Entry | null | undefined,
|
2020-01-22 20:42:24 +02:00
|
|
|
collection: Collection,
|
|
|
|
defaultFolder: string,
|
|
|
|
folderKey: string,
|
2022-10-20 11:57:30 -04:00
|
|
|
slugConfig?: Slug,
|
2021-02-08 20:01:21 +02:00
|
|
|
) {
|
2022-10-20 11:57:30 -04:00
|
|
|
if (!entry || !entry.data) {
|
2020-01-22 20:42:24 +02:00
|
|
|
return folderTemplate;
|
|
|
|
}
|
2020-02-14 22:31:33 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
let fields = set(entry.data, folderKey, defaultFolder) as EntryData;
|
2022-11-07 10:27:58 -05:00
|
|
|
fields = addFileTemplateFields(
|
|
|
|
entry.path,
|
|
|
|
fields,
|
|
|
|
'folder' in collection ? collection.folder : '',
|
|
|
|
);
|
2020-01-22 20:42:24 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
const date = parseDateFromEntry(entry, selectInferedField(collection, 'date')) || null;
|
|
|
|
const identifier = get(fields, keyToPathArray(selectIdentifier(collection)));
|
|
|
|
const processSegment = getProcessSegment(slugConfig, [defaultFolder, fields?.dirname as string]);
|
2020-01-22 20:42:24 +02:00
|
|
|
|
|
|
|
const mediaFolder = compileStringTemplate(
|
|
|
|
folderTemplate,
|
|
|
|
date,
|
|
|
|
identifier,
|
|
|
|
fields,
|
2020-09-28 23:20:07 +10:00
|
|
|
processSegment,
|
2020-01-22 20:42:24 +02:00
|
|
|
);
|
2020-02-14 22:31:33 +02:00
|
|
|
|
2020-01-22 20:42:24 +02:00
|
|
|
return mediaFolder;
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|