|
| 1 | +import { urlOption } from './url.js'; |
| 2 | + |
| 3 | +export function toMdFilename(name: string) { |
| 4 | + return name |
| 5 | + .split('-') |
| 6 | + .map((part) => `${part.at(0)?.toUpperCase()}${part.slice(1).toLowerCase()}`) |
| 7 | + .join(''); |
| 8 | +} |
| 9 | + |
| 10 | +export function resolveResourceFromDocsURL(link: string) { |
| 11 | + const url = urlOption(link); |
| 12 | + if (!url) { |
| 13 | + return null; |
| 14 | + } |
| 15 | + |
| 16 | + const pathParts = url.pathname.split('/').slice(2); |
| 17 | + if (!pathParts.length) { |
| 18 | + return null; |
| 19 | + } |
| 20 | + |
| 21 | + return { |
| 22 | + docsAnchor: url.hash, |
| 23 | + githubUrl: `https://raw.githubusercontent.com/discord/discord-api-docs/main/${pathParts |
| 24 | + .slice(0, -1) |
| 25 | + .join('/')}/${toMdFilename(pathParts.at(-1)!)}.md`, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +type Heading = { |
| 30 | + docs_anchor: string; |
| 31 | + label: string; |
| 32 | + route: string; |
| 33 | + verb: string; |
| 34 | +}; |
| 35 | + |
| 36 | +function parseHeadline(text: string): Heading | null { |
| 37 | + const match = /#{1,7} (?<label>.*) % (?<verb>\w{3,6}) (?<route>.*)/g.exec(text); |
| 38 | + if (!match) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + const { groups } = match; |
| 43 | + return { |
| 44 | + docs_anchor: `#${groups!.label.replaceAll(' ', '-').toLowerCase()}`, |
| 45 | + label: groups!.label, |
| 46 | + verb: groups!.verb, |
| 47 | + route: groups!.route, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +// https://raw.githubusercontent.com/discord/discord-api-docs/main/docs/resources/user/User.md |
| 52 | +// https://raw.githubusercontent.com/discord/discord-api-docs/main/docs/resources/User.md |
| 53 | + |
| 54 | +type ParsedSection = { |
| 55 | + heading: Heading | null; |
| 56 | + headline: string; |
| 57 | + lines: string[]; |
| 58 | +}; |
| 59 | + |
| 60 | +function cleanLine(line: string) { |
| 61 | + return line |
| 62 | + .replaceAll(/\[(.*?)]\(.*?\)/g, '$1') |
| 63 | + .replaceAll(/{(.*?)#.*?}/g, '$1') |
| 64 | + .trim(); |
| 65 | +} |
| 66 | + |
| 67 | +export function parseSections(content: string): ParsedSection[] { |
| 68 | + const res = []; |
| 69 | + const section: ParsedSection = { |
| 70 | + heading: null, |
| 71 | + lines: [], |
| 72 | + headline: '', |
| 73 | + }; |
| 74 | + |
| 75 | + for (const line of content.split('\n')) { |
| 76 | + const cleanedLine = cleanLine(line); |
| 77 | + |
| 78 | + if (line.startsWith('>')) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + if (line.startsWith('#')) { |
| 83 | + if (section.headline.length) { |
| 84 | + res.push({ ...section }); |
| 85 | + |
| 86 | + section.lines = []; |
| 87 | + section.heading = null; |
| 88 | + section.headline = ''; |
| 89 | + } |
| 90 | + |
| 91 | + section.headline = cleanedLine; |
| 92 | + const parsedHeading = parseHeadline(cleanedLine); |
| 93 | + if (parsedHeading) { |
| 94 | + section.heading = parsedHeading; |
| 95 | + } |
| 96 | + |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + if (cleanedLine.length) { |
| 101 | + section.lines.push(cleanedLine); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return res; |
| 106 | +} |
| 107 | + |
| 108 | +export function findRelevantDocsSection(query: string, docsMd: string) { |
| 109 | + const sections = parseSections(docsMd); |
| 110 | + for (const section of sections) { |
| 111 | + if (section.heading?.docs_anchor === query) { |
| 112 | + return section; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +export async function fetchDocsBody(link: string) { |
| 118 | + const githubResource = resolveResourceFromDocsURL(link); |
| 119 | + if (!githubResource) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + const docsMd = await fetch(githubResource.githubUrl).then(async (res) => res.text()); |
| 124 | + const section = findRelevantDocsSection(githubResource.docsAnchor, docsMd); |
| 125 | + |
| 126 | + if (section) { |
| 127 | + return section; |
| 128 | + } |
| 129 | +} |
0 commit comments