@@ -183,6 +183,33 @@ function slugify(text: string): string {
183183 . replace ( / ^ - | - $ / g, '' ) // Trim leading/trailing hyphens
184184}
185185
186+ /** These path on npmjs.com don't belong to packages or search, so we shouldn't try to replace them with npmx.dev urls */
187+ const reservedPathsNpmJs = [
188+ 'products' ,
189+ 'login' ,
190+ 'signup' ,
191+ // 'advisories',
192+ 'blog' ,
193+ 'about' ,
194+ 'press' ,
195+ 'policies' ,
196+ ]
197+
198+ const isNpmJsUrlThatCanBeRedirected = ( url : URL ) => {
199+ if ( url . host !== 'www.npmjs.com' && url . host !== 'npmjs.com' ) {
200+ return false
201+ }
202+
203+ if (
204+ url . pathname === '/' ||
205+ reservedPathsNpmJs . some ( path => url . pathname . startsWith ( `/${ path } ` ) )
206+ ) {
207+ return false
208+ }
209+
210+ return true
211+ }
212+
186213/**
187214 * Resolve a relative URL to an absolute URL.
188215 * If repository info is available, resolve to provider's raw file URLs.
@@ -199,6 +226,11 @@ function resolveUrl(url: string, packageName: string, repoInfo?: RepositoryInfo)
199226 try {
200227 const parsed = new URL ( url , 'https://example.com' )
201228 if ( parsed . protocol === 'http:' || parsed . protocol === 'https:' ) {
229+ // Redirect npmjs urls to ourself
230+ console . log ( { isnpm : isNpmJsUrlThatCanBeRedirected ( parsed ) } )
231+ if ( isNpmJsUrlThatCanBeRedirected ( parsed ) ) {
232+ return parsed . pathname + parsed . search + parsed . hash
233+ }
202234 return url
203235 }
204236 } catch {
@@ -362,6 +394,7 @@ ${html}
362394 // Resolve link URLs, add security attributes, and collect playground links
363395 renderer . link = function ( { href, title, tokens } : Tokens . Link ) {
364396 const resolvedHref = resolveUrl ( href , packageName , repoInfo )
397+ console . log ( { resolvedHref } )
365398 const text = this . parser . parseInline ( tokens )
366399 const titleAttr = title ? ` title="${ title } "` : ''
367400
0 commit comments