2021-03-01 14:24:21 +03:00
|
|
|
// @TODO: put together a plan for how the frontend should exist as modules and move this out
|
|
|
|
// The preview header contains a query string with the custom preview data
|
|
|
|
// This is deliberately slightly obscure & means we don't need to add body parsing to the frontend :D
|
|
|
|
// If we start passing in strings like title or description we will probably need to change this
|
|
|
|
const PREVIEW_HEADER_NAME = 'x-ghost-preview';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
function decodeValue(value) {
|
|
|
|
if (value === '' || value === 'null' || value === 'undefined') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:23:39 +03:00
|
|
|
function getPreviewData(previewHeader, customThemeSettingKeys = []) {
|
2021-03-01 14:24:21 +03:00
|
|
|
// Keep the string shorter with short codes for certain parameters
|
|
|
|
const supportedSettings = {
|
|
|
|
c: 'accent_color',
|
|
|
|
icon: 'icon',
|
|
|
|
logo: 'logo',
|
2021-09-30 15:23:39 +03:00
|
|
|
cover: 'cover_image',
|
2021-09-30 16:07:28 +03:00
|
|
|
custom: 'custom',
|
|
|
|
d: 'description'
|
2021-03-01 14:24:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let opts = new URLSearchParams(previewHeader);
|
|
|
|
|
2021-09-30 12:36:05 +03:00
|
|
|
const previewData = {};
|
|
|
|
|
2021-03-01 14:24:21 +03:00
|
|
|
opts.forEach((value, key) => {
|
|
|
|
value = decodeValue(value);
|
|
|
|
if (supportedSettings[key]) {
|
2021-09-30 12:36:05 +03:00
|
|
|
_.set(previewData, supportedSettings[key], value);
|
2021-03-01 14:24:21 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-30 15:23:39 +03:00
|
|
|
if (previewData.custom) {
|
|
|
|
try {
|
|
|
|
const custom = {};
|
|
|
|
const previewCustom = JSON.parse(previewData.custom);
|
|
|
|
|
|
|
|
if (typeof previewCustom === 'object') {
|
|
|
|
customThemeSettingKeys.forEach((key) => {
|
|
|
|
custom[key] = previewCustom[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
previewData.custom = custom;
|
|
|
|
} catch (e) {
|
|
|
|
previewData.custom = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 12:36:05 +03:00
|
|
|
previewData._preview = previewHeader;
|
2021-03-01 14:24:21 +03:00
|
|
|
|
2021-09-30 12:36:05 +03:00
|
|
|
return previewData;
|
2021-03-01 14:24:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports._PREVIEW_HEADER_NAME = PREVIEW_HEADER_NAME;
|
2021-09-30 15:23:39 +03:00
|
|
|
module.exports.handle = (req, customThemeSettingKeys) => {
|
2021-09-30 12:36:05 +03:00
|
|
|
let previewData = {};
|
|
|
|
|
2021-03-01 14:24:21 +03:00
|
|
|
if (req && req.header(PREVIEW_HEADER_NAME)) {
|
2021-09-30 15:23:39 +03:00
|
|
|
previewData = getPreviewData(req.header(PREVIEW_HEADER_NAME), customThemeSettingKeys);
|
2021-03-01 14:24:21 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 12:36:05 +03:00
|
|
|
return previewData;
|
2021-03-01 14:24:21 +03:00
|
|
|
};
|