mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 17:32:15 +03:00
7d388cb9e1
closes #9136 Changed the functionality in `make-absolute-urls.js` util to not convert the URL when starting which an `#`, as it indicates and internal link. The util is used inside of the `{{amp_content}}` helper and to render the RSS feed. I tested the changes with the most popular RSS reader 'Feedly' and it seems like these internal links get converted to absolute URL inside of Feedly automatically.
62 lines
2.0 KiB
JavaScript
62 lines
2.0 KiB
JavaScript
var cheerio = require('cheerio'),
|
|
url = require('url'),
|
|
utils = require('../utils');
|
|
|
|
/**
|
|
* 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 = utils.url.urlJoin(baseUrl, attributeValue);
|
|
el.attr(attributeName, attributeValue);
|
|
});
|
|
});
|
|
|
|
return htmlContent;
|
|
}
|
|
|
|
module.exports = makeAbsoluteUrls;
|