import { css } from '@emotion/react'; import { theme } from '@expo/styleguide'; import React from 'react'; import ReactMarkdown from 'react-markdown'; import { Code, InlineCode } from '~/components/base/code'; import { H4 } from '~/components/base/headings'; import Link from '~/components/base/link'; import { LI, UL } from '~/components/base/list'; import { B, P, Quote } from '~/components/base/paragraph'; import { CommentData, MethodParamData, TypeDefinitionData, TypeDefinitionTypesData, } from '~/components/plugins/api/APIDataTypes'; export enum TypeDocKind { Enum = 4, Variable = 32, Function = 64, Class = 128, Interface = 256, Property = 1024, TypeAlias = 4194304, } export type MDRenderers = React.ComponentProps['renderers']; export const mdRenderers: MDRenderers = { blockquote: ({ children }) => ( {React.Children.map(children, child => child.type.name === 'paragraph' ? child.props.children : child )} ), code: ({ value, language }) => {value}, heading: ({ children }) =>

{children}

, inlineCode: ({ value }) => {value}, list: ({ children }) => , listItem: ({ children }) =>
  • {children}
  • , link: ({ href, children }) => {children}, paragraph: ({ children }) => (children ?

    {children}

    : null), strong: ({ children }) => {children}, text: ({ value }) => (value ? {value} : null), }; export const mdInlineRenderers: MDRenderers = { ...mdRenderers, paragraph: ({ children }) => (children ? {children} : null), }; const nonLinkableTypes = ['Date', 'T', 'TaskOptions', 'Uint8Array']; export const resolveTypeName = ({ elementType, name, type, types, typeArguments, declaration, }: TypeDefinitionData): string | JSX.Element | (string | JSX.Element)[] => { if (name) { if (type === 'reference') { if (typeArguments) { if (name === 'Promise') { return ( {name}<{typeArguments.map(resolveTypeName)}> ); } else if (name === 'Record') { return ( {name}<{typeArguments.map(resolveTypeName).join(',')}> ); } else { return `${typeArguments.map(resolveTypeName)}`; } } else { if (nonLinkableTypes.includes(name)) { return name; } else { return ( {name} ); } } } else { return name; } } else if (elementType?.name) { if (elementType.type === 'reference') { return ( {elementType.name} {type === 'array' && '[]'} ); } if (type === 'array') { return elementType.name + '[]'; } return elementType.name + type; } else if (type === 'union' && types?.length) { return types .map((t: TypeDefinitionTypesData) => t.type === 'reference' ? ( {t.name} ) : t.type === 'array' ? ( `${t.elementType?.name}[]` ) : t.type === 'literal' && typeof t.value === 'string' ? ( `'${t.name || t.value}'` ) : ( `${t.name || t.value}` ) ) .map((valueToRender, index) => ( {valueToRender} {index + 1 !== types.length && ' | '} )); } else if (declaration?.signatures) { const baseSignature = declaration.signatures[0]; if (baseSignature?.parameters?.length) { return ( <> ( {baseSignature.parameters?.map((param, index) => ( {param.name}: {resolveTypeName(param.type)} {index + 1 !== baseSignature.parameters.length && ', '} ))} ) {'=>'} {resolveTypeName(baseSignature.type)} ); } else { return `() => ${resolveTypeName(baseSignature.type)}`; } } return 'undefined'; }; export const renderParam = ({ comment, name, type }: MethodParamData): JSX.Element => (
  • {name} ({resolveTypeName(type)})
  • ); export type CommentTextBlockProps = { comment?: CommentData; renderers?: MDRenderers; withDash?: boolean; }; export const CommentTextBlock: React.FC = ({ comment, renderers = mdRenderers, withDash, }) => { const shortText = comment?.shortText?.trim().length ? ( {comment.shortText} ) : null; const text = comment?.text?.trim().length ? ( {comment.text} ) : null; const example = comment?.tags?.filter(tag => tag.tag === 'example')[0]; const exampleText = example ? ( {`__Example:__ ${example.text}`} ) : null; return ( <> {withDash && (shortText || text) ? ' - ' : null} {shortText} {text} {exampleText} ); }; export const STYLES_OPTIONAL = css` color: ${theme.text.secondary}; font-size: 90%; padding-top: 22px; `; export const STYLES_SECONDARY = css` color: ${theme.text.secondary}; font-size: 90%; font-weight: 600; `;