Skip to content

Commit 5a4ccf3

Browse files
committed
fix(discorddocs): cover more cases to display docs body lines
1 parent 2729a86 commit 5a4ccf3

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/functions/algoliaResponse.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export async function algoliaResponse(
3737
}).then(async (res) => res.json())) as AlgoliaHit;
3838

3939
const docsBody = hit.url.includes('discord.com') ? await fetchDocsBody(hit.url) : null;
40-
const headlineSuffix = docsBody?.heading ? inlineCode(`${docsBody.heading.verb} ${docsBody.heading.route}`) : null;
40+
const headlineSuffix =
41+
docsBody?.heading?.verb && docsBody?.heading.route
42+
? inlineCode(`${docsBody.heading.verb} ${docsBody.heading.route}`)
43+
: null;
4144

4245
const contentParts = [
4346
target ? `${italic(`Suggestion for ${userMention(target)}:`)}` : null,

src/util/discordDocs.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export function toMdFilename(name: string) {
44
return name
55
.split('-')
66
.map((part) => `${part.at(0)?.toUpperCase()}${part.slice(1).toLowerCase()}`)
7-
.join('');
7+
.join('_');
88
}
99

1010
export function resolveResourceFromDocsURL(link: string) {
@@ -34,17 +34,19 @@ type Heading = {
3434
};
3535

3636
function parseHeadline(text: string): Heading | null {
37-
const match = /#{1,7} (?<label>.*) % (?<verb>\w{3,6}) (?<route>.*)/g.exec(text);
38-
if (!match) {
37+
const match = /#{1,7} (?<label>.+) % (?<verb>\w{3,6}) (?<route>.*)|#{1,7} (?<onlylabel>.+)/g.exec(text);
38+
if (!match?.groups) {
3939
return null;
4040
}
4141

4242
const { groups } = match;
43+
const label = groups.label ?? groups.onlylabel;
44+
4345
return {
44-
docs_anchor: `#${groups!.label.replaceAll(' ', '-').toLowerCase()}`,
45-
label: groups!.label,
46-
verb: groups!.verb,
47-
route: groups!.route,
46+
docs_anchor: `#${label.replaceAll(' ', '-').toLowerCase()}`,
47+
label,
48+
verb: groups.verb,
49+
route: groups.route,
4850
};
4951
}
5052

@@ -108,7 +110,7 @@ export function parseSections(content: string): ParsedSection[] {
108110
export function findRelevantDocsSection(query: string, docsMd: string) {
109111
const sections = parseSections(docsMd);
110112
for (const section of sections) {
111-
if (section.heading?.docs_anchor === query) {
113+
if (section.heading?.docs_anchor.startsWith(query)) {
112114
return section;
113115
}
114116
}

0 commit comments

Comments
 (0)