Added elaborate template switching.
This commit is contained in:
@@ -1,19 +1,8 @@
|
||||
import { format } from 'date-fns';
|
||||
import Cookies from 'js-cookie';
|
||||
import { parse } from 'yaml';
|
||||
|
||||
import slugify from '#/utils/slugify.js';
|
||||
|
||||
import defaultTemplate from '#/assets/summary.yaml';
|
||||
|
||||
const cookies = Cookies.withConverter({
|
||||
write: (value) => value,
|
||||
});
|
||||
|
||||
const storedTemplate = cookies.get('summary');
|
||||
const template = storedTemplate
|
||||
? parse(JSON.parse(storedTemplate)?.custom)
|
||||
: defaultTemplate;
|
||||
import ellipsis from '#/utils/ellipsis.js';
|
||||
|
||||
const genderMap = {
|
||||
f: 'female',
|
||||
@@ -35,11 +24,11 @@ const propProcessors = {
|
||||
},
|
||||
tags: (sceneInfo, options) => sceneInfo.tags
|
||||
.filter((tag) => {
|
||||
if (options.include && !options.include.includes(tag.name) && !options.include.includes(tag.slug)) {
|
||||
if (options.include && !options.include.includes(tag.slug)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options.exclude?.includes(tag.name) || options.exclude?.includes(tag.slug)) {
|
||||
if (options.exclude?.includes(tag.slug)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -54,32 +43,58 @@ const propProcessors = {
|
||||
date: (sceneInfo, options) => format(sceneInfo.effectiveDate, options.format || 'yyyy-MM-dd'),
|
||||
};
|
||||
|
||||
export default function processReleaseTemplate(release, chain = template, delimit = ' ', wrapOpen = '', wrapClose = '') {
|
||||
function curateValue(value, item) {
|
||||
return [].concat(value) // account for both arrays (actors, tags) and strings (title, channel)
|
||||
.slice(0, item.limit || Infinity)
|
||||
.map((listValue) => (item.slugify ? slugify(listValue, item.slugify) : listValue))
|
||||
.map((listValue) => ellipsis(listValue, item.slice || Infinity, item.ellipsis || ''))
|
||||
.join(item.delimit || ', ');
|
||||
}
|
||||
|
||||
function traverseTemplate(chain, release, {
|
||||
delimit = ' ',
|
||||
wrap = ['', ''],
|
||||
} = {}) {
|
||||
const results = chain.reduce((result, item) => {
|
||||
const key = typeof item === 'string' ? item : item.key;
|
||||
const keys = typeof item === 'string' ? item : item.key;
|
||||
|
||||
if (key) {
|
||||
const value = propProcessors[key]?.(release, typeof item === 'string' ? { key } : item) || release[key];
|
||||
if ((item.channels && !item.channels.includes(release.channel?.slug)) || item.notChannels?.includes(release.channel?.slug)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return result.concat(value.join(item.delimit || ', '));
|
||||
}
|
||||
if ((item.networks && !item.networks.includes(release.network?.slug)) || item.notNetworks?.includes(release.network?.slug)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
return result.concat(item.slugify ? slugify(value, item.slugify) : value);
|
||||
if (keys) {
|
||||
const value = keys.split('|').reduce((acc, key) => acc
|
||||
|| propProcessors[key]?.(release, typeof item === 'string' ? { key } : item)
|
||||
|| release[key], null);
|
||||
|
||||
return result.concat(curateValue(value, item));
|
||||
}
|
||||
|
||||
if (item.items) {
|
||||
const value = processReleaseTemplate(release, item.items, item.delimit, item.wrap?.[0] || '', item.wrap?.[1] || '');
|
||||
const group = traverseTemplate(item.items, release, {
|
||||
delimit: item.delimit,
|
||||
wrap: item.wrap,
|
||||
});
|
||||
|
||||
return result.concat(item.slugify ? slugify(value, item.slugify) : value);
|
||||
return result.concat(curateValue(group, item));
|
||||
}
|
||||
|
||||
return [];
|
||||
return result;
|
||||
}, []);
|
||||
|
||||
if (results.length > 0) {
|
||||
return `${wrapOpen}${results.filter(Boolean).join(delimit)}${wrapClose}`;
|
||||
return `${wrap[0] || ''}${results.filter(Boolean).join(delimit)}${wrap[1] || ''}`;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
export default function processSummaryTemplate(template, release) {
|
||||
const chain = parse(template);
|
||||
|
||||
return traverseTemplate(chain, release);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user