Skip to content

Commit 30e04a8

Browse files
committed
fix(discorddocs): handle more edge cases
* non-exact matching anchors and section headings * true title case comparison (words < 3 lowercased) * remove colons from heading anchors * remove example comment that's no longer relevant * try mdx if md is 404 * loosely compare anchors resolves #216
1 parent 4d007af commit 30e04a8

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/util/discordDocs.ts

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { toTitlecase } from './misc.js';
12
import { urlOption } from './url.js';
23

34
export function toMdFilename(name: string) {
45
return name
56
.split('-')
6-
.map((part) => `${part.at(0)?.toUpperCase()}${part.slice(1).toLowerCase()}`)
7+
.map((part) => toTitlecase(part))
78
.join('_');
89
}
910

@@ -43,14 +44,13 @@ function parseHeadline(text: string): Heading | null {
4344
const label = groups.label ?? groups.onlylabel;
4445

4546
return {
46-
docs_anchor: `#${label.replaceAll(' ', '-').toLowerCase()}`,
47+
docs_anchor: `#${label.replaceAll(' ', '-').replaceAll(':', '').toLowerCase()}`,
4748
label,
4849
verb: groups.verb,
4950
route: groups.route,
5051
};
5152
}
5253

53-
// https://raw.githubusercontent.com/discord/discord-api-docs/main/docs/resources/user/User.md
5454
// https://raw.githubusercontent.com/discord/discord-api-docs/main/docs/resources/User.md
5555

5656
type ParsedSection = {
@@ -107,10 +107,26 @@ export function parseSections(content: string): ParsedSection[] {
107107
return res;
108108
}
109109

110+
function compressAnchor(anchor: string) {
111+
return anchor.replaceAll('-', '');
112+
}
113+
114+
function anchorsCompressedEqual(one?: string, other?: string) {
115+
if (!one || !other) {
116+
return false;
117+
}
118+
119+
const one_ = compressAnchor(one);
120+
const other_ = compressAnchor(other);
121+
122+
return one_ === other_;
123+
}
124+
110125
export function findRelevantDocsSection(query: string, docsMd: string) {
111126
const sections = parseSections(docsMd);
112127
for (const section of sections) {
113-
if (section.heading?.docs_anchor.startsWith(query)) {
128+
const anchor = section.heading?.docs_anchor;
129+
if (anchor?.startsWith(query) || anchorsCompressedEqual(anchor, query)) {
114130
return section;
115131
}
116132
}
@@ -122,7 +138,14 @@ export async function fetchDocsBody(link: string) {
122138
return null;
123139
}
124140

125-
const docsMd = await fetch(githubResource.githubUrl).then(async (res) => res.text());
141+
const docsMd = await fetch(githubResource.githubUrl).then(async (res) => {
142+
if (res.status === 404) {
143+
// some docs pages use the .mdx format
144+
return fetch(`${githubResource.githubUrl}x`).then(async (innerRes) => innerRes.text());
145+
}
146+
147+
return res.text();
148+
});
126149
const section = findRelevantDocsSection(githubResource.docsAnchor, docsMd);
127150

128151
if (section) {

src/util/misc.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Transform provided text to Titlecase
3+
* (words shorter than 3 characters are lowercased)
4+
*
5+
* @param text - The text to fransform
6+
* @returns Transformed text
7+
*/
8+
export function toTitlecase(text: string) {
9+
return text
10+
.trim()
11+
.split(' ')
12+
.map((word) => {
13+
if (word.length < 3) {
14+
return word.toLowerCase();
15+
}
16+
17+
return word[0]!.toUpperCase() + word.slice(1);
18+
})
19+
.join(' ');
20+
}

0 commit comments

Comments
 (0)