diff --git a/core/frontend/helpers/img_url.js b/core/frontend/helpers/img_url.js index d5156c555d..f0cf8c055b 100644 --- a/core/frontend/helpers/img_url.js +++ b/core/frontend/helpers/img_url.js @@ -11,7 +11,7 @@ const url = require('url'); const _ = require('lodash'); const proxy = require('./proxy'); 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) { // 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) { + const hasLeadingSlash = imagePath[0] === '/'; + + if (hasLeadingSlash) { + return '/' + getImageWithSize(imagePath.slice(1), requestedSize, imageSizes); + } + if (!requestedSize) { return imagePath; } diff --git a/core/test/unit/helpers/img_url_spec.js b/core/test/unit/helpers/img_url_spec.js index 41114529c5..0913c4c2ef 100644 --- a/core/test/unit/helpers/img_url_spec.js +++ b/core/test/unit/helpers/img_url_spec.js @@ -179,5 +179,24 @@ describe('{{image}} helper', function () { should.exist(rendered); 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'); + }); }); });