From 9fc9e4311dc41d7ca107a471e44cb78a31c45fa6 Mon Sep 17 00:00:00 2001 From: Rishabh Date: Fri, 13 Jan 2023 15:38:54 +0530 Subject: [PATCH] Removed www prefix from newsletter link table closes https://github.com/TryGhost/Team/issues/2206 - removes `www.` from the url shown on links table in post analytics - we had previously removed http(s) protocol from it as well, and they are only shown while editing the url --- ghost/admin/app/services/utils.js | 8 +++--- ghost/admin/tests/unit/services/utils-test.js | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 ghost/admin/tests/unit/services/utils-test.js diff --git a/ghost/admin/app/services/utils.js b/ghost/admin/app/services/utils.js index 947644dcef..a173c38002 100644 --- a/ghost/admin/app/services/utils.js +++ b/ghost/admin/app/services/utils.js @@ -16,9 +16,9 @@ export default class UtilsService extends Service { /** * Remove tracking parameters from a URL - * @param {string} url + * @param {string} url * @param {boolean} [display] Set to true to remove protocol and hash from the URL - * @returns + * @returns */ cleanTrackedUrl(url, display = false) { // Remove our own querystring parameters and protocol @@ -31,6 +31,8 @@ export default class UtilsService extends Service { return urlObj.toString(); } // Return URL without protocol - return urlObj.host + (urlObj.pathname === '/' && !urlObj.search ? '' : urlObj.pathname) + (urlObj.search ? urlObj.search : '') + (urlObj.hash ? urlObj.hash : ''); + const urlWithoutProtocol = urlObj.host + (urlObj.pathname === '/' && !urlObj.search ? '' : urlObj.pathname) + (urlObj.search ? urlObj.search : '') + (urlObj.hash ? urlObj.hash : ''); + // remove www. from the start of the URL + return urlWithoutProtocol.replace(/^www\./, ''); } } diff --git a/ghost/admin/tests/unit/services/utils-test.js b/ghost/admin/tests/unit/services/utils-test.js new file mode 100644 index 0000000000..28840e5de0 --- /dev/null +++ b/ghost/admin/tests/unit/services/utils-test.js @@ -0,0 +1,25 @@ +import {describe, it} from 'mocha'; import {expect} from 'chai'; +import {setupTest} from 'ember-mocha'; + +describe('Unit: Service: utils', function () { + setupTest(); + + describe('cleanTrackedUrl', function () { + let utilsService; + beforeEach(function () { + utilsService = this.owner.lookup('service:utils'); + }); + + it('removes protocol and www from url if display is true', function () { + const url = 'https://www.ghost.org'; + const output = utilsService.cleanTrackedUrl(url, true); + expect(output).to.be('ghost.org'); + }); + + it('removes tracking params from the url', function () { + const url = 'https://www.ghost.org?ref=123&attribution_id=something&attribution_type=something&leave=123'; + const output = utilsService.cleanTrackedUrl(url, false); + expect(output).to.be('https://www.ghost.org?leave=123'); + }); + }); +});