'use strict';
const R = require('ramda');
/*
Embedded content type
{
"data": {
"type": "embed",
"id":12345,
"attributes": {
"subType": "poll",
"label": "Poll"
},
"links":{
"url":"https://www.mirror.co.uk/.../embedded-view/mirror-12345"
}
}
}
*/
const capitalize = str => str.replace(/^./, R.toUpper);
const dehyphenize = R.compose(R.join(' '), R.split('-'));
const censored = R.flip(R.reduce((str, [match,rep]) => str.replace(new RegExp(match,'gi'), rep)));
const objToMap = obj => new Map(R.toPairs(obj));
const createEmbedUrl = (url, pubName, filename, id) => `${url}${filename}/embedded-webview/${pubName}-${id}`;
const wordObj = {
'interactive HTML':'Interactive Content',
'html':'Interactive Content',
'jotform': 'Form',
'facebook':'Facebook'
};
const words = objToMap(wordObj);
const humanReadableLabel = R.compose(dehyphenize, capitalize, censored(words));
module.exports = (homeSection, filename, item) => {
const attributes = {
"subType": item.sub_type,
"label": humanReadableLabel(item.sub_type)
};
const links = {
"url": createEmbedUrl(
homeSection.url,
R.path(['publication', 'name'], homeSection) ? homeSection.publication.name : '',
filename,
item.escenic_id
)
};
const data = {
"type":"embed",
"id": item.escenic_id,
attributes,
links
};
return data;
}; |