Skip to content

Commit 4269150

Browse files
committed
fix(*): truncate autocomplete names and use central constant
1 parent 67727fa commit 4269150

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

src/functions/autocomplete/algoliaAutoComplete.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Response } from 'polka';
55
import { fetch } from 'undici';
66
import type { AlgoliaHit, AlgoliaSearchResult } from '../../types/algolia.js';
77
import { compactAlgoliaObjectId } from '../../util/compactAlgoliaId.js';
8-
import { API_BASE_ALGOLIA, AUTOCOMPLETE_MAX_ITEMS } from '../../util/constants.js';
8+
import { API_BASE_ALGOLIA, AUTOCOMPLETE_MAX_ITEMS, AUTOCOMPLETE_MAX_NAME_LENGTH } from '../../util/constants.js';
99
import { dedupeAlgoliaHits } from '../../util/dedupe.js';
1010
import { prepareHeader } from '../../util/respond.js';
1111
import { truncate } from '../../util/truncate.js';
@@ -54,10 +54,10 @@ function autoCompleteMap(elements: AlgoliaHit[]) {
5454
.filter((element) => {
5555
const value = compactAlgoliaObjectId(element.objectID);
5656
// API restriction. Cannot resolve from truncated, so filtering here.
57-
return value.length <= 100;
57+
return value.length <= AUTOCOMPLETE_MAX_NAME_LENGTH;
5858
})
5959
.map((element) => ({
60-
name: truncate(resolveHitToNamestring(element), 100, ''),
60+
name: truncate(resolveHitToNamestring(element), AUTOCOMPLETE_MAX_NAME_LENGTH, ''),
6161
value: compactAlgoliaObjectId(element.objectID),
6262
}));
6363
}

src/functions/autocomplete/docsAutoComplete.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
} from 'discord-api-types/v10';
66
import { ApplicationCommandOptionType, InteractionResponseType } from 'discord-api-types/v10';
77
import type { Response } from 'polka';
8-
import { AUTOCOMPLETE_MAX_ITEMS, DJS_QUERY_SEPARATOR } from '../../util/constants.js';
8+
import { AUTOCOMPLETE_MAX_ITEMS, AUTOCOMPLETE_MAX_NAME_LENGTH, DJS_QUERY_SEPARATOR } from '../../util/constants.js';
99
import { getCurrentMainPackageVersion, getDjsVersions } from '../../util/djsdocs.js';
1010
import { truncate } from '../../util/truncate.js';
1111

@@ -169,7 +169,7 @@ export async function djsMeiliSearch(query: string, version: string) {
169169
});
170170

171171
const docsResult = (await searchResult.json()) as any;
172-
const hits = docsResult.results.flatMap((res: any) => res.hits).sort((one: any, other: any) => one.id - other.id);
172+
const hits = docsResult.results.flatMap((res: any) => res.hits);
173173

174174
return {
175175
...docsResult,
@@ -184,7 +184,11 @@ export async function djsMeiliSearch(query: string, version: string) {
184184

185185
return {
186186
...hit,
187-
autoCompleteName: truncate(`${hit.name}${hit.summary ? ` - ${sanitizeText(hit.summary)}` : ''}`, 100, ' '),
187+
autoCompleteName: truncate(
188+
`${hit.name}${hit.summary ? ` - ${sanitizeText(hit.summary)}` : ''}`,
189+
AUTOCOMPLETE_MAX_NAME_LENGTH,
190+
' ',
191+
),
188192
autoCompleteValue: parts.join(DJS_QUERY_SEPARATOR),
189193
isMember,
190194
};

src/functions/autocomplete/nodeAutoComplete.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { stringify } from 'node:querystring';
33
import { InteractionResponseType } from 'discord-api-types/v10';
44
import type { Response } from 'polka';
55
import { fetch } from 'undici';
6-
import { API_BASE_ORAMA, AUTOCOMPLETE_MAX_ITEMS } from '../../util/constants.js';
6+
import { API_BASE_ORAMA, AUTOCOMPLETE_MAX_ITEMS, AUTOCOMPLETE_MAX_NAME_LENGTH } from '../../util/constants.js';
77
import { prepareHeader } from '../../util/respond.js';
88
import { truncate } from '../../util/truncate.js';
99

@@ -32,9 +32,9 @@ function autoCompleteMap(elements: OramaDocument[]) {
3232
return elements.map((element) => {
3333
const cleanSectionTitle = element.pageSectionTitle.replaceAll('`', '');
3434
const name = truncate(`${element.pageTitle} > ${cleanSectionTitle}`, 90, '');
35-
if (element.path.length > 100) {
35+
if (element.path.length > AUTOCOMPLETE_MAX_NAME_LENGTH) {
3636
return {
37-
name: truncate(`[path too long] ${element.pageTitle} > ${cleanSectionTitle}`, 100, ''),
37+
name: truncate(`[path too long] ${element.pageTitle} > ${cleanSectionTitle}`, AUTOCOMPLETE_MAX_NAME_LENGTH, ''),
3838
value: element.pageTitle,
3939
};
4040
}

src/functions/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { Response } from 'polka';
99
import TurndownService from 'turndown';
1010
import { fetch } from 'undici';
1111
import type { NodeDocs } from '../types/NodeDocs.js';
12-
import { API_BASE_NODE, EMOJI_ID_NODE } from '../util/constants.js';
12+
import { API_BASE_NODE, AUTOCOMPLETE_MAX_NAME_LENGTH, EMOJI_ID_NODE } from '../util/constants.js';
1313
import { logger } from '../util/logger.js';
1414
import { prepareErrorResponse, prepareResponse } from '../util/respond.js';
1515
import { truncate } from '../util/truncate.js';
@@ -104,7 +104,7 @@ export async function nodeAutoCompleteResolve(res: Response, query: string, user
104104

105105
const text = paragraph.text().split('\n').join(' ');
106106
const sentence = text.split(/[!.?](\s|$)/)?.[0];
107-
const effectiveSentence = (sentence ?? truncate(text, 100, '')).trim();
107+
const effectiveSentence = (sentence ?? truncate(text, AUTOCOMPLETE_MAX_NAME_LENGTH, '')).trim();
108108

109109
const contentParts = [
110110
`<:node:${EMOJI_ID_NODE}> ${hyperlink(inlineCode(headingCode.length ? headingCode : heading), url.toString())}`,

src/util/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const API_BASE_ALGOLIA = 'algolia.net' as const;
3030
export const API_BASE_DISCORD = 'https://discord.com/api/v9' as const;
3131
export const API_BASE_ORAMA = 'https://cloud.orama.run/v1' as const;
3232
export const AUTOCOMPLETE_MAX_ITEMS = 25;
33+
export const AUTOCOMPLETE_MAX_NAME_LENGTH = 100;
3334
export const MAX_MESSAGE_LENGTH = 4_000;
3435
export const REMOTE_TAG_URL = 'https://raw.githubusercontent.com/discordjs/discord-utils-bot/main/tags' as const;
3536
export const WEBSITE_URL_ROOT = 'https://discordjs.dev';

0 commit comments

Comments
 (0)