core/server/services/url/utils.js
function makeAbsoluteUrls(html, siteUrl, itemUrl, options = {assetsOnly: false}) {
    html = html || '';
    const htmlContent = cheerio.load(html, {decodeEntities: false});
    const staticImageUrlPrefixRegex = new RegExp(STATIC_IMAGE_URL_PREFIX);

    // convert relative resource urls to absolute
    ['href', 'src'].forEach(function forEach(attributeName) {
        htmlContent('[' + attributeName + ']').each(function each(ix, el) {
            el = htmlContent(el);

            let attributeValue = el.attr(attributeName);

            // if URL is absolute move on to the next element
            try {
                const 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;
            }

            if (options.assetsOnly && !attributeValue.match(staticImageUrlPrefixRegex)) {
                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.
            const baseUrl = attributeValue[0] === '/' ? siteUrl : itemUrl;
            attributeValue = urlJoin(baseUrl, attributeValue);
            el.attr(attributeName, attributeValue);
        });
    });

    return htmlContent;
}
function makeAbsoluteUrls(html, siteUrl, itemUrl, options = {assetsOnly: false}) {
    html = html || '';
    const htmlContent = cheerio.load(html, {decodeEntities: false});
    const staticImageUrlPrefixRegex = new RegExp(STATIC_IMAGE_URL_PREFIX);

    // convert relative resource urls to absolute
    ['href', 'src'].forEach(function forEach(attributeName) {
        htmlContent('[' + attributeName + ']').each(function each(ix, el) {
            el = htmlContent(el);

            let attributeValue = el.attr(attributeName);

            // if URL is absolute move on to the next element
            try {
                const 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;
            }

            if (options.assetsOnly && !attributeValue.match(staticImageUrlPrefixRegex)) {
                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.
            const baseUrl = attributeValue[0] === '/' ? siteUrl : itemUrl;
            attributeValue = urlJoin(baseUrl, attributeValue);
            el.attr(attributeName, attributeValue);
        });
    });

    return htmlContent;
}
function makeAbsoluteUrls ( html , siteUrl , itemUrl , options = { assetsOnly : false } ) { html = html || '' ; const htmlContent = cheerio . load ( html , { decodeEntities : false } ) ; const staticImageUrlPrefixRegex = new RegExp ( STATIC_IMAGE_URL_PREFIX ) ; // convert relative resource urls to absolute [ 'href' , 'src' ] . forEach ( function forEach ( attributeName ) { htmlContent ( '[' + attributeName + ']' ) . each ( function each ( ix , el ) { el = htmlContent ( el ) ; let attributeValue = el . attr ( attributeName ) ; // if URL is absolute move on to the next element try { const 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 ; } if ( options . assetsOnly && ! attributeValue . match ( staticImageUrlPrefixRegex ) ) { 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. const baseUrl = attributeValue [ 0 ] === '/' ? siteUrl : itemUrl ; attributeValue = urlJoin ( baseUrl , attributeValue ) ; el . attr ( attributeName , attributeValue ) ; } ) ; } ) ; return htmlContent ; }
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
Make absolute URLs