0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-15 03:01:37 -05:00

Added image as an allowed custom theme setting type

refs https://github.com/TryGhost/Team/issues/1107

- updated schema validation to allow `'image'` through as a known setting type now that Admin has support
- added transformation of setting values for `'image'` types because they will be URLs and should be stored with `__GHOST_URL__`
This commit is contained in:
Kevin Ansfield 2021-10-14 18:41:40 +01:00
parent 3d57ac4464
commit d7ae6e0138
3 changed files with 27 additions and 1 deletions

View file

@ -681,7 +681,8 @@ module.exports = {
'select',
'boolean',
'color',
'text'
'text',
'image'
]]
}
},

View file

@ -1,5 +1,6 @@
const _ = require('lodash');
const ghostBookshelf = require('./base');
const urlUtils = require('../../shared/url-utils');
const CustomThemeSetting = ghostBookshelf.Model.extend({
tableName: 'custom_theme_settings',
@ -18,6 +19,12 @@ const CustomThemeSetting = ghostBookshelf.Model.extend({
attrs.value = JSON.parse(attrs.value);
}
// transform URLs to absolute for image settings
if (settingType === 'image' && attrs.value) {
console.log('.parse', attrs.value, urlUtils.transformReadyToAbsolute(attrs.value));
attrs.value = urlUtils.transformReadyToAbsolute(attrs.value);
}
return attrs;
},
@ -41,6 +48,14 @@ const CustomThemeSetting = ghostBookshelf.Model.extend({
}
}
return attrs;
},
formatOnWrite(attrs) {
if (attrs.type === 'image' && attrs.value) {
attrs.value = urlUtils.toTransformReady(attrs.value);
}
return attrs;
}
});

View file

@ -30,6 +30,9 @@ describe('Unit: models/custom-theme-setting', function () {
returns = setting.parse({theme: 'test', key: 'something', value: 'null', type: 'select'});
should.equal(returns.value, 'null');
returns = setting.parse({theme: 'test', key: 'something', value: '__GHOST_URL__/assets/image.jpg', type: 'image'});
should.equal(returns.value, 'http://127.0.0.1:2369/assets/image.jpg');
});
});
@ -49,5 +52,12 @@ describe('Unit: models/custom-theme-setting', function () {
returns = setting.format({theme: 'test', key: 'dark_mode', value: 'true', type: 'boolean'});
should.equal(returns.value, 'true');
});
it('transforms urls when persisting to db', function () {
const setting = models.CustomThemeSetting.forge();
let returns = setting.formatOnWrite({theme: 'test', key: 'something', value: '/assets/image.jpg', type: 'image'});
should.equal(returns.value, '__GHOST_URL__/assets/image.jpg');
});
});
});