From 81334a2a2c5d404b328d1e1f2e2ece4681bb7ca5 Mon Sep 17 00:00:00 2001 From: Almeida Date: Fri, 10 Nov 2023 14:50:25 +0000 Subject: [PATCH] feat: link basic types to MDN/TS documentation (#9913) * feat: link basic types to MDN/TS documentation * refactor: requested changes --- .../src/components/DocumentationLink.tsx | 9 +++ apps/website/src/components/ExcerptText.tsx | 15 +++- .../src/util/builtinDocumentationLinks.ts | 76 +++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 apps/website/src/components/DocumentationLink.tsx create mode 100644 apps/website/src/util/builtinDocumentationLinks.ts diff --git a/apps/website/src/components/DocumentationLink.tsx b/apps/website/src/components/DocumentationLink.tsx new file mode 100644 index 000000000..bef2aa71a --- /dev/null +++ b/apps/website/src/components/DocumentationLink.tsx @@ -0,0 +1,9 @@ +import type { PropsWithChildren } from 'react'; + +export function DocumentationLink({ children, href }: PropsWithChildren<{ readonly href: string }>) { + return ( + + {children} + + ); +} diff --git a/apps/website/src/components/ExcerptText.tsx b/apps/website/src/components/ExcerptText.tsx index adf6b4383..bd7b89592 100644 --- a/apps/website/src/components/ExcerptText.tsx +++ b/apps/website/src/components/ExcerptText.tsx @@ -1,6 +1,8 @@ import type { ApiModel, Excerpt } from '@discordjs/api-extractor-model'; import { ExcerptTokenKind } from '@discordjs/api-extractor-model'; +import { BuiltinDocumentationLinks } from '~/util/builtinDocumentationLinks'; import { DISCORD_API_TYPES_DOCS_URL } from '~/util/constants'; +import { DocumentationLink } from './DocumentationLink'; import { ItemLink } from './ItemLink'; import { resolveItemURI } from './documentation/util'; @@ -25,6 +27,15 @@ export function ExcerptText({ model, excerpt }: ExcerptTextProps) { // TODO: Real fix in api-extractor needed const text = token.text.replaceAll('\n', '').replaceAll(/\s{2}$/g, ''); if (token.kind === ExcerptTokenKind.Reference) { + if (text in BuiltinDocumentationLinks) { + const href = BuiltinDocumentationLinks[text as keyof typeof BuiltinDocumentationLinks]; + return ( + + {text} + + ); + } + const source = token.canonicalReference?.source; const symbol = token.canonicalReference?.symbol; if (source && 'packageName' in source && source.packageName === 'discord-api-types' && symbol) { @@ -38,9 +49,9 @@ export function ExcerptText({ model, excerpt }: ExcerptTextProps) { else href += `/${meaning}/${text}`; return ( - + {text} - + ); } diff --git a/apps/website/src/util/builtinDocumentationLinks.ts b/apps/website/src/util/builtinDocumentationLinks.ts new file mode 100644 index 000000000..8f05caa48 --- /dev/null +++ b/apps/website/src/util/builtinDocumentationLinks.ts @@ -0,0 +1,76 @@ +export const BuiltinDocumentationLinks = { + // Built-in types + bigint: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/BigInt', + boolean: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean', + null: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/null', + number: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number', + string: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String', + symbol: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol', + undefined: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined', + + // Built-in classes + Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array', + ArrayBuffer: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer', + AsyncGenerator: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator', + AsyncIterable: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Iteration_protocols', + AsyncIterableIterator: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Iteration_protocols', + Buffer: 'https://nodejs.org/api/buffer.html#class-buffer', + Date: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date', + Error: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error', + Function: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function', + Generator: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Generator', + Iterable: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Iteration_protocols', + IterableIterator: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Iteration_protocols', + Iterator: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Iterator', + Map: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map', + Promise: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise', + RegExp: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp', + Set: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set', + WeakMap: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WeakMap', + WeakRef: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WeakRef', + WeakSet: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WeakSet', + + // Typed arrays + BigInt64Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array', + BigUint64Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array', + Float32Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Float32Array', + Float64Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Float64Array', + Int16Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int16Array', + Int32Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int32Array', + Int8Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int8Array', + Uint16Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array', + Uint32Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array', + Uint8Array: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array', + Uint8ClampedArray: 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray', + + // TypeScript types + any: 'https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any', + never: 'https://www.typescriptlang.org/docs/handbook/2/functions.html#never', + object: 'https://www.typescriptlang.org/docs/handbook/2/functions.html#object', + ReadonlyArray: 'https://www.typescriptlang.org/docs/handbook/2/objects.html#the-readonlyarray-type', + unknown: 'https://www.typescriptlang.org/docs/handbook/2/functions.html#unknown', + void: 'https://www.typescriptlang.org/docs/handbook/2/functions.html#void', + + // TypeScript utility types + Awaited: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#awaitedtype', + Partial: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype', + Required: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype', + Readonly: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype', + Record: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type', + Pick: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys', + Omit: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys', + Exclude: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#excludeuniontype-excludedmembers', + Extract: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#extracttype-union', + NonNullable: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullabletype', + Parameters: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype', + ConstructorParameters: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#constructorparameterstype', + ReturnType: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype', + InstanceType: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype', + ThisParameterType: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#thisparametertypetype', + OmitThisParameter: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#omitthisparametertype', + ThisType: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#thistypetype', + Uppercase: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#uppercasestringtype', + Lowercase: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#lowercasestringtype', + Capitalize: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#capitalizestringtype', + Uncapitalize: 'https://www.typescriptlang.org/docs/handbook/utility-types.html#uncapitalizestringtype', +} as const;