Skip to content

Commit f8577f1

Browse files
committed
feat(node): handle non-docs related orama results
1 parent 48a9390 commit f8577f1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/functions/node.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { fetch } from 'undici';
1111
import type { NodeDocs } from '../types/NodeDocs.js';
1212
import { API_BASE_NODE, AUTOCOMPLETE_MAX_NAME_LENGTH, EMOJI_ID_NODE } from '../util/constants.js';
1313
import { logger } from '../util/logger.js';
14+
import { toTitlecase } from '../util/misc.js';
1415
import { prepareErrorResponse, prepareResponse } from '../util/respond.js';
1516
import { truncate } from '../util/truncate.js';
1617
import { 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+
125144
export 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

Comments
 (0)