Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/discordkit/discordkit. 💬 TypeScript SDK for Discord's API
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239/** * T2: Discord docs markdown parser. * * Reads a cached `.discord-docs/<page>.md` file and emits a structured * `DocResource`. Pure: no opinions about TypeScript/valibot output. * * Built on unified + remark-parse + remark-gfm + remark-mdx so MDX * constructs like `<Route method="GET">/users/@me</Route>`, * `<ManualAnchor id="..." />`, and `<Note>...</Note>` parse as proper * AST nodes rather than raw text. * * Approach: bottom-up scan. We walk the AST linearly tracking the current * heading stack at each node, then classify Routes (endpoints), Tables * (objects/enums/params), and admonitions by what heading they live under. * This avoids fragile assumptions about depth nesting — Discord's docs are * not consistent about whether a Structure table lives under ###### or ### * or directly under the parent ##. * * Usage as a library: * import { parseResource } from "./parse.ts"; * const resource = parseResource(markdown); * * Usage as a CLI: * node --experimental-strip-types scripts/docs/parse.ts <relative-path-under-.discord-docs> * node --experimental-strip-types scripts/docs/parse.ts --json resources/user.md > out.json */
import { readFileSync, existsSync } from "node:fs";import { resolve, dirname, join } from "node:path";import { fileURLToPath } from "node:url";import { unified } from "unified";import remarkParse from "remark-parse";import remarkGfm from "remark-gfm";import remarkMdx from "remark-mdx";import { toString as mdToString } from "mdast-util-to-string";import type { Root, RootContent, Table, PhrasingContent } from "mdast";
// MDX AST node interfaces.interface MdxJsxElement { type: `mdxJsxFlowElement` | `mdxJsxTextElement`; name: string | null; attributes: MdxJsxAttribute[]; children: RootContent[];}interface MdxJsxAttribute { type: `mdxJsxAttribute`; name: string; value: | string | { type: `mdxJsxAttributeValueExpression`; value: string } | null;}
// ─── public types ─────────────────────────────────────────────────────────
export type HttpMethod = `GET` | `POST` | `PUT` | `PATCH` | `DELETE`;
export interface DocResource { title: string; description: string; objects: DocObject[]; enums: DocEnum[]; endpoints: DocEndpoint[]; /** `## ...` sections that yielded neither an endpoint nor an object/enum. */ unparsedSections: string[];}
export interface DocObject { name: string; anchor: string | null; structureAnchor: string | null; description: string; fields: DocField[];}
export interface DocEnum { name: string; anchor: string | null; rows: DocEnumRow[];}
export interface DocEnumRow { value: string; name: string; description: string | null;}
export interface DocEndpoint { name: string; slug: string; method: HttpMethod; path: string; rawPath: string; description: string; notes: AdmonitionBlock[]; jsonParams: DocFieldGroup[]; queryParams: DocFieldGroup[]; formParams: DocFieldGroup[]; /** * Code-block examples that appear *under* the endpoint heading, * typically nested below an H6 sub-heading like `Example Partial Guild` * or `Example Response`. Captured in document order. */ examples: DocExample[];}
export interface DocExample { /** The closest sub-heading the example sits under (e.g. `Example Partial Guild`), or `null` if none. */ headingText: string | null; /** The fenced language tag — `json`, `python`, `text`, etc. May be empty. */ lang: string; /** The code-block contents, with trailing whitespace stripped. */ value: string;}
export interface AdmonitionBlock { kind: `Note` | `Warning` | `Warn` | `Info` | `Tip` | `Danger`; content: string;}
export interface DocFieldGroup { variant: string; fields: DocField[];}
export interface DocField { rawName: string; name: string; optional: boolean; type: DocFieldType; description: string; required: boolean | null; default: string | null;}
export type DocFieldType = | { kind: `primitive`; name: string; nullable: boolean } | { kind: `ref`; refName: string; refUrl: string; nullable: boolean; objectSuffix: boolean; } | { kind: `array`; element: DocFieldType; nullable: boolean } | { kind: `raw`; raw: string; nullable: boolean };
// ─── constants ─────────────────────────────────────────────────────────────
const PRIMITIVE_TYPES = new Set([ `snowflake`, `string`, `integer`, `number`, `float`, `boolean`, `bool`, `object`, `dict`, `any`, `null`, `iso8601 timestamp`, `binary`, `file contents`, `mixed`]);
const ADMONITION_NAMES = new Set<AdmonitionBlock[`kind`]>([ `Note`, `Warning`, `Warn`, `Info`, `Tip`, `Danger`]);
// ─── public entry point ────────────────────────────────────────────────────
export function parseResource(markdown: string): DocResource { const tree = unified() .use(remarkParse) .use(remarkGfm) .use(remarkMdx) .parse(markdown);
// Title and resource description. const { title, description, bodyStart } = extractHeader(tree.children);
// Pre-pass: build an indexed view of every node, with the heading-stack // at each position. Each node has [h2, h3, h4, h5, h6] showing the nearest // heading at each level above it (or null). interface IndexedNode { node: RootContent; stack: Array<string | null>; // index 2..6 → heading text at that depth, or null /** The deepest non-null heading in the stack. */ deepestHeading: { depth: number; text: string } | null; }
const indexed: IndexedNode[] = []; const stack: Array<string | null> = [ null, null, null, null, null, null, null ]; for (let i = bodyStart; i < tree.children.length; i++) { const node = tree.children[i]; if (node.type === `heading`) { const depth = node.depth; const text = mdToString(node).trim(); stack[depth] = text; // Clear any deeper levels — they belong to the previous sub-tree. for (let d = depth + 1; d <= 6; d++) stack[d] = null; // Don't index heading nodes themselves — only the content following. continue; } const stackCopy: Array<string | null> = [...stack]; let deepest: { depth: number; text: string } | null = null; for (let d = 6; d >= 2; d--) { const heading = stackCopy[d]; if (heading) { deepest = { depth: d, text: heading }; break; } } indexed.push({ node, stack: stackCopy, deepestHeading: deepest }); }
// First pass — collect endpoint headings (any heading immediately followed by a Route). // We treat ANY heading depth as a potential endpoint anchor (depth-2 in practice, // but be robust). We identify the endpoint by its closest enclosing heading // (typically H2) and capture the Route content for method/path. interface EndpointSeed { headingText: string; headingDepth: number; method: HttpMethod; rawPath: string; path: string; descriptionNodes: RootContent[]; /** Set of non-structural sub-heading texts we've already injected into description. */ seenSubHeadings: Set<string>; notes: AdmonitionBlock[]; examples: DocExample[]; /** Param groups by kind, keyed by variant label. */ paramTables: Array<{ kind: `json` | `query` | `form`; variant: string; table: Table; headingText: string; // the sub-heading title that wrapped this table }>; } const endpointSeeds: EndpointSeed[] = []; /** Map from heading text + depth to seed (so we can attach later-found content). */ const endpointByKey = new Map<string, EndpointSeed>(); const headingKey = (depth: number, text: string): string => `${depth}::${text}`;
for (const entry of indexed) { const route = findRouteMdx(entry.node); if (!route) continue; const methodAttr = getMdxAttribute(route, `method`); if (!methodAttr) continue; // The endpoint's heading is the closest H2 in the stack (most reliable signal). // Fallback: deepest heading at any level. const anchor = (entry.stack[2] ? { depth: 2, text: entry.stack[2] } : entry.deepestHeading) ?? null; if (!anchor) continue;
const method = methodAttr.toUpperCase() as HttpMethod; const rawPath = mdxElementText(route).trim(); const seed: EndpointSeed = { headingText: anchor.text, headingDepth: anchor.depth, method, rawPath, path: normalizePath(rawPath), descriptionNodes: [], seenSubHeadings: new Set(), notes: [], examples: [], paramTables: [] }; endpointSeeds.push(seed); endpointByKey.set(headingKey(anchor.depth, anchor.text), seed); }
// Second pass — for each non-heading node, attach to its closest endpoint // OR record as candidate for object/enum. const tableCandidates: Array<{ entry: IndexedNode; table: Table }> = []; const admonitionCandidates: Array<{ entry: IndexedNode; admonition: AdmonitionBlock; }> = [];
for (const entry of indexed) { const { node, stack } = entry; // Find the closest endpoint among ancestors at depths 2 or 3. let endpointSeed: EndpointSeed | undefined; for (let d = 6; d >= 2; d--) { const t = stack[d]; if (!t) continue; const s = endpointByKey.get(headingKey(d, t)); if (s) { endpointSeed = s; break; } }
// Admonitions: attach to closest endpoint as notes; ignore otherwise. const admonition = nodeAsAdmonition(node); if (admonition) { if (endpointSeed) endpointSeed.notes.push(admonition); else admonitionCandidates.push({ entry, admonition }); continue; }
// Skip ManualAnchor, mdxjsEsm, html, and other meta nodes. if (isMdxElement(node, `ManualAnchor`)) continue; if (node.type === `mdxjsEsm`) continue; if (node.type === `html`) continue; if (node.type === `thematicBreak`) continue;
// Tables: classify shape and attach as either endpoint params or object/enum candidate. if (node.type === `table`) { const header = parseTableHeader(node); const tableKind = classifyTable(header, entry); if (tableKind && endpointSeed && tableKind.kind === `param`) { endpointSeed.paramTables.push({ kind: tableKind.paramKind, variant: tableKind.variant, table: node, headingText: tableKind.headingText }); continue; } tableCandidates.push({ entry, table: node }); continue; }
// Code blocks under an endpoint. These typically nest below an H6 // "Example …" / "Example Response" sub-heading, or sit directly at H2 // for endpoints whose description includes a snippet. Skip when the // closest sub-heading is a known params/structure heading — those are // already represented as `jsonParams`/`queryParams`/`formParams`. if (node.type === `code` && endpointSeed) { const codeNode = node; const subHeading = closestSubHeading(stack, endpointSeed.headingDepth); if (!subHeading || !isStructuralHeading(subHeading)) { endpointSeed.examples.push({ headingText: subHeading, lang: codeNode.lang ?? ``, value: (codeNode.value ?? ``).replace(/\s+$/, ``) }); } continue; }
// Description prose. Content sits either directly under the endpoint // heading OR under a non-structural sub-heading (e.g. ###### Caveats, // ###### Limitations). Structural sub-headings like "JSON Params" or // "Response Body" are already captured as params tables and skipped. // // When entering a new non-structural sub-section for the first time, // inject a synthetic bolded heading node so the section title renders // alongside its content. if (endpointSeed) { const deepestNonNull = entry.deepestHeading; if (deepestNonNull) { const underEndpointDirectly = deepestNonNull.depth === endpointSeed.headingDepth && deepestNonNull.text === endpointSeed.headingText;
const underSubHeading = deepestNonNull.depth > endpointSeed.headingDepth && !isStructuralHeading(deepestNonNull.text);
if (underEndpointDirectly || underSubHeading) { // Inject sub-heading title once per section. if ( underSubHeading && !endpointSeed.seenSubHeadings.has(deepestNonNull.text) ) { endpointSeed.seenSubHeadings.add(deepestNonNull.text); endpointSeed.descriptionNodes.push( synthBoldParagraph(deepestNonNull.text) ); } // Exclude the Route line itself. if (!nodeContainsRoute(node)) { endpointSeed.descriptionNodes.push(node); } } } } }
// Build endpoints. const endpoints: DocEndpoint[] = endpointSeeds.map((seed) => { const jsonParams: DocFieldGroup[] = []; const queryParams: DocFieldGroup[] = []; const formParams: DocFieldGroup[] = []; for (const p of seed.paramTables) { const group: DocFieldGroup = { variant: p.variant, fields: parseFieldsTable(p.table) }; if (p.kind === `json`) jsonParams.push(group); else if (p.kind === `query`) queryParams.push(group); else if (p.kind === `form`) formParams.push(group); } return { name: seed.headingText, slug: slugify(seed.headingText), method: seed.method, path: seed.path, rawPath: seed.rawPath, description: nodesToText(seed.descriptionNodes).trim(), notes: seed.notes, jsonParams, queryParams, formParams, examples: seed.examples }; });
// Build objects/enums from remaining table candidates. const objects: DocObject[] = []; const enums: DocEnum[] = [];
// Group tables by their owning heading. For each group of (heading, [tables]): // - If any table has Field+Type header: it's the object structure for that heading. // - Other tables (Value/Name etc.) become nested enums named after their sub-heading. interface TableGroup { headingDepth: number; headingText: string; /** The heading at each depth level above the table — used to detect "nested" sub-headings. */ deeperHeading: { depth: number; text: string } | null; table: Table; } const groups: TableGroup[] = []; for (const { entry, table } of tableCandidates) { const { stack } = entry; // Identify the OBJECT-level heading: deepest of H2/H3 in stack. let objectHeading: { depth: number; text: string } | null = null; for (const d of [3, 2]) { const t = stack[d]; if (t) { objectHeading = { depth: d, text: t }; break; } } if (!objectHeading) continue; // The SUB-heading that wraps the table (H4–H6), if any. let deeperHeading: { depth: number; text: string } | null = null; for (let d = 6; d >= 4; d--) { const t = stack[d]; if (t) { deeperHeading = { depth: d, text: t }; break; } } groups.push({ headingDepth: objectHeading.depth, headingText: objectHeading.text, deeperHeading, table }); }
// Group by objectHeading. const byObjectHeading = new Map<string, TableGroup[]>(); for (const g of groups) { const key = `${g.headingDepth}::${g.headingText}`; const arr = byObjectHeading.get(key) ?? []; arr.push(g); byObjectHeading.set(key, arr); }
// Helper: collect description prose for an object — paragraphs and lists // sitting at depth-2 or depth-3 under the object's heading, BEFORE its // structure table. We stop scanning at the first table or at any node // whose deepest heading is below the object heading but in a sub- // heading that suggests we've left the description (e.g. "Example", // sub-object definitions). const extractObjectDescription = ( objHeadingDepth: number, objHeadingText: string, structureTable: Table ): string => { const parts: string[] = []; for (const e of indexed) { const { node } = e; if (node === structureTable) break; // Skip nodes outside the object's heading scope. const stackAtDepth = e.stack[objHeadingDepth]; if (stackAtDepth !== objHeadingText) continue; // Skip if a deeper heading is set — we only want prose directly // under the object heading, not nested sub-sections like Example. let underNestedHeading = false; for (let d = objHeadingDepth + 1; d <= 6; d++) { if (e.stack[d]) { underNestedHeading = true; break; } } if (underNestedHeading) continue; if (node.type === `heading`) continue; if (node.type === `table`) continue; if (node.type === `html`) continue; if (node.type === `code`) continue; if (nodeAsAdmonition(node)) continue; if (isMdxElement(node, `ManualAnchor`)) continue; if (node.type === `mdxjsEsm`) continue; if (node.type === `thematicBreak`) continue; // Skip the Route line for endpoint pages where this collides. if (nodeContainsRoute(node)) continue; const text = nodeToMarkdown(node).trim(); if (text) parts.push(text); } return parts.join(`\n\n`).trim(); };
for (const [, ts] of byObjectHeading) { // Find the structure table for the object (Field+Type). const structureGroup = ts.find((t) => { const header = parseTableHeader(t.table); return header && isStructureHeader(header); }); if (structureGroup) { // It's an object. The structure may be under a sub-heading (Structure) // or directly under the object heading. const fields = parseFieldsTable(structureGroup.table); // Capture the prose between the object's heading and its structure // table: any paragraphs/lists whose deepest heading is the object // heading (or a non-structural sub-heading like "Limits") and that // come before the structure table itself. const description = extractObjectDescription( structureGroup.headingDepth, structureGroup.headingText, structureGroup.table ); objects.push({ name: structureGroup.headingText, anchor: slugify(structureGroup.headingText), structureAnchor: null, description, fields }); // Other tables in this group → potential nested enums. for (const other of ts) { if (other === structureGroup) continue; const e = tableAsEnum(other); if (e) enums.push(e); } } else { // No structure table — but other tables may be enums. for (const t of ts) { const e = tableAsEnum(t); if (e) enums.push(e); } } }
// Compute unparsed sections: H2 headings that produced no endpoint and no object/enum. const unparsedSections: string[] = []; const seenAsEndpoint = new Set(endpointSeeds.map((s) => s.headingText)); const seenAsObject = new Set(objects.map((o) => o.name)); // Find all H2 headings in the doc. for (const node of tree.children) { if (node.type === `heading` && node.depth === 2) { const text = mdToString(node).trim(); if (seenAsEndpoint.has(text)) continue; if (seenAsObject.has(text)) continue; // Allow obj-suffix mismatch. if ( seenAsObject.has(`${text} Object`) || seenAsObject.has(`${text} Structure`) ) continue; unparsedSections.push(text); } }
return { title, description, objects, enums, endpoints, unparsedSections };}
// ─── helpers ───────────────────────────────────────────────────────────────
function extractHeader(children: RootContent[]): { title: string; description: string; bodyStart: number;} { let title = ``; let description = ``; let i = 0; for (; i < children.length; i++) { const node = children[i]; if (node.type === `heading` && node.depth === 1) { title = mdToString(node).trim(); i++; break; } } for (; i < children.length; i++) { const node = children[i]; if (node.type === `heading`) break; if (node.type === `blockquote`) { const text = mdToString(node).trim(); // Skip the "Documentation Index" header bot every Discord page has. if (text.startsWith(`## Documentation Index`)) continue; description = text; i++; break; } } return { title, description, bodyStart: i };}
function nodeAsAdmonition(node: RootContent): AdmonitionBlock | null { if (node.type !== `mdxJsxFlowElement` && node.type !== `mdxJsxTextElement`) return null; const el = node as unknown as MdxJsxElement; if (!el.name) return null; if (!ADMONITION_NAMES.has(el.name as AdmonitionBlock[`kind`])) return null; // Preserve markdown inside the admonition (backticks, bold, etc.) using // the same JSDoc-aware stringifier we use for description paragraphs. // Collapse all whitespace (including newlines from hard-wrapped source MDX) // to single spaces so the admonition body renders as one logical line. const content = el.children .map((c) => nodeToMarkdown(c)) .join(` `) .replace(/\s+/g, ` `) .trim(); return { kind: el.name as AdmonitionBlock[`kind`], content };}
function nodeContainsRoute(node: RootContent): boolean { return findRouteMdx(node) !== null;}
/** * Headings that introduce structured content already captured elsewhere on * the endpoint (parameter tables, response shape). Code blocks sitting * under these are skipped to avoid duplicating data we already encode as * `jsonParams`/`queryParams`/`formParams`. * * Match is case-insensitive and tolerant to minor wording shifts ("JSON * Params" vs. "JSON/Form Params"). Anything else — `Example Response`, * `Example Partial Guild`, etc. — falls through and is captured as a * {@link DocExample}. */const STRUCTURAL_HEADING_PATTERNS: RegExp[] = [ /\b(json|form|query(?:\s*string)?|url)\s*params?\b/i, /\bresponse\s*(?:body|structure)\b/i, /\bheaders?\b/i, /\bparameters?\b/i];
function isStructuralHeading(heading: string): boolean { return STRUCTURAL_HEADING_PATTERNS.some((re) => re.test(heading));}
/** * Given the heading stack for a node and the depth of the owning endpoint, * return the text of the deepest sub-heading strictly *below* the endpoint * (or `null` if the node sits directly under the endpoint heading). */function closestSubHeading( stack: Array<string | null>, endpointDepth: number): string | null { for (let d = 6; d > endpointDepth; d--) { const t = stack[d]; if (t) return t; } return null;}
/** * Build a synthetic mdast `paragraph` node containing a single `strong` * child holding the supplied text. Used to inject sub-section titles * (e.g. "Caveats", "Limitations") into the description stream so they * render as `**Caveats**` when the description is stringified. */function synthBoldParagraph(text: string): RootContent { return { type: `paragraph`, children: [ { type: `strong`, children: [{ type: `text`, value: text }] } ] } as unknown as RootContent;}
function findRouteMdx(node: RootContent): MdxJsxElement | null { if (isMdxElement(node, `Route`)) return node; if (node.type === `paragraph`) { for (const c of node.children) { // Inside a paragraph, only inline MDX elements (mdxJsxTextElement) appear. // Flow-level mdxJsxFlowElement can't nest under phrasing content. if (c.type === `mdxJsxTextElement`) { const cAny = c as unknown as MdxJsxElement; if (cAny.name === `Route`) return cAny; } } } return null;}
function isMdxElement( node: RootContent, name: string): node is RootContent & MdxJsxElement { return ( (node.type === `mdxJsxFlowElement` || node.type === `mdxJsxTextElement`) && (node as unknown as MdxJsxElement).name === name );}
function getMdxAttribute(el: MdxJsxElement, attrName: string): string | null { for (const attr of el.attributes) { if (attr.type !== `mdxJsxAttribute`) continue; if (attr.name !== attrName) continue; if (typeof attr.value === `string`) return attr.value; if (attr.value && typeof attr.value === `object` && `value` in attr.value) { return attr.value.value; } } return null;}
function mdxElementText(el: MdxJsxElement): string { return el.children.map((c) => mdToString(c)).join(``);}
// ─── table classification ──────────────────────────────────────────────────
interface IndexedNodeView { stack: Array<string | null>; deepestHeading: { depth: number; text: string } | null;}
function classifyTable( header: string[] | null, entry: IndexedNodeView): { kind: `param`; paramKind: `json` | `query` | `form`; variant: string; headingText: string;} | null { if (!header) return null; // Param tables sit under a heading whose title starts with "JSON Params", "Query String Params", or "Form Params" (or JSON/Form Params). // The heading is most often H6 but Discord also uses H3 in some places. for (let d = 6; d >= 3; d--) { const t = entry.stack[d]; if (!t) continue; const baseTitle = t.replace(/\s*\([^)]*\)\s*$/, ``).trim(); if (/^JSON Params$/i.test(baseTitle)) { return { kind: `param`, paramKind: `json`, variant: extractVariantLabel(t), headingText: t }; } if (/^Query String Params$/i.test(baseTitle)) { return { kind: `param`, paramKind: `query`, variant: extractVariantLabel(t), headingText: t }; } if (/^Form Params$|^JSON\/Form Params$/i.test(baseTitle)) { return { kind: `param`, paramKind: `form`, variant: extractVariantLabel(t), headingText: t }; } // Only check the deepest heading that's set — anything above is irrelevant. break; } return null;}
function isStructureHeader(header: string[]): boolean { // First column is Field or Name, second is Type. return ( (header[0] === `field` || header[0] === `name`) && header[1] === `type` );}
function tableAsEnum(group: { headingText: string; deeperHeading: { depth: number; text: string } | null; table: Table;}): DocEnum | null { const table = group.table; const header = parseTableHeader(table); if (!header) return null; if (isStructureHeader(header)) return null;
const descIdx = header.findIndex((c) => c === `description`); let valueIdx = header.findIndex((c) => c === `value`); if (valueIdx === -1) valueIdx = header.findIndex((c) => c === `id`); let labelIdx = header.findIndex((c) => c === `name`); if (labelIdx === -1) { labelIdx = header.findIndex( (c, i) => i !== valueIdx && c !== `description` ); } if (valueIdx === -1) { if (header.length < 2) return null; valueIdx = header.findIndex( (c, i) => i !== labelIdx && c !== `description` ); } if (valueIdx === -1 || labelIdx === -1) return null;
const rows = parseTableRowsAsRecord(table, header).map((row) => ({ value: row[header[valueIdx]] ?? ``, name: row[header[labelIdx]] ?? ``, description: descIdx >= 0 ? (row[header[descIdx]] ?? null) : null })); if (rows.length === 0) return null;
// Enum name: if there's a deeper-heading (level 4-6), use it. Otherwise use the object-level heading. const name = group.deeperHeading ? group.deeperHeading.text : stripObjectSuffix(group.headingText); // Discord nests enum tables inside their parent object's section. // The anchor convention is `<parent slug>-<enum slug>` for nested enums // and just `<slug>` for top-level enums. Mirror that. const anchor = group.deeperHeading ? `${slugify(group.headingText)}-${slugify(group.deeperHeading.text)}` : slugify(name); return { name, anchor, rows };}
function parseTableHeader(table: Table): string[] | null { const headerRow = table.children[0]; if (!headerRow || headerRow.type !== `tableRow`) return null; return headerRow.children.map((cell) => mdToString(cell).trim().toLowerCase() );}
function parseTableRowsAsRecord( table: Table, header: string[]): Array<Record<string, string>> { const rows: Array<Record<string, string>> = []; for (let i = 1; i < table.children.length; i++) { const row = table.children[i]; if (row.type !== `tableRow`) continue; const record: Record<string, string> = {}; for (let c = 0; c < header.length; c++) { const cell = row.children[c]; record[header[c]] = cell ? cellToMarkdown(cell) : ``; } rows.push(record); } return rows;}
function parseFieldsTable(table: Table): DocField[] { const header = parseTableHeader(table); if (!header) return []; const fieldIdx = header.findIndex((c) => c === `field` || c === `name`); const typeIdx = header.findIndex((c) => c === `type`); const descIdx = header.findIndex((c) => c === `description`); const reqIdx = header.findIndex((c) => c === `required`); const defaultIdx = header.findIndex((c) => c === `default`); if (fieldIdx === -1 || typeIdx === -1) return [];
const fields: DocField[] = []; for (let i = 1; i < table.children.length; i++) { const row = table.children[i]; if (row.type !== `tableRow`) continue; const cells = row.children; const fieldText = ( cells[fieldIdx] ? mdToString(cells[fieldIdx]) : `` ).trim(); const typeCell = cells[typeIdx]; const descText = descIdx >= 0 && cells[descIdx] ? cellToMarkdown(cells[descIdx]) : ``; const reqText = reqIdx >= 0 && cells[reqIdx] ? mdToString(cells[reqIdx]).trim().toLowerCase() : ``; const defaultText = defaultIdx >= 0 && cells[defaultIdx] ? mdToString(cells[defaultIdx]).trim() : ``;
let optional = false; let rawName = fieldText; if (rawName.endsWith(`?`)) { optional = true; rawName = rawName.slice(0, -1); }
fields.push({ rawName, name: snakeToCamel(rawName), optional, type: parseTypeCell(typeCell), description: descText, required: reqIdx >= 0 ? reqText === `true` ? true : reqText === `false` ? false : null : null, default: defaultIdx >= 0 ? defaultText || null : null }); } return fields;}
function cellToMarkdown(cell: RootContent | PhrasingContent): string { if (cell.type === `tableCell`) { return cell.children.map(phrasingToMarkdown).join(``); } return mdToString(cell);}
function phrasingToMarkdown(node: PhrasingContent): string { // mdast's PhrasingContent union has ~25 members; we only handle the ones // Discord's docs actually emit. The `default` branch falls back to mdast's // own to-string for anything unexpected, so missing cases degrade // gracefully rather than crash. // oxlint-disable-next-line typescript/switch-exhaustiveness-check switch (node.type) { case `text`: return node.value; case `inlineCode`: return `\`${node.value}\``; case `strong`: return `**${node.children.map(phrasingToMarkdown).join(``)}**`; case `emphasis`: return `*${node.children.map(phrasingToMarkdown).join(``)}*`; case `link`: return `[${node.children.map(phrasingToMarkdown).join(``)}](${node.url})`; case `html`: return node.value; case `break`: return `\n`; default: { const maybeChildren = (node as { children?: PhrasingContent[] }).children; if (maybeChildren) return maybeChildren.map(phrasingToMarkdown).join(``); return mdToString(node); } }}
function parseTypeCell(cell: RootContent | undefined): DocFieldType { if (!cell) return { kind: `raw`, raw: ``, nullable: false }; let text = cell.type === `tableCell` ? cell.children.map(phrasingToMarkdown).join(``).trim() : mdToString(cell).trim();
let nullable = false; if (text.startsWith(`?`)) { nullable = true; text = text.slice(1).trim(); }
let objectSuffix = false; const objectMatch = /\s+objects?\s*$/i.exec(text); if (objectMatch) { text = text.slice(0, objectMatch.index).trim(); objectSuffix = true; }
const arrayMatch = /^array(?:\s+of\s+(.+))?$/i.exec(text); if (arrayMatch) { if (!arrayMatch[1]) { return { kind: `array`, element: { kind: `raw`, raw: `unknown`, nullable: false }, nullable }; } let elementText = arrayMatch[1].replace(/^partial\s+/i, ``).trim(); if (/^strings?$/i.test(elementText)) elementText = `string`; else if (/^snowflakes?$/i.test(elementText)) elementText = `snowflake`; else if (/^integers?$/i.test(elementText)) elementText = `integer`; const fakeCell: PhrasingContent = { type: `text`, value: elementText }; const wrapped: RootContent = { type: `tableCell`, children: [fakeCell] }; return { kind: `array`, element: parseTypeCell(wrapped), nullable }; }
const linkMatch = /^\[([^\]]+)\]\(([^)]+)\)\s*$/.exec(text); if (linkMatch) { return { kind: `ref`, refName: linkMatch[1].trim(), refUrl: linkMatch[2].trim(), nullable, objectSuffix }; }
const lower = text.toLowerCase(); if (PRIMITIVE_TYPES.has(lower)) { return { kind: `primitive`, name: lower, nullable }; }
return { kind: `raw`, raw: text, nullable };}
// ─── misc helpers ──────────────────────────────────────────────────────────
function nodesToText(nodes: RootContent[]): string { return nodes .filter( (n) => n.type !== `html` || !(n as { value: string }).value.startsWith(`<!--`) ) .map((n) => nodeToMarkdown(n)) .join(`\n\n`) .replace(/\n{3,}/g, `\n\n`);}
/** * Markdown-preserving stringifier for top-level nodes (paragraphs, lists, * etc.). Unlike `mdToString` from `mdast-util-to-string` — which strips all * markdown to plain text — this keeps `` `code` ``, `**bold**`, `*italics*`, * and *external* `[text](url)` links intact. Discord-internal cross-doc * links (e.g. `/developers/resources/channel#channel-object`) are flattened * to their display text so a downstream pass can re-link them as * `{@link Name | display}` references against the local type registry. * * Other block types fall back to plain text. */function nodeToMarkdown(node: RootContent): string { // mdast's RootContent union has ~25 members; see the matching // suppression on `phrasingToMarkdown` above for the same rationale. // oxlint-disable-next-line typescript/switch-exhaustiveness-check switch (node.type) { case `paragraph`: // Collapse soft line breaks (hard-wrapped MDX source) to single spaces // so each paragraph renders as one logical line. Hard breaks (markdown // `\` + newline) are emitted as `\n` by `phrasingForJsDoc` for the // `break` node, but those are rare in Discord's docs. return node.children .map(phrasingForJsDoc) .join(``) .replace(/[ \t]*\n[ \t]*/g, ` `); case `list`: { const marker = node.ordered ? `1.` : `-`; return node.children .map((item) => { const text = (item.children as RootContent[]) .map((c) => nodeToMarkdown(c)) .join(`\n\n`); return `${marker} ${text}`; }) .join(`\n`); } default: return mdToString(node); }}
/** * Like {@link phrasingToMarkdown}, but tailored for JSDoc prose: * Discord-internal links (`/developers/...`) are flattened to their display * text. External links are preserved as full markdown so the JSDoc can show * a clickable URL. */function phrasingForJsDoc(node: PhrasingContent): string { if (node.type === `link`) { const display = node.children.map(phrasingForJsDoc).join(``); if (node.url.startsWith(`/developers/`)) return display; return `[${display}](${node.url})`; } if (node.type === `text`) return node.value; if (node.type === `inlineCode`) return `\`${node.value}\``; if (node.type === `strong`) return `**${node.children.map(phrasingForJsDoc).join(``)}**`; if (node.type === `emphasis`) return `*${node.children.map(phrasingForJsDoc).join(``)}*`; if (node.type === `html`) return node.value; if (node.type === `break`) return `\n`; const maybeChildren = (node as { children?: PhrasingContent[] }).children; if (maybeChildren) return maybeChildren.map(phrasingForJsDoc).join(``); return mdToString(node);}
function normalizePath(rawPath: string): string { // Discord placeholders look like `[\{webhook.id}](url)` or `[\{user.id}](url)`. // The repo convention uses the SUFFIX as the param name (so `webhook.token` // becomes `:token`, `user.id` becomes `:user`), with a fallback to the // PREFIX when only a single placeholder uses that prefix. // // Standalone placeholders like `{instance_id}` (no dot prefix) get // converted to `:instanceId` (camelCase, matching repo convention). return rawPath .replace( /\[\\?\{([^.}]+)\.([^}]+)\}\]\([^)]+\)/g, (_, prefix: string, suffix: string) => { const cleanSuffix = suffix.replace(/\\_/g, `_`); return cleanSuffix === `id` ? `:${prefix}` : `:${snakeToCamel(cleanSuffix)}`; } ) .replace( /\[\\?\{([^}]+)\}\]\([^)]+\)/g, (_, raw: string) => `:${snakeToCamel(raw.replace(/\\_/g, `_`))}` ) .replace( /\\?\{([^.}]+)\.([^}]+)\}/g, (_, prefix: string, suffix: string) => { const cleanSuffix = suffix.replace(/\\_/g, `_`); return cleanSuffix === `id` ? `:${prefix}` : `:${snakeToCamel(cleanSuffix)}`; } ) .replace( /\\?\{([^}]+)\}/g, (_, raw: string) => `:${snakeToCamel(raw.replace(/\\_/g, `_`))}` );}
function slugify(heading: string): string { return heading .toLowerCase() .replace(/[^a-z0-9\s-]/g, ``) .trim() .replace(/\s+/g, `-`);}
function snakeToCamel(name: string): string { return name.replace(/_([a-zA-Z])/g, (_, ch) => ch.toUpperCase());}
function extractVariantLabel(headingText: string): string { const m = /\(([^)]+)\)\s*$/.exec(headingText); return m ? m[1].trim() : ``;}
function stripObjectSuffix(text: string): string { return text .replace(/\s+Object$/i, ``) .replace(/\s+Structure$/i, ``) .trim();}
// ─── CLI ───────────────────────────────────────────────────────────────────
const invokedDirectly = process.argv[1] ?.replace(/\\/g, `/`) .endsWith(`scripts/docs/parse.ts`);
if (invokedDirectly) { const args = process.argv.slice(2); const jsonMode = args.includes(`--json`); const target = args.find((a) => !a.startsWith(`--`)); if (!target) { console.error( `usage: node --experimental-strip-types scripts/docs/parse.ts [--json] <relative-path-under-.discord-docs>` ); process.exit(1); } const projectRoot = resolve(dirname(fileURLToPath(import.meta.url)), `../..`); const filePath = join(projectRoot, `.discord-docs`, target); if (!existsSync(filePath)) { console.error(`not found: ${filePath}`); process.exit(1); } const md = readFileSync(filePath, `utf8`); const result = parseResource(md); if (jsonMode) { console.log(JSON.stringify(result, null, 2)); } else { summarize(result); }}
function summarize(r: DocResource): void { console.log(`# ${r.title}`); console.log(`> ${r.description}\n`); console.log(`Objects (${r.objects.length}):`); for (const o of r.objects) { console.log(` - ${o.name} (${o.fields.length} fields)`); } console.log(`\nEnums (${r.enums.length}):`); for (const e of r.enums) { console.log(` - ${e.name} (${e.rows.length} rows)`); } console.log(`\nEndpoints (${r.endpoints.length}):`); for (const ep of r.endpoints) { const params: string[] = []; if (ep.jsonParams.length) { params.push( `json[${ep.jsonParams.reduce((s, g) => s + g.fields.length, 0)}]` ); } if (ep.queryParams.length) { params.push( `query[${ep.queryParams.reduce((s, g) => s + g.fields.length, 0)}]` ); } if (ep.formParams.length) { params.push( `form[${ep.formParams.reduce((s, g) => s + g.fields.length, 0)}]` ); } console.log( ` - ${ep.method.padEnd(6)} ${ep.path.padEnd(50)} ${ep.name}${params.length ? ` (${params.join(`, `)})` : ``}` ); } if (r.unparsedSections.length > 0) { console.log(`\nUnparsed sections (${r.unparsedSections.length}):`); for (const s of r.unparsedSections) console.log(` - ${s}`); }}