2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00

Moved make-absolute-urls to url service

refs #9178

- this util uses the url services (!)
- moving this file into lib would not make sense right now
  - that would mean a module requires first ../lib/url, which then requires ../services/url
- the url service definitely need a clean up 😃
This commit is contained in:
kirrg001 2017-12-14 22:32:34 +01:00
parent 64626dedd1
commit 991ccb1d35
6 changed files with 113 additions and 120 deletions

View file

@ -15,7 +15,6 @@ var Promise = require('bluebird'),
logging = proxy.logging,
i18n = proxy.i18n,
errors = proxy.errors,
makeAbsoluteUrl = require('../../../../utils/make-absolute-urls'),
urlService = require('../../../../services/url'),
amperizeCache = {},
allowedAMPTags = [],
@ -126,7 +125,7 @@ function getAmperizeHTML(html, post) {
amperize = amperize || new Amperize();
// make relative URLs abolute
html = makeAbsoluteUrl(html, urlService.utils.urlFor('home', true), post.url).html();
html = urlService.utils.makeAbsoluteUrls(html, urlService.utils.urlFor('home', true), post.url).html();
if (!amperizeCache[post.id] || moment(new Date(amperizeCache[post.id].updated_at)).diff(new Date(post.updated_at)) < 0) {
return new Promise(function (resolve) {

View file

@ -2,8 +2,6 @@ var downsize = require('downsize'),
RSS = require('rss'),
urlService = require('../../services/url'),
filters = require('../../filters'),
processUrls = require('../../utils/make-absolute-urls'),
generateFeed,
generateItem,
generateTags;
@ -23,7 +21,7 @@ generateTags = function generateTags(data) {
generateItem = function generateItem(post, siteUrl, secure) {
var itemUrl = urlService.utils.urlFor('post', {post: post, secure: secure}, true),
htmlContent = processUrls(post.html, siteUrl, itemUrl),
htmlContent = urlService.utils.makeAbsoluteUrls(post.html, siteUrl, itemUrl),
item = {
title: post.title,
// @TODO: DRY this up with data/meta/index & other excerpt code

View file

@ -1,9 +1,11 @@
'use strict';
// Contains all path information to be used throughout the codebase.
// Assumes that config.url is set, and is valid
var moment = require('moment-timezone'),
const moment = require('moment-timezone'),
_ = require('lodash'),
url = require('url'),
cheerio = require('cheerio'),
config = require('../../config'),
settingsCache = require('../settings/cache'),
// @TODO: unify this with the path in server/app.js
@ -365,6 +367,64 @@ function redirectToAdmin(status, res, adminPath) {
return res.redirect(redirectUrl);
}
/**
* Make absolute URLs
* @param {string} html
* @param {string} siteUrl (blog URL)
* @param {string} itemUrl (URL of current context)
* @returns {object} htmlContent
* @description Takes html, blog url and item url and converts relative url into
* absolute urls. Returns an object. The html string can be accessed by calling `html()` on
* the variable that takes the result of this function
*/
function makeAbsoluteUrls(html, siteUrl, itemUrl) {
var htmlContent = cheerio.load(html, {decodeEntities: false});
// convert relative resource urls to absolute
['href', 'src'].forEach(function forEach(attributeName) {
htmlContent('[' + attributeName + ']').each(function each(ix, el) {
var baseUrl,
attributeValue,
parsed;
el = htmlContent(el);
attributeValue = el.attr(attributeName);
// if URL is absolute move on to the next element
try {
parsed = url.parse(attributeValue);
if (parsed.protocol) {
return;
}
// Do not convert protocol relative URLs
if (attributeValue.lastIndexOf('//', 0) === 0) {
return;
}
} catch (e) {
return;
}
// CASE: don't convert internal links
if (attributeValue[0] === '#') {
return;
}
// compose an absolute URL
// if the relative URL begins with a '/' use the blog URL (including sub-directory)
// as the base URL, otherwise use the post's URL.
baseUrl = attributeValue[0] === '/' ? siteUrl : itemUrl;
attributeValue = urlJoin(baseUrl, attributeValue);
el.attr(attributeName, attributeValue);
});
});
return htmlContent;
}
module.exports.makeAbsoluteUrls = makeAbsoluteUrls;
module.exports.getProtectedSlugs = getProtectedSlugs;
module.exports.getSubdir = getSubdir;
module.exports.urlJoin = urlJoin;

View file

@ -1,61 +0,0 @@
var cheerio = require('cheerio'),
url = require('url'),
urlService = require('../services/url');
/**
* Make absolute URLs
* @param {string} html
* @param {string} siteUrl (blog URL)
* @param {string} itemUrl (URL of current context)
* @returns {object} htmlContent
* @description Takes html, blog url and item url and converts relative url into
* absolute urls. Returns an object. The html string can be accessed by calling `html()` on
* the variable that takes the result of this function
*/
function makeAbsoluteUrls(html, siteUrl, itemUrl) {
var htmlContent = cheerio.load(html, {decodeEntities: false});
// convert relative resource urls to absolute
['href', 'src'].forEach(function forEach(attributeName) {
htmlContent('[' + attributeName + ']').each(function each(ix, el) {
var baseUrl,
attributeValue,
parsed;
el = htmlContent(el);
attributeValue = el.attr(attributeName);
// if URL is absolute move on to the next element
try {
parsed = url.parse(attributeValue);
if (parsed.protocol) {
return;
}
// Do not convert protocol relative URLs
if (attributeValue.lastIndexOf('//', 0) === 0) {
return;
}
} catch (e) {
return;
}
// CASE: don't convert internal links
if (attributeValue[0] === '#') {
return;
}
// compose an absolute URL
// if the relative URL begins with a '/' use the blog URL (including sub-directory)
// as the base URL, otherwise use the post's URL.
baseUrl = attributeValue[0] === '/' ? siteUrl : itemUrl;
attributeValue = urlService.utils.urlJoin(baseUrl, attributeValue);
el.attr(attributeName, attributeValue);
});
});
return htmlContent;
}
module.exports = makeAbsoluteUrls;

View file

@ -3,11 +3,11 @@ var should = require('should'),
sinon = require('sinon'),
_ = require('lodash'),
moment = require('moment-timezone'),
urlService = require('../../../server/services/url'),
constants = require('../../../server/lib/constants'),
settingsCache = require('../../../server/services/settings/cache'),
configUtils = require('../../utils/configUtils'),
testUtils = require('../../utils'),
urlService = require('../../../../server/services/url'),
constants = require('../../../../server/lib/constants'),
settingsCache = require('../../../../server/services/settings/cache'),
configUtils = require('../../../utils/configUtils'),
testUtils = require('../../../utils'),
config = configUtils.config,
sandbox = sinon.sandbox.create();
@ -721,4 +721,48 @@ describe('Url', function () {
urlService.utils.redirectToAdmin(302, res, '#/my/awesome/path');
});
});
describe('make absolute urls ', function () {
var siteUrl = 'http://my-ghost-blog.com',
itemUrl = 'my-awesome-post';
beforeEach(function () {
configUtils.set({url: 'http://my-ghost-blog.com'});
});
afterEach(function () {
configUtils.restore();
});
it('[success] does not convert absolute URLs', function () {
var html = '<a href="http://my-ghost-blog.com/content/images" title="Absolute URL">',
result = urlService.utils.makeAbsoluteUrls(html, siteUrl, itemUrl).html();
result.should.match(/<a href="http:\/\/my-ghost-blog.com\/content\/images" title="Absolute URL">/);
});
it('[failure] does not convert protocol relative `//` URLs', function () {
var html = '<a href="//my-ghost-blog.com/content/images" title="Absolute URL">',
result = urlService.utils.makeAbsoluteUrls(html, siteUrl, itemUrl).html();
result.should.match(/<a href="\/\/my-ghost-blog.com\/content\/images" title="Absolute URL">/);
});
it('[failure] does not convert internal links starting with "#"', function () {
var html = '<a href="#jumptosection" title="Table of Content">',
result = urlService.utils.makeAbsoluteUrls(html, siteUrl, itemUrl).html();
result.should.match(/<a href="#jumptosection" title="Table of Content">/);
});
it('[success] converts a relative URL', function () {
var html = '<a href="/about#nowhere" title="Relative URL">',
result = urlService.utils.makeAbsoluteUrls(html, siteUrl, itemUrl).html();
result.should.match(/<a href="http:\/\/my-ghost-blog.com\/about#nowhere" title="Relative URL">/);
});
it('[success] converts a relative URL including subdirectories', function () {
var html = '<a href="/about#nowhere" title="Relative URL">',
result = urlService.utils.makeAbsoluteUrls(html, 'http://my-ghost-blog.com/blog', itemUrl).html();
result.should.match(/<a href="http:\/\/my-ghost-blog.com\/blog\/about#nowhere" title="Relative URL">/);
});
});
});

View file

@ -1,47 +0,0 @@
var should = require('should'), // jshint ignore:line
makeAbsoluteUrls = require('../../../server/utils/make-absolute-urls'),
configUtils = require('../../utils/configUtils');
describe('Make absolute URLs ', function () {
var siteUrl = 'http://my-ghost-blog.com',
itemUrl = 'my-awesome-post';
beforeEach(function () {
configUtils.set({url: 'http://my-ghost-blog.com'});
});
afterEach(function () {
configUtils.restore();
});
it('[success] does not convert absolute URLs', function () {
var html = '<a href="http://my-ghost-blog.com/content/images" title="Absolute URL">',
result = makeAbsoluteUrls(html, siteUrl, itemUrl).html();
result.should.match(/<a href="http:\/\/my-ghost-blog.com\/content\/images" title="Absolute URL">/);
});
it('[failure] does not convert protocol relative `//` URLs', function () {
var html = '<a href="//my-ghost-blog.com/content/images" title="Absolute URL">',
result = makeAbsoluteUrls(html, siteUrl, itemUrl).html();
result.should.match(/<a href="\/\/my-ghost-blog.com\/content\/images" title="Absolute URL">/);
});
it('[failure] does not convert internal links starting with "#"', function () {
var html = '<a href="#jumptosection" title="Table of Content">',
result = makeAbsoluteUrls(html, siteUrl, itemUrl).html();
result.should.match(/<a href="#jumptosection" title="Table of Content">/);
});
it('[success] converts a relative URL', function () {
var html = '<a href="/about#nowhere" title="Relative URL">',
result = makeAbsoluteUrls(html, siteUrl, itemUrl).html();
result.should.match(/<a href="http:\/\/my-ghost-blog.com\/about#nowhere" title="Relative URL">/);
});
it('[success] converts a relative URL including subdirectories', function () {
var html = '<a href="/about#nowhere" title="Relative URL">',
result = makeAbsoluteUrls(html, 'http://my-ghost-blog.com/blog', itemUrl).html();
result.should.match(/<a href="http:\/\/my-ghost-blog.com\/blog\/about#nowhere" title="Relative URL">/);
});
});