mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
✨ Added size attribute support to img_url helper (#10182)
refs #10181 Adds support to request a size in the img_url helper using syntax like: <img src="{{img_url profile_image size="small"}}"/> Requires the image_sizes config to be defined in the themes package.json
This commit is contained in:
parent
8c3d29edb2
commit
c2275ed131
5 changed files with 93 additions and 8 deletions
|
@ -7,8 +7,9 @@
|
||||||
// Returns the URL for the current object scope i.e. If inside a post scope will return image permalink
|
// Returns the URL for the current object scope i.e. If inside a post scope will return image permalink
|
||||||
// `absolute` flag outputs absolute URL, else URL is relative.
|
// `absolute` flag outputs absolute URL, else URL is relative.
|
||||||
|
|
||||||
var proxy = require('./proxy'),
|
const proxy = require('./proxy');
|
||||||
urlService = proxy.urlService;
|
const urlService = proxy.urlService;
|
||||||
|
const STATIC_IMAGE_URL_PREFIX = `/${urlService.utils.STATIC_IMAGE_URL_PREFIX}`;
|
||||||
|
|
||||||
module.exports = function imgUrl(attr, options) {
|
module.exports = function imgUrl(attr, options) {
|
||||||
// CASE: if no attribute is passed, e.g. `{{img_url}}` we show a warning
|
// CASE: if no attribute is passed, e.g. `{{img_url}}` we show a warning
|
||||||
|
@ -17,19 +18,57 @@ module.exports = function imgUrl(attr, options) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var absolute = options && options.hash && options.hash.absolute;
|
const absolute = options && options.hash && options.hash.absolute;
|
||||||
|
|
||||||
|
const size = options && options.hash && options.hash.size;
|
||||||
|
const imageSizes = options && options.data && options.data.config && options.data.config.image_sizes;
|
||||||
|
|
||||||
|
const image = getImageWithSize(attr, size, imageSizes);
|
||||||
|
|
||||||
// CASE: if attribute is passed, but it is undefined, then the attribute was
|
// CASE: if attribute is passed, but it is undefined, then the attribute was
|
||||||
// an unknown value, e.g. {{img_url feature_img}} and we also show a warning
|
// an unknown value, e.g. {{img_url feature_img}} and we also show a warning
|
||||||
if (attr === undefined) {
|
if (image === undefined) {
|
||||||
proxy.logging.warn(proxy.i18n.t('warnings.helpers.img_url.attrIsRequired'));
|
proxy.logging.warn(proxy.i18n.t('warnings.helpers.img_url.attrIsRequired'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attr) {
|
if (image) {
|
||||||
return urlService.utils.urlFor('image', {image: attr}, absolute);
|
return urlService.utils.urlFor('image', {image}, absolute);
|
||||||
}
|
}
|
||||||
|
|
||||||
// CASE: if you pass e.g. cover_image, but it is not set, then attr is null!
|
// CASE: if you pass e.g. cover_image, but it is not set, then attr is null!
|
||||||
// in this case we don't show a warning
|
// in this case we don't show a warning
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function getImageWithSize(imagePath, requestedSize, imageSizes) {
|
||||||
|
if (!imagePath) {
|
||||||
|
return imagePath;
|
||||||
|
}
|
||||||
|
if (!requestedSize) {
|
||||||
|
return imagePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (/https?:\/\//.test(imagePath) && !imagePath.startsWith(urlService.utils.getBlogUrl())) {
|
||||||
|
return imagePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!imageSizes || !imageSizes[requestedSize]) {
|
||||||
|
return imagePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
const {width, height} = imageSizes[requestedSize];
|
||||||
|
|
||||||
|
if (!width && !height) {
|
||||||
|
return imagePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [imgBlogUrl, imageName] = imagePath.split(STATIC_IMAGE_URL_PREFIX);
|
||||||
|
|
||||||
|
const sizeDirectoryName = prefixIfPresent('w', width) + prefixIfPresent('h', height);
|
||||||
|
|
||||||
|
return [imgBlogUrl, STATIC_IMAGE_URL_PREFIX, `/size/${sizeDirectoryName}`, imageName].join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function prefixIfPresent(prefix, string) {
|
||||||
|
return string ? prefix + string : '';
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
var _ = require('lodash'),
|
var _ = require('lodash'),
|
||||||
defaultConfig = require('./defaults'),
|
defaultConfig = require('./defaults'),
|
||||||
allowedKeys = ['posts_per_page'];
|
allowedKeys = ['posts_per_page', 'image_sizes'];
|
||||||
|
|
||||||
module.exports.create = function configLoader(packageJson) {
|
module.exports.create = function configLoader(packageJson) {
|
||||||
var config = _.cloneDeep(defaultConfig);
|
var config = _.cloneDeep(defaultConfig);
|
||||||
|
|
|
@ -71,6 +71,7 @@ themeMiddleware.updateTemplateData = function updateTemplateData(req, res, next)
|
||||||
|
|
||||||
if (activeTheme.get()) {
|
if (activeTheme.get()) {
|
||||||
themeData.posts_per_page = activeTheme.get().config('posts_per_page');
|
themeData.posts_per_page = activeTheme.get().config('posts_per_page');
|
||||||
|
themeData.image_sizes = activeTheme.get().config('image_sizes');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request-specific information
|
// Request-specific information
|
||||||
|
|
|
@ -92,4 +92,49 @@ describe('{{image}} helper', function () {
|
||||||
rendered.should.equal('http://example.com/picture.jpg');
|
rendered.should.equal('http://example.com/picture.jpg');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('image_sizes', function () {
|
||||||
|
before(function () {
|
||||||
|
configUtils.set({url: 'http://localhost:82832'});
|
||||||
|
});
|
||||||
|
after(function () {
|
||||||
|
configUtils.restore();
|
||||||
|
});
|
||||||
|
it('should output correct url for absolute paths which are internal', function () {
|
||||||
|
var rendered = helpers.img_url('http://localhost:82832/content/images/my-coole-img.jpg', {
|
||||||
|
hash: {
|
||||||
|
size: 'medium',
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
config: {
|
||||||
|
image_sizes: {
|
||||||
|
medium: {
|
||||||
|
width: 400
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
should.exist(rendered);
|
||||||
|
rendered.should.equal('http://localhost:82832/content/images/size/w400/my-coole-img.jpg');
|
||||||
|
});
|
||||||
|
it('should output the correct url for relative paths', function () {
|
||||||
|
var rendered = helpers.img_url('/content/images/my-coole-img.jpg', {
|
||||||
|
hash: {
|
||||||
|
size: 'medium',
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
config: {
|
||||||
|
image_sizes: {
|
||||||
|
medium: {
|
||||||
|
width: 400
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
should.exist(rendered);
|
||||||
|
rendered.should.equal('/content/images/size/w400/my-coole-img.jpg');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -95,7 +95,7 @@ describe('Themes', function () {
|
||||||
|
|
||||||
describe('updateTemplateData', function () {
|
describe('updateTemplateData', function () {
|
||||||
var updateTemplateData = middleware[1],
|
var updateTemplateData = middleware[1],
|
||||||
themeDataExpectedProps = ['posts_per_page'],
|
themeDataExpectedProps = ['posts_per_page', 'image_sizes'],
|
||||||
blogDataExpectedProps = [
|
blogDataExpectedProps = [
|
||||||
'url', 'title', 'description', 'logo', 'cover_image', 'icon', 'twitter', 'facebook', 'navigation',
|
'url', 'title', 'description', 'logo', 'cover_image', 'icon', 'twitter', 'facebook', 'navigation',
|
||||||
'timezone', 'amp'
|
'timezone', 'amp'
|
||||||
|
|
Loading…
Add table
Reference in a new issue