2018-04-17 22:18:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('config');
|
|
|
|
const dateFns = require('date-fns');
|
|
|
|
|
|
|
|
const extensions = {
|
|
|
|
'image/jpeg': '.jpg',
|
|
|
|
'image/gif': '.gif',
|
|
|
|
'video/mp4': '.mp4',
|
|
|
|
'video/webm': '.webm'
|
|
|
|
};
|
|
|
|
|
2018-04-17 23:55:20 +00:00
|
|
|
function interpolate(path, post, item) {
|
2018-04-17 22:18:04 +00:00
|
|
|
const dateFormat = config.patterns.dateformat || 'YYYYMMDD';
|
|
|
|
|
|
|
|
const vars = {
|
|
|
|
$postId: post.id,
|
2018-04-18 00:05:30 +00:00
|
|
|
$postTitle: (post.title || '').slice(0, config.patterns.titleLength),
|
2018-04-17 22:18:04 +00:00
|
|
|
$postUser: post.user,
|
2018-04-17 23:55:20 +00:00
|
|
|
$postDate: dateFns.format(post.datetime, dateFormat),
|
|
|
|
$postIndex: post.index + config.patterns.indexOffset
|
2018-04-17 22:18:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(post.content.album) {
|
|
|
|
Object.assign(vars, {
|
|
|
|
$albumId: post.content.album.id,
|
2018-04-18 00:05:30 +00:00
|
|
|
$albumTitle: (post.content.album.title || '').slice(0, config.patterns.titleLength),
|
2018-04-17 22:18:04 +00:00
|
|
|
$albumDescription: post.content.album.description,
|
|
|
|
$albumDate: dateFns.format(post.content.album.datetime, dateFormat)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if(item) {
|
|
|
|
Object.assign(vars, {
|
|
|
|
$itemId: item.id,
|
2018-04-18 00:05:30 +00:00
|
|
|
$itemTitle: (item.title || '').slice(0, config.patterns.titleLength),
|
2018-04-17 22:18:04 +00:00
|
|
|
$itemDescription: item.description,
|
|
|
|
$itemDate: dateFns.format(item.datetime, dateFormat),
|
2018-04-17 23:55:20 +00:00
|
|
|
$itemIndex: item.index + config.patterns.indexOffset,
|
2018-04-17 22:18:04 +00:00
|
|
|
$ext: extensions[item.type]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.entries(vars).reduce((acc, [key, value], index) => {
|
|
|
|
// strip slashes for filesystem compatability
|
|
|
|
value = (value || '').toString().replace(/\//g, config.patterns.slashSubstitute);
|
|
|
|
|
|
|
|
return acc.replace(key, value);
|
|
|
|
}, path);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = interpolate;
|