0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/frontend/services/theme-engine/preview.js
Kevin Ansfield 8a17e723a1 Moved usage of preview options from globalTemplateOptions to localTemplateOptions
refs https://github.com/TryGhost/Team/issues/1097

globalTemplateOptions are supposed to be static with localTemplateOptions being merged in per-request, however the per-request preview data was being extracted and set in the global options. Comments suggest that the global data should be static and eventually updated via other means, the usage of the request object to get per-request preview data is working against that.

- adjusted the preview handler to return an object rather than changing properties by reference on a passed in object
- moved preview data fetching out of `getSiteData()` used in `updateGlobalTemplateOptions()` and into `updateLocalTemplateOptions()` so that we're not relying on the request object in `updateGlobalTemplateOptions()`
2021-09-30 12:12:55 +01:00

51 lines
1.4 KiB
JavaScript

// @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;
}
function getPreviewData(previewHeader) {
// Keep the string shorter with short codes for certain parameters
const supportedSettings = {
c: 'accent_color',
icon: 'icon',
logo: 'logo',
cover: 'cover_image'
};
let opts = new URLSearchParams(previewHeader);
const previewData = {};
opts.forEach((value, key) => {
value = decodeValue(value);
if (supportedSettings[key]) {
_.set(previewData, supportedSettings[key], value);
}
});
previewData._preview = previewHeader;
return previewData;
}
module.exports._PREVIEW_HEADER_NAME = PREVIEW_HEADER_NAME;
module.exports.handle = (req) => {
let previewData = {};
if (req && req.header(PREVIEW_HEADER_NAME)) {
previewData = getPreviewData(req.header(PREVIEW_HEADER_NAME));
}
return previewData;
};