@@ -118,12 +118,13 @@ class TcIntegrationLayout extends Component<TcIntegrationLayoutProps> {
118
118
119
119
// if we're not clicking an anchor tag, there's nothing to do
120
120
const eventTarget = event . target as HTMLElement ;
121
- if ( eventTarget ?. localName !== 'a' ) {
121
+ const anchorTag = eventTarget . closest ( 'a' ) ;
122
+ if ( ! anchorTag ) {
122
123
return ;
123
124
}
124
125
125
126
// if the target of the click isn't external, there's nothing to do
126
- const target = eventTarget as HTMLAnchorElement ;
127
+ const target = anchorTag ;
127
128
const url = new URL ( target . href ) ;
128
129
if ( url . host === window . location . host ) {
129
130
return ;
@@ -134,14 +135,31 @@ class TcIntegrationLayout extends Component<TcIntegrationLayoutProps> {
134
135
event . preventDefault ( ) ;
135
136
136
137
// if this is a freecodecamp lesson, change its domain and path
137
- if ( url . host === 'learn. freecodecamp.org') {
138
- url . host = new URL ( document . referrer ) . host ;
138
+ const fccHost = ' freecodecamp.org';
139
+ if ( url . host . endsWith ( fccHost ) ) {
139
140
// TODO: it would be nice to not require that the FCC
140
141
// app knows about the paths in the platform UI, but
141
142
// creating a way to share this info would be complex and
142
143
// time consuming, so we can handle it when we get another
143
144
// provider.
144
- url . pathname = `learn/freecodecamp${ url . pathname } ` ;
145
+
146
+ // set the pathname for the 2 flavors of lesson URL
147
+ const platformPathPrefix = 'learn/freecodecamp' ;
148
+ const learnPrefix = '/learn/' ;
149
+ if ( url . host === `learn.${ fccHost } ` ) {
150
+ url . pathname = `${ platformPathPrefix } ${ url . pathname } ` ;
151
+ } else if (
152
+ url . host === `www.${ fccHost } ` &&
153
+ url . pathname . startsWith ( learnPrefix )
154
+ ) {
155
+ url . pathname = url . pathname . replace (
156
+ learnPrefix ,
157
+ `/${ platformPathPrefix } /`
158
+ ) ;
159
+ }
160
+
161
+ // set the host to the iframe's parent domain
162
+ url . host = new URL ( document . referrer ) . host ;
145
163
}
146
164
147
165
// now open the url in a new tab
0 commit comments