@@ -6,9 +6,14 @@ export const algoliaClient = algoliasearch(
6
6
process . env . ALGOLIA_ADMIN_KEY
7
7
) ;
8
8
9
- export const searchIndexNameMap = {
10
- 'http://localhost:3030' : 'news-es' , // Dockerized Spanish Ghost instance for testing, which is mimicking the old English instance
11
- 'https://chinese.freecodecamp.org/news/' : 'news-zh' ,
9
+ const searchIndexNameMap = {
10
+ // Dockerized Spanish Ghost instance for testing
11
+ 'http://localhost:3030' : 'news-es' ,
12
+ // The Chinese publication is at https://chinese.freecodecamp.org/news/,
13
+ // but this map and the getSearchIndexName function are used after formatting
14
+ // the post for Algolia, which updates necessary URLs for the search record
15
+ // to start with https://www.freecodecamp.org/chinese/news/
16
+ 'https://www.freecodecamp.org/chinese/news/' : 'news-zh' ,
12
17
'https://www.freecodecamp.org/espanol/news/' : 'news-es' ,
13
18
'https://www.freecodecamp.org/italian/news/' : 'news-it' ,
14
19
'https://www.freecodecamp.org/japanese/news/' : 'news-ja' ,
@@ -96,6 +101,30 @@ export const formatHashnodePost = (post) => {
96
101
} ;
97
102
} ;
98
103
104
+ export const getBaseSiteURL = ( url ) => {
105
+ const URLObj = new URL ( url ) ;
106
+ const { host, pathname } = URLObj ;
107
+ const pathParts = pathname . split ( '/' ) . filter ( Boolean ) ;
108
+ let siteLang ;
109
+ if ( host . startsWith ( 'chinese' ) ) {
110
+ siteLang = 'chinese' ;
111
+ } else if ( pathParts . length === 3 ) {
112
+ // Webhooks will only be triggered by posts, so the path will
113
+ // always have 3 parts for our localized instances:
114
+ // (/<lang>/news/<slug>/).
115
+ // Or if it's coming from a Dockerized test instance / localhost,
116
+ // it will only have 1 part: (/<slug>/).
117
+ siteLang = pathParts [ 0 ] ;
118
+ }
119
+ const computedPath = siteLang ? `/${ siteLang } /news/` : '/news/' ;
120
+
121
+ if ( host . startsWith ( 'localhost' ) ) {
122
+ return `http://${ host } ${ computedPath } ` ;
123
+ } else {
124
+ return `https://www.freecodecamp.org${ computedPath } ` ;
125
+ }
126
+ } ;
127
+
99
128
export const formatGhostPost = ( post ) => {
100
129
const {
101
130
id,
@@ -107,22 +136,7 @@ export const formatGhostPost = (post) => {
107
136
feature_image,
108
137
published_at
109
138
} = post ;
110
- const URLObj = new URL ( url ) ;
111
- const { href, origin, pathname } = URLObj ;
112
- const pathParts = pathname . split ( '/' ) . filter ( Boolean ) ;
113
- let siteLang ;
114
- if ( href . startsWith ( 'https://chinese.freecodecamp.org/' ) ) {
115
- siteLang = 'chinese' ;
116
- } else if ( pathParts . length === 3 ) {
117
- // Webhooks will only be triggered by posts, so the path will
118
- // always have 3 parts for our localized instances:
119
- // (/<lang>/news/<slug>/).
120
- // Or if it's coming from a Dockerized test instance, it will
121
- // only have 1 part: (/<slug>/).
122
- siteLang = pathParts [ 0 ] ;
123
- }
124
- const siteURL = `${ origin } /${ siteLang ? `${ siteLang } /` : '' } news/` ;
125
- console . log ( { siteURL, siteLang, href, origin, pathname, pathParts } ) ;
139
+ const siteURL = getBaseSiteURL ( url ) ;
126
140
127
141
return {
128
142
objectID : id ,
0 commit comments