1import { css } from '@emotion/react'; 2import { theme } from '@expo/styleguide'; 3import React from 'react'; 4import ReactMarkdown from 'react-markdown'; 5 6import { Code, InlineCode } from '~/components/base/code'; 7import { H4 } from '~/components/base/headings'; 8import Link from '~/components/base/link'; 9import { LI, UL } from '~/components/base/list'; 10import { B, P, Quote } from '~/components/base/paragraph'; 11import { 12 CommentData, 13 MethodParamData, 14 TypeDefinitionData, 15 TypeDefinitionTypesData, 16} from '~/components/plugins/api/APIDataTypes'; 17 18export enum TypeDocKind { 19 Enum = 4, 20 Variable = 32, 21 Function = 64, 22 Class = 128, 23 Interface = 256, 24 Property = 1024, 25 TypeAlias = 4194304, 26} 27 28export type MDRenderers = React.ComponentProps<typeof ReactMarkdown>['renderers']; 29 30export const mdRenderers: MDRenderers = { 31 blockquote: ({ children }) => ( 32 <Quote> 33 {React.Children.map(children, child => 34 child.type.name === 'paragraph' ? child.props.children : child 35 )} 36 </Quote> 37 ), 38 code: ({ value, language }) => <Code className={`language-${language}`}>{value}</Code>, 39 heading: ({ children }) => <H4>{children}</H4>, 40 inlineCode: ({ value }) => <InlineCode>{value}</InlineCode>, 41 list: ({ children }) => <UL>{children}</UL>, 42 listItem: ({ children }) => <LI>{children}</LI>, 43 link: ({ href, children }) => <Link href={href}>{children}</Link>, 44 paragraph: ({ children }) => (children ? <P>{children}</P> : null), 45 strong: ({ children }) => <B>{children}</B>, 46 text: ({ value }) => (value ? <span>{value}</span> : null), 47}; 48 49export const mdInlineRenderers: MDRenderers = { 50 ...mdRenderers, 51 paragraph: ({ children }) => (children ? <span>{children}</span> : null), 52}; 53 54const nonLinkableTypes = ['Date', 'T', 'TaskOptions', 'Uint8Array']; 55 56export const resolveTypeName = ({ 57 elementType, 58 name, 59 type, 60 types, 61 typeArguments, 62 declaration, 63}: TypeDefinitionData): string | JSX.Element | (string | JSX.Element)[] => { 64 if (name) { 65 if (type === 'reference') { 66 if (typeArguments) { 67 if (name === 'Promise') { 68 return ( 69 <span> 70 {'Promise<'} 71 {typeArguments.map(resolveTypeName)} 72 {'>'} 73 </span> 74 ); 75 } else { 76 return `${typeArguments.map(resolveTypeName)}`; 77 } 78 } else { 79 if (nonLinkableTypes.includes(name)) { 80 return name; 81 } else { 82 return ( 83 <Link href={`#${name.toLowerCase()}`} key={`type-link-${name}`}> 84 {name} 85 </Link> 86 ); 87 } 88 } 89 } else { 90 return name; 91 } 92 } else if (elementType?.name) { 93 if (elementType.type === 'reference') { 94 return ( 95 <Link href={`#${elementType.name?.toLowerCase()}`} key={`type-link-${elementType.name}`}> 96 {elementType.name} 97 {type === 'array' && '[]'} 98 </Link> 99 ); 100 } 101 if (type === 'array') { 102 return elementType.name + '[]'; 103 } 104 return elementType.name + type; 105 } else if (type === 'union' && types?.length) { 106 return types 107 .map((t: TypeDefinitionTypesData) => 108 t.type === 'reference' ? ( 109 <Link href={`#${t.name?.toLowerCase()}`} key={`type-link-${t.name}`}> 110 {t.name} 111 </Link> 112 ) : t.type === 'array' ? ( 113 `${t.elementType?.name}[]` 114 ) : t.type === 'literal' && typeof t.value === 'string' ? ( 115 `'${t.name || t.value}'` 116 ) : ( 117 `${t.name || t.value}` 118 ) 119 ) 120 .map((valueToRender, index) => ( 121 <span key={`union-type-${index}`}> 122 {valueToRender} 123 {index + 1 !== types.length && ' | '} 124 </span> 125 )); 126 } else if (declaration?.signatures) { 127 const baseSignature = declaration.signatures[0]; 128 if (baseSignature?.parameters?.length) { 129 return ( 130 <> 131 ( 132 {baseSignature.parameters?.map((param, index) => ( 133 <span key={`param-${index}-${param.name}`}> 134 {param.name}: {resolveTypeName(param.type)} 135 {index + 1 !== baseSignature.parameters.length && ', '} 136 </span> 137 ))} 138 ) {'=>'} {resolveTypeName(baseSignature.type)} 139 </> 140 ); 141 } else { 142 return `() => ${resolveTypeName(baseSignature.type)}`; 143 } 144 } 145 return 'undefined'; 146}; 147 148export const renderParam = ({ comment, name, type }: MethodParamData): JSX.Element => ( 149 <LI key={`param-${name}`}> 150 <B> 151 {name} (<InlineCode>{resolveTypeName(type)}</InlineCode>) 152 </B> 153 <CommentTextBlock comment={comment} renderers={mdInlineRenderers} withDash /> 154 </LI> 155); 156 157export type CommentTextBlockProps = { 158 comment?: CommentData; 159 renderers?: MDRenderers; 160 withDash?: boolean; 161}; 162 163export const CommentTextBlock: React.FC<CommentTextBlockProps> = ({ 164 comment, 165 renderers = mdRenderers, 166 withDash, 167}) => { 168 const shortText = comment?.shortText?.trim().length ? ( 169 <ReactMarkdown renderers={renderers}>{comment.shortText}</ReactMarkdown> 170 ) : null; 171 const text = comment?.text?.trim().length ? ( 172 <ReactMarkdown renderers={renderers}>{comment.text}</ReactMarkdown> 173 ) : null; 174 return ( 175 <> 176 {withDash && (shortText || text) ? ' - ' : null} 177 {shortText} 178 {text} 179 </> 180 ); 181}; 182 183export const STYLES_OPTIONAL = css` 184 color: ${theme.text.secondary}; 185 font-size: 90%; 186 padding-top: 22px; 187`; 188 189export const STYLES_SECONDARY = css` 190 color: ${theme.text.secondary}; 191 font-size: 90%; 192 font-weight: 600; 193`; 194