From d24ea3ce55992786375c01469f5a09d46c9995ea Mon Sep 17 00:00:00 2001 From: Rishabh Date: Thu, 7 Jul 2022 11:09:53 +0200 Subject: [PATCH] Updated script url to use admin url refs https://github.com/TryGhost/Team/issues/1665 - replaces usage of site url to admin url as script only needs admin url for setup - updates tests --- ghost/sodo-search/src/App.js | 4 ++-- ghost/sodo-search/src/App.test.js | 2 +- ghost/sodo-search/src/index.js | 14 +++++++------- ghost/sodo-search/src/search-index.js | 4 ++-- ghost/sodo-search/src/search-index.test.js | 12 ++++++------ 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/ghost/sodo-search/src/App.js b/ghost/sodo-search/src/App.js index a618e026e3..e63a44c4c8 100644 --- a/ghost/sodo-search/src/App.js +++ b/ghost/sodo-search/src/App.js @@ -9,7 +9,7 @@ export default class App extends React.Component { super(props); const searchIndex = new SearchIndex({ - siteURL: props.siteURL, + adminUrl: props.adminUrl, apiKey: props.apiKey }); @@ -98,7 +98,7 @@ export default class App extends React.Component { { window.location.hash = '#/search'; - render(); + render(); // const containerElement = screen.getElementsByClassName('gh-portal-popup-container'); const containerElement = document.querySelector('.gh-root-frame'); expect(containerElement).toBeInTheDocument(); diff --git a/ghost/sodo-search/src/index.js b/ghost/sodo-search/src/index.js index 180532e02f..977a3daa86 100644 --- a/ghost/sodo-search/src/index.js +++ b/ghost/sodo-search/src/index.js @@ -17,26 +17,26 @@ function getSiteData() { */ const scriptTag = document.querySelector('script[data-sodo-search]'); if (scriptTag) { - const siteUrl = scriptTag.dataset.sodoSearch; + const adminUrl = scriptTag.dataset.sodoSearch; const apiKey = scriptTag.dataset.key; const appVersion = scriptTag.dataset.version; - return {siteUrl, apiKey, appVersion}; + return {adminUrl, apiKey, appVersion}; } return {}; } -function setup({siteUrl}) { +function setup() { addRootDiv(); } function init() { - const {siteUrl: customSiteUrl, apiKey, apiUrl, appVersion} = getSiteData(); - const siteUrl = customSiteUrl || window.location.origin; - setup({siteUrl}); + const {adminUrl, apiKey, appVersion} = getSiteData(); + const adminBaseUrl = (adminUrl || window.location.origin)?.replace(/\/+$/, ''); + setup(); ReactDOM.render( , diff --git a/ghost/sodo-search/src/search-index.js b/ghost/sodo-search/src/search-index.js index c4e4ad6ba5..ade0f0d043 100644 --- a/ghost/sodo-search/src/search-index.js +++ b/ghost/sodo-search/src/search-index.js @@ -2,9 +2,9 @@ import elasticlunr from 'elasticlunr'; import GhostContentAPI from '@tryghost/content-api'; export default class SearchIndex { - constructor({siteURL, apiKey}) { + constructor({adminUrl, apiKey}) { this.api = new GhostContentAPI({ - url: siteURL, + url: adminUrl, key: apiKey, version: 'v5.0' }); diff --git a/ghost/sodo-search/src/search-index.test.js b/ghost/sodo-search/src/search-index.test.js index 3b50195407..c835d66201 100644 --- a/ghost/sodo-search/src/search-index.test.js +++ b/ghost/sodo-search/src/search-index.test.js @@ -3,9 +3,9 @@ import nock from 'nock'; describe('search index', function () { test('initializes search index', async () => { - const siteURL = 'http://localhost'; + const adminUrl = 'http://localhost'; const apiKey = '69010382388f9de5869ad6e558'; - const searchIndex = new SearchIndex({siteURL, apiKey, storage: localStorage}); + const searchIndex = new SearchIndex({adminUrl, apiKey, storage: localStorage}); const scope = nock('http://localhost/ghost/api/content') .get('/posts/?key=69010382388f9de5869ad6e558&limit=all&fields=id%2Cslug%2Ctitle%2Cexcerpt%2Curl%2Cupdated_at%2Cvisibility&order=updated_at%20DESC&formats=plaintext') @@ -21,7 +21,7 @@ describe('search index', function () { tags: [] }); - await searchIndex.init({siteURL, apiKey}); + await searchIndex.init(); expect(scope.isDone()).toBeTruthy(); @@ -33,9 +33,9 @@ describe('search index', function () { }); test('allows to search for indexed posts and authors', async () => { - const siteURL = 'http://localhost'; + const adminUrl = 'http://localhost'; const apiKey = '69010382388f9de5869ad6e558'; - const searchIndex = new SearchIndex({siteURL, apiKey, storage: localStorage}); + const searchIndex = new SearchIndex({adminUrl, apiKey, storage: localStorage}); nock('http://localhost/ghost/api/content') .get('/posts/?key=69010382388f9de5869ad6e558&limit=all&fields=id%2Cslug%2Ctitle%2Cexcerpt%2Curl%2Cupdated_at%2Cvisibility&order=updated_at%20DESC&formats=plaintext') @@ -73,7 +73,7 @@ describe('search index', function () { }] }); - await searchIndex.init({siteURL, apiKey}); + await searchIndex.init(); let searchResults = searchIndex.search('Barcelona'); expect(searchResults.posts.length).toEqual(1);