mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Fixed img_url helper when using image sizes with relative path… (#10964)
closes #10949 This updates the getImageWithSize function in the img_url helper to consider relative paths WITHOUT a leading slash the "base case". If a path does have a leading slash, we remove it, pass it through the function again, and then prepend the slash.
This commit is contained in:
parent
7cc90a3f62
commit
b0efad7ac9
2 changed files with 26 additions and 1 deletions
|
@ -11,7 +11,7 @@ const url = require('url');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const proxy = require('./proxy');
|
const proxy = require('./proxy');
|
||||||
const urlUtils = proxy.urlUtils;
|
const urlUtils = proxy.urlUtils;
|
||||||
const STATIC_IMAGE_URL_PREFIX = `/${urlUtils.STATIC_IMAGE_URL_PREFIX}`;
|
const STATIC_IMAGE_URL_PREFIX = `${urlUtils.STATIC_IMAGE_URL_PREFIX}`;
|
||||||
|
|
||||||
module.exports = function imgUrl(requestedImageUrl, options) {
|
module.exports = function imgUrl(requestedImageUrl, options) {
|
||||||
// CASE: if no url is passed, e.g. `{{img_url}}` we show a warning
|
// CASE: if no url is passed, e.g. `{{img_url}}` we show a warning
|
||||||
|
@ -94,6 +94,12 @@ function detectInternalImage(requestedImageUrl) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getImageWithSize(imagePath, requestedSize, imageSizes) {
|
function getImageWithSize(imagePath, requestedSize, imageSizes) {
|
||||||
|
const hasLeadingSlash = imagePath[0] === '/';
|
||||||
|
|
||||||
|
if (hasLeadingSlash) {
|
||||||
|
return '/' + getImageWithSize(imagePath.slice(1), requestedSize, imageSizes);
|
||||||
|
}
|
||||||
|
|
||||||
if (!requestedSize) {
|
if (!requestedSize) {
|
||||||
return imagePath;
|
return imagePath;
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,5 +179,24 @@ describe('{{image}} helper', function () {
|
||||||
should.exist(rendered);
|
should.exist(rendered);
|
||||||
rendered.should.equal('/content/images/size/w400/my-coole-img.jpg');
|
rendered.should.equal('/content/images/size/w400/my-coole-img.jpg');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should output the correct url for relative paths without leading slash', 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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue