Skip to content

Commit f7808dd

Browse files
committed
fix(*): line-based truncation
truncate based on full lines to avoid cutting into unresolved markdown highlights or other elements
1 parent 60c724f commit f7808dd

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/functions/algoliaResponse.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { AlgoliaHit } from '../types/algolia.js';
66
import { expandAlgoliaObjectId } from '../util/compactAlgoliaId.js';
77
import { API_BASE_ALGOLIA } from '../util/constants.js';
88
import { fetchDocsBody } from '../util/discordDocs.js';
9+
import { noCodeLines } from '../util/djsguide.js';
910
import { prepareResponse, prepareErrorResponse } from '../util/respond.js';
1011
import { truncate } from '../util/truncate.js';
1112
import { resolveHitToNamestring } from './autocomplete/algoliaAutoComplete.js';
@@ -45,9 +46,28 @@ export async function algoliaResponse(
4546

4647
const contentParts = [
4748
`<:${emojiName}:${emojiId}> ${bold(resolveHitToNamestring(hit))}${headlineSuffix ? ` ${headlineSuffix}` : ''}`,
48-
hit.content?.length ? `${truncate(decode(hit.content), 300)}` : docsSection?.lines.at(0),
49-
`${hyperlink('read more', hideLinkEmbed(hit.url))}`,
50-
].filter(Boolean) as string[];
49+
];
50+
51+
if (hit.content?.length) {
52+
contentParts.push(`${truncate(decode(hit.content), 300)}`);
53+
} else {
54+
const descriptionParts = [];
55+
let descriptionLength = 0;
56+
const relevantLines = noCodeLines(docsSection?.lines ?? []);
57+
58+
if (relevantLines.length) {
59+
for (const line of relevantLines) {
60+
if (descriptionLength + line.length < 500) {
61+
descriptionParts.push(line);
62+
descriptionLength += line.length;
63+
}
64+
}
65+
66+
contentParts.push(descriptionParts.join(' '));
67+
}
68+
}
69+
70+
contentParts.push(`${hyperlink('read more', hideLinkEmbed(hit.url))}`);
5171

5272
prepareResponse(res, contentParts.join('\n'), {
5373
ephemeral,

src/functions/oramaResponse.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,17 @@ export async function oramaResponse(res: Response, resultUrl: string, user?: str
6464

6565
const relevantLines = noCodeLines(section?.lines ?? []);
6666
if (relevantLines.length) {
67-
contentParts.push(truncate(relevantLines.join(' '), 300));
67+
const descriptionParts = [];
68+
let descriptionLength = 0;
69+
70+
for (const line of relevantLines) {
71+
if (descriptionLength + line.length < 500) {
72+
descriptionParts.push(line);
73+
descriptionLength += line.length;
74+
}
75+
}
76+
77+
contentParts.push(descriptionParts.join(' '));
6878
}
6979

7080
contentParts.push(hyperlink('read more', parsed.guideUrl));

0 commit comments

Comments
 (0)