feat(DocsLink): Implement the component (#9380)

Co-authored-by: Noel <buechler.noel@outlook.com>
This commit is contained in:
Jiralite 2023-04-13 20:55:15 +01:00 committed by GitHub
parent 70da3746f8
commit 39c6694561
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 2 deletions

View file

@ -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 (
<a className="inline-flex flex-row place-items-center gap-1" href={url} rel="noopener noreferrer" target="_blank">
{text}
<FiExternalLink size={18} />
</a>
);
}

View file

@ -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;