@@ -11,6 +11,7 @@ import { fetch } from 'undici';
1111import type { NodeDocs } from '../types/NodeDocs.js' ;
1212import { API_BASE_NODE , AUTOCOMPLETE_MAX_NAME_LENGTH , EMOJI_ID_NODE } from '../util/constants.js' ;
1313import { logger } from '../util/logger.js' ;
14+ import { toTitlecase } from '../util/misc.js' ;
1415import { prepareErrorResponse , prepareResponse } from '../util/respond.js' ;
1516import { truncate } from '../util/truncate.js' ;
1617import { urlOption } from '../util/url.js' ;
@@ -122,6 +123,24 @@ export async function nodeAutoCompleteResolve(res: Response, query: string, user
122123 return res ;
123124}
124125
126+ function parsePathToPhrase ( path : string ) {
127+ const [ head , tail ] = path . split ( '#' ) ;
128+ const _headPart = head ?. length ? head . replaceAll ( '-' , ' ' ) : undefined ;
129+ const headPart = _headPart ?. split ( '/' ) . at ( - 1 ) ;
130+ const tailPart = tail ?. length ? tail . replaceAll ( '-' , ' ' ) : undefined ;
131+
132+ const parts : string [ ] = [ ] ;
133+ if ( headPart ) {
134+ parts . push ( toTitlecase ( headPart ) ) ;
135+ }
136+
137+ if ( tailPart ) {
138+ parts . push ( toTitlecase ( tailPart ) ) ;
139+ }
140+
141+ return parts . join ( ' > ' ) ;
142+ }
143+
125144export async function nodeSearch (
126145 res : Response ,
127146 query : string ,
@@ -134,6 +153,18 @@ export async function nodeSearch(
134153 const url = `${ API_BASE_NODE } /dist/${ version } /docs/api/all.json` ;
135154 let allNodeData = jsonCache . get ( url ) ;
136155
156+ if ( ! query . startsWith ( 'docs' ) ) {
157+ prepareResponse (
158+ res ,
159+ `<:node:${ EMOJI_ID_NODE } > ${ bold ( 'Learn more about Node.js:' ) } \n${ hyperlink ( parsePathToPhrase ( trimmedQuery ) , `${ API_BASE_NODE } /en/${ trimmedQuery } ` ) } ` ,
160+ {
161+ ephemeral,
162+ suggestion : user ? { userId : user , kind : 'documentation' } : undefined ,
163+ } ,
164+ ) ;
165+ return res ;
166+ }
167+
137168 if ( ! allNodeData ) {
138169 const data = ( await fetch ( url ) . then ( async ( response ) => response . json ( ) ) ) as NodeDocs ;
139170 jsonCache . set ( url , data ) ;
0 commit comments