Skip to content

Commit 466843f

Browse files
committed
fix(*): better defaulting for page matches
* default for the first section, if the query is not an anchor, but an endpoint (relevant for guide, mostly) * truncate descriptions with ellipsis, if the sentence was not properly terminated
1 parent f7808dd commit 466843f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/functions/oramaResponse.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { EMOJI_ID_GUIDE } from '../util/constants.js';
44
import { findRelevantDocsSection } from '../util/discordDocs.js';
55
import { noCodeLines, resolveResourceFromGuideUrl } from '../util/djsguide.js';
66
import { prepareErrorResponse, prepareResponse } from '../util/respond.js';
7-
import { truncate } from '../util/truncate.js';
87

98
type GuideCacheEntry = {
109
page: string;
@@ -55,7 +54,7 @@ export async function oramaResponse(res: Response, resultUrl: string, user?: str
5554
return res;
5655
}
5756

58-
const section = findRelevantDocsSection(`#${parsed.anchor ?? parsed.endpoint}`, docsContents);
57+
const section = findRelevantDocsSection(`#${parsed.anchor ?? parsed.endpoint}`, docsContents, !parsed.anchor);
5958

6059
if (section) {
6160
const title = section.headline ?? parsed.endpoint ?? 'No Title';
@@ -74,7 +73,13 @@ export async function oramaResponse(res: Response, resultUrl: string, user?: str
7473
}
7574
}
7675

77-
contentParts.push(descriptionParts.join(' '));
76+
const description = descriptionParts.join(' ');
77+
const isTerminated = ['.', '?', '!'].some((terminator) => description.endsWith(terminator));
78+
if (isTerminated) {
79+
contentParts.push(description);
80+
} else {
81+
contentParts.push(`${description}...`);
82+
}
7883
}
7984

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

src/util/discordDocs.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export function parseSections(content: string): ParsedSection[] {
127127
return res;
128128
}
129129

130-
export function findRelevantDocsSection(query: string, docsMd: string) {
130+
export function findRelevantDocsSection(query: string, docsMd: string, defaultFirst = false) {
131131
const sections = parseSections(docsMd);
132132

133133
for (const section of sections) {
@@ -137,6 +137,10 @@ export function findRelevantDocsSection(query: string, docsMd: string) {
137137
return section;
138138
}
139139
}
140+
141+
if (defaultFirst && sections.length > 0) {
142+
return sections.at(0)!;
143+
}
140144
}
141145

142146
export async function fetchDocsBody(link: string) {

0 commit comments

Comments
 (0)