ripunzel/src/interpolate.js

82 lines
2.9 KiB
JavaScript
Raw Normal View History

'use strict';
const config = require('config');
const path = require('path');
const url = require('url');
const dateFns = require('date-fns');
const mime = require('mime-types');
function interpolate(pattern, user, post, item) {
const dateFormat = config.library.dateFormat || 'YYYYMMDD';
2024-09-11 03:16:53 +00:00
const vars = {
$base: config.library.base
};
if(user) {
Object.assign(vars, {
$user: user.name,
$username: user.name,
$userId: user.id,
$userCreated: dateFns.format(user.created, dateFormat),
$userVerified: user.verified ? config.library.booleans.verified : '',
$userVerifiedEmail: user.verifiedEmail ? config.library.booleans.verifiedEmail : '',
$userGold: user.gold ? config.library.booleans.gold : '',
});
2024-09-11 03:16:53 +00:00
if(user.profile) {
Object.assign(vars, {
$profileId: user.profile.id,
$profileTitle: user.profile.title,
$profileDescription: user.profile.description,
$profileOver18: user.profile.over18 ? config.library.booleans.over18 : ''
});
}
}
if(post) {
Object.assign(vars, {
$postId: post.id,
$postTitle: (post.title || '').slice(0, config.library.titleLength),
$postUser: post.user || user.user,
$postDate: dateFns.format(post.datetime, dateFormat),
$postIndex: post.index + config.library.indexOffset,
2024-09-11 03:16:53 +00:00
$subreddit: post.subreddit,
$host: post.host.label
});
if(post.content.album) {
Object.assign(vars, {
$albumId: post.content.album.id,
$albumTitle: (post.content.album.title || '').slice(0, config.library.titleLength),
$albumDescription: post.content.album.description,
$albumDate: dateFns.format(post.content.album.datetime, dateFormat)
});
}
}
if(item) {
Object.assign(vars, {
$itemId: item.id,
$itemTitle: (item.title || '').slice(0, config.library.titleLength),
$itemDescription: item.description,
$itemDate: dateFns.format(item.datetime, dateFormat),
$itemIndex: item.index + config.library.indexOffset,
$extracted: item.extracted ? config.library.booleans.extracted : '',
$preview: item.preview ? config.library.booleans.preview : '',
2024-09-11 03:16:55 +00:00
$ext: `.${mime.extension(item.type) || path.extname(url.parse(item.url).pathname)}`
});
}
return Object.entries(vars).reduce((acc, [key, value], index) => {
// substitute slashes for filesystem compatability
2024-09-11 03:16:53 +00:00
if(key !== '$base') {
value = (value || '').toString().replace(/\//g, config.library.slashSubstitute);
}
return acc.replace(key, value);
}, pattern);
};
module.exports = interpolate;