Skip to content

Commit

Permalink
fix(*): truncate autocomplete names and use central constant
Browse files Browse the repository at this point in the history
  • Loading branch information
almostSouji committed Dec 17, 2024
1 parent 67727fa commit 4269150
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/functions/autocomplete/algoliaAutoComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Response } from 'polka';
import { fetch } from 'undici';
import type { AlgoliaHit, AlgoliaSearchResult } from '../../types/algolia.js';
import { compactAlgoliaObjectId } from '../../util/compactAlgoliaId.js';
import { API_BASE_ALGOLIA, AUTOCOMPLETE_MAX_ITEMS } from '../../util/constants.js';
import { API_BASE_ALGOLIA, AUTOCOMPLETE_MAX_ITEMS, AUTOCOMPLETE_MAX_NAME_LENGTH } from '../../util/constants.js';
import { dedupeAlgoliaHits } from '../../util/dedupe.js';
import { prepareHeader } from '../../util/respond.js';
import { truncate } from '../../util/truncate.js';
Expand Down Expand Up @@ -54,10 +54,10 @@ function autoCompleteMap(elements: AlgoliaHit[]) {
.filter((element) => {
const value = compactAlgoliaObjectId(element.objectID);
// API restriction. Cannot resolve from truncated, so filtering here.
return value.length <= 100;
return value.length <= AUTOCOMPLETE_MAX_NAME_LENGTH;
})
.map((element) => ({
name: truncate(resolveHitToNamestring(element), 100, ''),
name: truncate(resolveHitToNamestring(element), AUTOCOMPLETE_MAX_NAME_LENGTH, ''),
value: compactAlgoliaObjectId(element.objectID),
}));
}
Expand Down
10 changes: 7 additions & 3 deletions src/functions/autocomplete/docsAutoComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
} from 'discord-api-types/v10';
import { ApplicationCommandOptionType, InteractionResponseType } from 'discord-api-types/v10';
import type { Response } from 'polka';
import { AUTOCOMPLETE_MAX_ITEMS, DJS_QUERY_SEPARATOR } from '../../util/constants.js';
import { AUTOCOMPLETE_MAX_ITEMS, AUTOCOMPLETE_MAX_NAME_LENGTH, DJS_QUERY_SEPARATOR } from '../../util/constants.js';
import { getCurrentMainPackageVersion, getDjsVersions } from '../../util/djsdocs.js';
import { truncate } from '../../util/truncate.js';

Expand Down Expand Up @@ -169,7 +169,7 @@ export async function djsMeiliSearch(query: string, version: string) {
});

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

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

return {
...hit,
autoCompleteName: truncate(`${hit.name}${hit.summary ? ` - ${sanitizeText(hit.summary)}` : ''}`, 100, ' '),
autoCompleteName: truncate(
`${hit.name}${hit.summary ? ` - ${sanitizeText(hit.summary)}` : ''}`,
AUTOCOMPLETE_MAX_NAME_LENGTH,
' ',
),
autoCompleteValue: parts.join(DJS_QUERY_SEPARATOR),
isMember,
};
Expand Down
6 changes: 3 additions & 3 deletions src/functions/autocomplete/nodeAutoComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { stringify } from 'node:querystring';
import { InteractionResponseType } from 'discord-api-types/v10';
import type { Response } from 'polka';
import { fetch } from 'undici';
import { API_BASE_ORAMA, AUTOCOMPLETE_MAX_ITEMS } from '../../util/constants.js';
import { API_BASE_ORAMA, AUTOCOMPLETE_MAX_ITEMS, AUTOCOMPLETE_MAX_NAME_LENGTH } from '../../util/constants.js';
import { prepareHeader } from '../../util/respond.js';
import { truncate } from '../../util/truncate.js';

Expand Down Expand Up @@ -32,9 +32,9 @@ function autoCompleteMap(elements: OramaDocument[]) {
return elements.map((element) => {
const cleanSectionTitle = element.pageSectionTitle.replaceAll('`', '');
const name = truncate(`${element.pageTitle} > ${cleanSectionTitle}`, 90, '');
if (element.path.length > 100) {
if (element.path.length > AUTOCOMPLETE_MAX_NAME_LENGTH) {
return {
name: truncate(`[path too long] ${element.pageTitle} > ${cleanSectionTitle}`, 100, ''),
name: truncate(`[path too long] ${element.pageTitle} > ${cleanSectionTitle}`, AUTOCOMPLETE_MAX_NAME_LENGTH, ''),
value: element.pageTitle,
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/functions/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { Response } from 'polka';
import TurndownService from 'turndown';
import { fetch } from 'undici';
import type { NodeDocs } from '../types/NodeDocs.js';
import { API_BASE_NODE, EMOJI_ID_NODE } from '../util/constants.js';
import { API_BASE_NODE, AUTOCOMPLETE_MAX_NAME_LENGTH, EMOJI_ID_NODE } from '../util/constants.js';
import { logger } from '../util/logger.js';
import { prepareErrorResponse, prepareResponse } from '../util/respond.js';
import { truncate } from '../util/truncate.js';
Expand Down Expand Up @@ -104,7 +104,7 @@ export async function nodeAutoCompleteResolve(res: Response, query: string, user

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

const contentParts = [
`<:node:${EMOJI_ID_NODE}> ${hyperlink(inlineCode(headingCode.length ? headingCode : heading), url.toString())}`,
Expand Down
1 change: 1 addition & 0 deletions src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const API_BASE_ALGOLIA = 'algolia.net' as const;
export const API_BASE_DISCORD = 'https://discord.com/api/v9' as const;
export const API_BASE_ORAMA = 'https://cloud.orama.run/v1' as const;
export const AUTOCOMPLETE_MAX_ITEMS = 25;
export const AUTOCOMPLETE_MAX_NAME_LENGTH = 100;
export const MAX_MESSAGE_LENGTH = 4_000;
export const REMOTE_TAG_URL = 'https://raw.githubusercontent.com/discordjs/discord-utils-bot/main/tags' as const;
export const WEBSITE_URL_ROOT = 'https://discordjs.dev';
Expand Down

0 comments on commit 4269150

Please sign in to comment.