Added summary template editor, improved summary template format and options.
This commit is contained in:
85
utils/process-summary-template.js
Normal file
85
utils/process-summary-template.js
Normal file
@@ -0,0 +1,85 @@
|
||||
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;
|
||||
|
||||
const genderMap = {
|
||||
f: 'female',
|
||||
m: 'male',
|
||||
t: 'transsexual',
|
||||
o: 'other',
|
||||
u: null,
|
||||
};
|
||||
|
||||
const propProcessors = {
|
||||
channel: (sceneInfo) => sceneInfo.channel?.name || sceneInfo.network?.name,
|
||||
network: (sceneInfo) => sceneInfo.network?.name || sceneInfo.channel?.name,
|
||||
actors: (sceneInfo, options) => {
|
||||
const genders = (options.genders || 'fmtou').split('').map((genderKey) => genderMap[genderKey]);
|
||||
|
||||
return sceneInfo.actors
|
||||
.filter((actor) => genders.includes(actor.gender))
|
||||
.map((actor) => actor.name);
|
||||
},
|
||||
tags: (sceneInfo, options) => sceneInfo.tags
|
||||
.filter((tag) => {
|
||||
if (options.include && !options.include.includes(tag.name) && !options.include.includes(tag.slug)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options.exclude?.includes(tag.name) || options.exclude?.includes(tag.slug)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options.priority && tag.priority < options.priority) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
.map((tag) => tag.name),
|
||||
movie: (sceneInfo) => sceneInfo.movies[0]?.title,
|
||||
date: (sceneInfo, options) => format(sceneInfo.effectiveDate, options.format || 'yyyy-MM-dd'),
|
||||
};
|
||||
|
||||
export default function processReleaseTemplate(release, chain = template, delimit = ' ', wrapOpen = '', wrapClose = '') {
|
||||
const results = chain.reduce((result, item) => {
|
||||
const key = typeof item === 'string' ? item : item.key;
|
||||
|
||||
if (key) {
|
||||
const value = propProcessors[key]?.(release, typeof item === 'string' ? { key } : item) || release[key];
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return result.concat(value.join(item.delimit || ', '));
|
||||
}
|
||||
|
||||
return result.concat(item.slugify ? slugify(value, item.slugify) : value);
|
||||
}
|
||||
|
||||
if (item.items) {
|
||||
const value = processReleaseTemplate(release, item.items, item.delimit, item.wrap?.[0] || '', item.wrap?.[1] || '');
|
||||
|
||||
return result.concat(item.slugify ? slugify(value, item.slugify) : value);
|
||||
}
|
||||
|
||||
return [];
|
||||
}, []);
|
||||
|
||||
if (results.length > 0) {
|
||||
return `${wrapOpen}${results.filter(Boolean).join(delimit)}${wrapClose}`;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
Reference in New Issue
Block a user