Skip to content

Commit

Permalink
fix(discorddocs): handle more edge cases
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
almostSouji committed Aug 31, 2024
1 parent 4d007af commit 30e04a8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/util/discordDocs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { toTitlecase } from './misc.js';
import { urlOption } from './url.js';

export function toMdFilename(name: string) {
return name
.split('-')
.map((part) => `${part.at(0)?.toUpperCase()}${part.slice(1).toLowerCase()}`)
.map((part) => toTitlecase(part))
.join('_');
}

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

return {
docs_anchor: `#${label.replaceAll(' ', '-').toLowerCase()}`,
docs_anchor: `#${label.replaceAll(' ', '-').replaceAll(':', '').toLowerCase()}`,
label,
verb: groups.verb,
route: groups.route,
};
}

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

type ParsedSection = {
Expand Down Expand Up @@ -107,10 +107,26 @@ export function parseSections(content: string): ParsedSection[] {
return res;
}

function compressAnchor(anchor: string) {
return anchor.replaceAll('-', '');
}

function anchorsCompressedEqual(one?: string, other?: string) {
if (!one || !other) {
return false;
}

const one_ = compressAnchor(one);
const other_ = compressAnchor(other);

return one_ === other_;
}

export function findRelevantDocsSection(query: string, docsMd: string) {
const sections = parseSections(docsMd);
for (const section of sections) {
if (section.heading?.docs_anchor.startsWith(query)) {
const anchor = section.heading?.docs_anchor;
if (anchor?.startsWith(query) || anchorsCompressedEqual(anchor, query)) {
return section;
}
}
Expand All @@ -122,7 +138,14 @@ export async function fetchDocsBody(link: string) {
return null;
}

const docsMd = await fetch(githubResource.githubUrl).then(async (res) => res.text());
const docsMd = await fetch(githubResource.githubUrl).then(async (res) => {
if (res.status === 404) {
// some docs pages use the .mdx format
return fetch(`${githubResource.githubUrl}x`).then(async (innerRes) => innerRes.text());
}

return res.text();
});
const section = findRelevantDocsSection(githubResource.docsAnchor, docsMd);

if (section) {
Expand Down
20 changes: 20 additions & 0 deletions src/util/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Transform provided text to Titlecase
* (words shorter than 3 characters are lowercased)
*
* @param text - The text to fransform
* @returns Transformed text
*/
export function toTitlecase(text: string) {
return text
.trim()
.split(' ')
.map((word) => {
if (word.length < 3) {
return word.toLowerCase();
}

return word[0]!.toUpperCase() + word.slice(1);
})
.join(' ');
}

0 comments on commit 30e04a8

Please sign in to comment.