import { css } from '@emotion/react'; import { theme } from '@expo/styleguide'; import MDX from '@mdx-js/runtime'; import * as React from 'react'; import * as components from '~/common/translate-markdown'; const STYLES_TABLE = css` font-size: 1rem; margin-top: 24px; `; const STYLES_HEAD = css` background-color: ${theme.background.tertiary}; `; const STYLES_DESCRIPTION_CELL = css` word-break: break-word; white-space: break-spaces; padding-bottom: 0.2rem; `; export type Property = { description?: string[]; name: string; type?: string | string[]; enum?: string[]; properties?: Property[]; }; type FormattedProperty = { name: string; description: string; nestingLevel: number; }; export function formatSchema(rawSchema: Property[]) { const formattedSchema: FormattedProperty[] = []; rawSchema.map(property => { appendProperty(formattedSchema, property, 0); }); return formattedSchema; } //appends a property and recursively appends sub-properties function appendProperty( formattedSchema: FormattedProperty[], property: Property, _nestingLevel: number ) { let nestingLevel = _nestingLevel; formattedSchema.push({ name: nestingLevel ? `${property.name}` : `${property.name}`, description: createDescription(property), nestingLevel, }); nestingLevel++; if (property.properties) { (property.properties ?? []).forEach(subproperty => { appendProperty(formattedSchema, subproperty, nestingLevel); }); } } export function _getType(property: Property) { if (property.enum) { return 'enum'; } else { return property.type?.toString().replace(/,/g, ' || '); } } export function createDescription(property: Property) { let propertyDescription = `**(${_getType(property)})**`; if (property.description) { propertyDescription += ` - ` + property.description.join('\n'); } return propertyDescription; } export default class EasJsonPropertiesTable extends React.Component<{ schema: Property[]; }> { render() { const formattedSchema = formatSchema(this.props.schema); return ( {formattedSchema.map((property, index) => { return ( ); })}
Property Description
{property.name}
{property.description}
); } }