mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Fixed image URL to be stored as relative in mobiledoc
refs #10477 closes #10472 - Adds transformation for any asset absolute URL's into relative used in mobiledoc
This commit is contained in:
parent
21e6242498
commit
e47d1e275f
2 changed files with 82 additions and 2 deletions
|
@ -1,16 +1,38 @@
|
|||
const _ = require('lodash');
|
||||
const {absoluteToRelative, getBlogUrl, STATIC_IMAGE_URL_PREFIX} = require('../../../../../../services/url/utils');
|
||||
|
||||
const blogDomain = getBlogUrl().replace(/^http(s?):\/\//, '').replace(/\/$/, '');
|
||||
|
||||
const handleImageUrl = (imageUrl) => {
|
||||
const blogUrl = getBlogUrl().replace(/^http(s?):\/\//, '').replace(/\/$/, '');
|
||||
const imageUrlAbsolute = imageUrl.replace(/^http(s?):\/\//, '');
|
||||
const imagePathRe = new RegExp(`^${blogUrl}/${STATIC_IMAGE_URL_PREFIX}`);
|
||||
const imagePathRe = new RegExp(`^${blogDomain}/${STATIC_IMAGE_URL_PREFIX}`);
|
||||
if (imagePathRe.test(imageUrlAbsolute)) {
|
||||
return absoluteToRelative(imageUrl);
|
||||
}
|
||||
return imageUrl;
|
||||
};
|
||||
|
||||
const handleContentUrls = (content) => {
|
||||
const imagePathRe = new RegExp(`(http(s?)://)?${blogDomain}/${STATIC_IMAGE_URL_PREFIX}`, 'g');
|
||||
|
||||
const matches = _.uniq(content.match(imagePathRe));
|
||||
|
||||
if (matches) {
|
||||
matches.forEach((match) => {
|
||||
const relative = absoluteToRelative(match);
|
||||
content = content.replace(new RegExp(match, 'g'), relative);
|
||||
});
|
||||
}
|
||||
|
||||
return content;
|
||||
};
|
||||
|
||||
const forPost = (attrs, options) => {
|
||||
// make all content image URLs relative, ref: https://github.com/TryGhost/Ghost/issues/10477
|
||||
if (attrs.mobiledoc) {
|
||||
attrs.mobiledoc = handleContentUrls(attrs.mobiledoc);
|
||||
}
|
||||
|
||||
if (attrs.feature_image) {
|
||||
attrs.feature_image = handleImageUrl(attrs.feature_image);
|
||||
}
|
||||
|
|
|
@ -228,6 +228,64 @@ describe('Unit: v2/utils/serializers/input/posts', function () {
|
|||
configUtils.restore();
|
||||
});
|
||||
|
||||
it('when mobiledoc contains an absolute URL to image', function () {
|
||||
configUtils.set({url: 'https://mysite.com'});
|
||||
const apiConfig = {};
|
||||
const frame = {
|
||||
options: {
|
||||
context: {
|
||||
user: 0,
|
||||
api_key: {
|
||||
id: 1,
|
||||
type: 'content'
|
||||
},
|
||||
},
|
||||
},
|
||||
data: {
|
||||
posts: [
|
||||
{
|
||||
id: 'id1',
|
||||
mobiledoc: '{"version":"0.3.1","atoms":[],"cards":[["image",{"src":"https://mysite.com/content/images/2019/02/image.jpg"}]]}'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
serializers.input.posts.edit(apiConfig, frame);
|
||||
|
||||
let postData = frame.data.posts[0];
|
||||
postData.mobiledoc.should.equal('{"version":"0.3.1","atoms":[],"cards":[["image",{"src":"/content/images/2019/02/image.jpg"}]]}');
|
||||
});
|
||||
|
||||
it('when mobiledoc contains multiple absolute URLs to images with different protocols', function () {
|
||||
configUtils.set({url: 'https://mysite.com'});
|
||||
const apiConfig = {};
|
||||
const frame = {
|
||||
options: {
|
||||
context: {
|
||||
user: 0,
|
||||
api_key: {
|
||||
id: 1,
|
||||
type: 'content'
|
||||
},
|
||||
},
|
||||
},
|
||||
data: {
|
||||
posts: [
|
||||
{
|
||||
id: 'id1',
|
||||
mobiledoc: '{"version":"0.3.1","atoms":[],"cards":[["image",{"src":"https://mysite.com/content/images/2019/02/image.jpg"}],["image",{"src":"http://mysite.com/content/images/2019/02/image.png"}]]'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
serializers.input.posts.edit(apiConfig, frame);
|
||||
|
||||
let postData = frame.data.posts[0];
|
||||
postData.mobiledoc.should.equal('{"version":"0.3.1","atoms":[],"cards":[["image",{"src":"/content/images/2019/02/image.jpg"}],["image",{"src":"/content/images/2019/02/image.png"}]]');
|
||||
});
|
||||
|
||||
it('when blog url is without subdir', function () {
|
||||
configUtils.set({url: 'https://mysite.com'});
|
||||
const apiConfig = {};
|
||||
|
|
Loading…
Add table
Reference in a new issue