From 39c66945616a805a48e8ee085adb45c38c5c7291 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Thu, 13 Apr 2023 20:55:15 +0100 Subject: [PATCH] feat(DocsLink): Implement the component (#9380) Co-authored-by: Noel --- apps/guide/src/components/DocsLink.tsx | 79 +++++++++++++++++++++++++- apps/guide/src/util/constants.ts | 24 ++++++++ 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/apps/guide/src/components/DocsLink.tsx b/apps/guide/src/components/DocsLink.tsx index aa9a25620..168e26b1d 100644 --- a/apps/guide/src/components/DocsLink.tsx +++ b/apps/guide/src/components/DocsLink.tsx @@ -1,3 +1,78 @@ -export function DocsLink() { - return null; +import { FiExternalLink } from 'react-icons/fi'; +import { BASE_URL, BASE_URL_LEGACY, PACKAGES, VERSION } from '~/util/constants'; + +interface DocsLinkOptions { + /** + * Whether to apply brackets to the end of the symbol to denote a method. + * + * @remarks Functions automatically infer this. + */ + brackets?: boolean; + /** + * The package. + * + * @defaultValue `'discord.js'` + */ + package?: (typeof PACKAGES)[number]; + /** + * The initial documentation class, function, interface etc. + * + * @example `'Client'` + */ + parent: string; + /** + * Whether to reference a static property. + * + * @remarks + * This should only be used for the https://discord.js.org domain + * as static properties are not identified in the URL. + */ + static?: boolean; + /** + * The symbol belonging to the parent. + * + * @example '`login'` + */ + symbol?: string; + /** + * The type of the {@link DocsLinkOptions.parent}. + * + * @example `'class'` + * @example `'Function'` + */ + type: string; +} + +export function DocsLink({ + package: docs = PACKAGES[0], + type, + parent, + symbol, + brackets, + static: staticReference, +}: DocsLinkOptions) { + const bracketText = brackets || type.toUpperCase() === 'FUNCTION' ? '()' : ''; + const trimmedSymbol = symbol; + let url; + let text; + + if (docs === PACKAGES[0]) { + url = `${BASE_URL_LEGACY}/${VERSION}/${type}/${parent}`; + if (trimmedSymbol) url += `?scrollTo=${trimmedSymbol}`; + + text = `${parent}${trimmedSymbol ? (trimmedSymbol.startsWith('s-') ? '.' : '#') : ''}${ + trimmedSymbol ? `${trimmedSymbol.replace(/(e|s)-/, '')}` : '' + }${bracketText}`; + } else { + url = `${BASE_URL}/${docs}/stable/${parent}:${type}`; + if (trimmedSymbol) url += `#${trimmedSymbol}`; + text = `${parent}${trimmedSymbol ? `${staticReference ? '.' : '#'}${trimmedSymbol}` : ''}${bracketText}`; + } + + return ( + + {text} + + + ); } diff --git a/apps/guide/src/util/constants.ts b/apps/guide/src/util/constants.ts index 0e728ecc5..4eea1a980 100644 --- a/apps/guide/src/util/constants.ts +++ b/apps/guide/src/util/constants.ts @@ -1,3 +1,27 @@ +export const BASE_URL = 'https://discord.js.org/docs/packages' as const; + +export const BASE_URL_LEGACY = 'https://old.discordjs.dev/#/docs/discord.js' as const; + export const DESCRIPTION = 'Imagine a guide... that explores the many possibilities for your discord.js bot.'; export const GITHUB_BASE_PAGES_PATH = 'https://github.com/discordjs/discord.js/tree/main/apps/guide/src/pages'; + +export const PACKAGES = [ + 'discord.js', + 'brokers', + 'builders', + 'collection', + 'core', + 'formatters', + 'proxy', + 'rest', + 'next', + 'util', + 'voice', + 'ws', +] as const; + +/** + * The stable version of discord.js. + */ +export const VERSION = '14.9.0' as const;