static-cms/src/lib/formatters.ts

247 lines
7.5 KiB
TypeScript
Raw Normal View History

import { stripIndent } from 'common-tags';
2022-09-28 20:04:00 -06:00
import { flow, partialRight, trimEnd, trimStart } from 'lodash';
2022-09-28 20:04:00 -06:00
import { FILES } from '../constants/collectionTypes';
import { COMMIT_AUTHOR, COMMIT_DATE } from '../constants/commitProps';
2022-09-30 06:13:47 -06:00
import { stringTemplate } from './widgets';
import {
2022-09-28 20:04:00 -06:00
getFileFromSlug,
selectField,
2022-09-28 20:04:00 -06:00
selectIdentifier,
selectInferedField,
} from '../reducers/collections';
import { sanitizeSlug } from './urlHelper';
import type { Map } from 'immutable';
2022-10-01 13:45:01 -04:00
import type { CmsConfig } from '../interface';
2022-09-28 20:04:00 -06:00
import type { CmsSlug, Collection, EntryMap } from '../types/redux';
const {
compileStringTemplate,
parseDateFromEntry,
SLUG_MISSING_REQUIRED_DATE,
keyToPathArray,
addFileTemplateFields,
} = stringTemplate;
const commitMessageTemplates = {
create: 'Create {{collection}} “{{slug}}”',
update: 'Update {{collection}} “{{slug}}”',
delete: 'Delete {{collection}} “{{slug}}”',
uploadMedia: 'Upload “{{path}}”',
deleteMedia: 'Delete “{{path}}”',
} as const;
const variableRegex = /\{\{([^}]+)\}\}/g;
type Options = {
slug?: string;
path?: string;
collection?: Collection;
authorLogin?: string;
authorName?: string;
};
export function commitMessageFormatter(
type: keyof typeof commitMessageTemplates,
config: CmsConfig,
{ slug, path, collection, authorLogin, authorName }: Options,
) {
const templates = { ...commitMessageTemplates, ...(config.backend.commit_messages || {}) };
2022-10-01 13:45:01 -04:00
return templates[type].replace(variableRegex, (_, variable) => {
switch (variable) {
case 'slug':
return slug || '';
case 'path':
return path || '';
case 'collection':
return collection ? collection.get('label_singular') || collection.get('label') : '';
case 'author-login':
return authorLogin || '';
case 'author-name':
return authorName || '';
default:
console.warn(`Ignoring unknown variable “${variable}” in commit message template.`);
return '';
}
});
}
export function prepareSlug(slug: string) {
return (
slug
.trim()
// Convert slug to lower-case
.toLocaleLowerCase()
// Remove single quotes.
.replace(/[']/g, '')
// Replace periods with dashes.
.replace(/[.]/g, '-')
);
}
export function getProcessSegment(slugConfig?: CmsSlug, ignoreValues?: string[]) {
return (value: string) =>
ignoreValues && ignoreValues.includes(value)
? value
: flow([value => String(value), prepareSlug, partialRight(sanitizeSlug, slugConfig)])(value);
}
export function slugFormatter(
collection: Collection,
entryData: Map<string, unknown>,
slugConfig?: CmsSlug,
) {
const slugTemplate = collection.get('slug') || '{{slug}}';
const identifier = entryData.getIn(keyToPathArray(selectIdentifier(collection) as string));
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);
if (!collection.has('path')) {
return slug;
} else {
const pathTemplate = prepareSlug(collection.get('path') as string);
return compileStringTemplate(pathTemplate, date, slug, entryData, (value: string) =>
value === slug ? value : processSegment(value),
);
}
}
export function previewUrlFormatter(
baseUrl: string,
collection: Collection,
slug: string,
entry: EntryMap,
slugConfig?: CmsSlug,
) {
/**
* Preview URL can't be created without `baseUrl`. This makes preview URLs
* optional for backends that don't support them.
*/
if (!baseUrl) {
return;
}
const basePath = trimEnd(baseUrl, '/');
const isFileCollection = collection.get('type') === FILES;
const file = isFileCollection ? getFileFromSlug(collection, entry.get('slug')) : undefined;
function getPathTemplate() {
return file?.get('preview_path') ?? collection.get('preview_path');
}
function getDateField() {
return file?.get('preview_path_date_field') ?? collection.get('preview_path_date_field');
}
/**
* If a `previewPath` is provided for the collection/file, use it to construct the
* URL path.
*/
const pathTemplate = getPathTemplate();
/**
* Without a `previewPath` for the collection/file (via config), the preview URL
* will be the URL provided by the backend.
*/
if (!pathTemplate) {
return baseUrl;
}
let fields = entry.get('data') as Map<string, string>;
fields = addFileTemplateFields(entry.get('path'), fields, collection.get('folder'));
const dateFieldName = getDateField() || selectInferedField(collection, 'date');
2021-05-19 14:39:35 +02:00
const date = parseDateFromEntry(entry as unknown as Map<string, unknown>, dateFieldName);
// Prepare and sanitize slug variables only, leave the rest of the
// `preview_path` template as is.
const processSegment = getProcessSegment(slugConfig, [fields.get('dirname')]);
let compiledPath;
try {
compiledPath = compileStringTemplate(pathTemplate, date, slug, fields, processSegment);
2022-09-28 20:04:00 -06:00
} catch (err: any) {
// Print an error and ignore `preview_path` if both:
// 1. Date is invalid (according to Moment), and
// 2. A date expression (eg. `{{year}}`) is used in `preview_path`
if (err.name === SLUG_MISSING_REQUIRED_DATE) {
console.error(stripIndent`
Collection "${collection.get('name')}" configuration error:
\`preview_path_date_field\` must be a field with a valid date. Ignoring \`preview_path\`.
`);
return basePath;
}
throw err;
}
const previewPath = trimStart(compiledPath, ' /');
return `${basePath}/${previewPath}`;
}
export function summaryFormatter(summaryTemplate: string, entry: EntryMap, collection: Collection) {
let entryData = entry.get('data');
const date =
parseDateFromEntry(
2021-05-19 14:39:35 +02:00
entry as unknown as Map<string, unknown>,
selectInferedField(collection, 'date'),
) || null;
const identifier = entryData.getIn(keyToPathArray(selectIdentifier(collection) as string));
entryData = addFileTemplateFields(entry.get('path'), entryData, collection.get('folder'));
Feat: entry sorting (#3494) * refactor: typescript search actions, add tests avoid duplicate search * refactor: switch from promise chain to async/await in loadEntries * feat: add sorting, initial commit * fix: set isFetching to true on entries request * fix: ui improvments and bug fixes * test: fix tests * feat(backend-gitlab): cache local tree) * fix: fix prop type warning * refactor: code cleanup * feat(backend-bitbucket): add local tree caching support * feat: swtich to orderBy and support multiple sort keys * fix: backoff function * fix: improve backoff * feat: infer sortable fields * feat: fetch file commit metadata - initial commit * feat: extract file author and date, finalize GitLab & Bitbucket * refactor: code cleanup * feat: handle github rate limit errors * refactor: code cleanup * fix(github): add missing author and date when traversing cursor * fix: add missing author and date when traversing cursor * refactor: code cleanup * refactor: code cleanup * refactor: code cleanup * test: fix tests * fix: rebuild local tree when head doesn't exist in remote branch * fix: allow sortable fields to be an empty array * fix: allow translation of built in sort fields * build: fix proxy server build * fix: hide commit author and date fields by default on non git backends * fix(algolia): add listAllEntries method for alogolia integration * fix: handle sort fields overflow * test(bitbucket): re-record some bitbucket e2e tests * test(bitbucket): fix media library test * refactor(gitgateway-gitlab): share request code and handle 404 errors * fix: always show commit date by default * docs: add sortableFields * refactor: code cleanup * improvement: drop multi-sort, rework sort UI * chore: force main package bumps Co-authored-by: Shawn Erquhart <shawn@erquh.art>
2020-04-01 06:13:27 +03:00
// allow commit information in summary template
if (entry.get('author') && !selectField(collection, COMMIT_AUTHOR)) {
entryData = entryData.set(COMMIT_AUTHOR, entry.get('author'));
}
if (entry.get('updatedOn') && !selectField(collection, COMMIT_DATE)) {
entryData = entryData.set(COMMIT_DATE, entry.get('updatedOn'));
}
const summary = compileStringTemplate(summaryTemplate, date, identifier, entryData);
return summary;
}
export function folderFormatter(
folderTemplate: string,
entry: EntryMap | undefined,
collection: Collection,
defaultFolder: string,
folderKey: string,
slugConfig?: CmsSlug,
) {
if (!entry || !entry.get('data')) {
return folderTemplate;
}
let fields = (entry.get('data') as Map<string, string>).set(folderKey, defaultFolder);
fields = addFileTemplateFields(entry.get('path'), fields, collection.get('folder'));
const date =
parseDateFromEntry(
2021-05-19 14:39:35 +02:00
entry as unknown as Map<string, unknown>,
selectInferedField(collection, 'date'),
) || null;
const identifier = fields.getIn(keyToPathArray(selectIdentifier(collection) as string));
const processSegment = getProcessSegment(slugConfig, [defaultFolder, fields.get('dirname')]);
const mediaFolder = compileStringTemplate(
folderTemplate,
date,
identifier,
fields,
processSegment,
);
return mediaFolder;
}