From b15f1207c1ad846d337a6fcfe8f370eb4e0b7f4a Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Mon, 15 Jun 2020 19:41:54 -0700 Subject: [PATCH] feat: Add syntax highlighting for constants --- src/components/ConstantReference.js | 76 ++++++++++++++++---- src/components/ConstantReference.module.scss | 1 + src/hooks/useApiDoc.js | 8 +-- 3 files changed, 66 insertions(+), 19 deletions(-) diff --git a/src/components/ConstantReference.js b/src/components/ConstantReference.js index 746fbf07c..49f2ed1b9 100644 --- a/src/components/ConstantReference.js +++ b/src/components/ConstantReference.js @@ -1,33 +1,85 @@ -import React from 'react'; -import Markdown from 'react-markdown'; +import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import CodeDef from './CodeDef'; import styles from './ConstantReference.module.scss'; +const Value = ({ value }) => { + const type = typeof value; + + if (Array.isArray(value)) { + return ( + <> + [ + + {value.map((item, idx) => ( +
+ + {idx !== value.length - 1 && ','} +
+ ))} +
+ ] + + ); + } + + if (value == null) { + return {String(value)}; + } + + console.log({ value, type }); + + switch (type) { + case 'object': + return ( + <> + {'{ '} + + {Object.entries(value).map(([key, value], idx, arr) => ( +
+ {key}: + + {idx !== arr.length - 1 && ', '} +
+ ))} +
+ {' }'} + + ); + case 'string': + return ; + case 'number': + return ; + default: + return value; + } +}; + +Value.propTypes = { + value: PropTypes.oneOfType([ + PropTypes.object, + PropTypes.array, + PropTypes.string, + ]), +}; + const ConstantReference = ({ constant }) => { - const { name, values, type } = constant; + const { name, value } = constant; return (

{name}

- {type === 'array' ? '[' : '{'} - - {values.map((value, i) => ( - - ))} - - {type === 'array' ? ']' : '}'} +
); }; ConstantReference.propTypes = { constant: PropTypes.shape({ - type: PropTypes.string, name: PropTypes.string, - values: PropTypes.any, + value: PropTypes.any, }), }; diff --git a/src/components/ConstantReference.module.scss b/src/components/ConstantReference.module.scss index 5f82e15a6..371c3175b 100644 --- a/src/components/ConstantReference.module.scss +++ b/src/components/ConstantReference.module.scss @@ -1,4 +1,5 @@ .container { + color: var(--color-neutrals-600); font-size: 0.875rem; &:not(:last-child) { diff --git a/src/hooks/useApiDoc.js b/src/hooks/useApiDoc.js index c3d2b0c9e..74f9b8ef1 100644 --- a/src/hooks/useApiDoc.js +++ b/src/hooks/useApiDoc.js @@ -48,13 +48,7 @@ const useApiDoc = (name) => { .map((member) => { return { name: `${name}.${member}`, - type: api[member] instanceof Array ? 'array' : typeof api[member], - values: - api[member] instanceof Array - ? api[member].map((el) => JSON.stringify(el)) - : Object.getOwnPropertyNames(api[member]).map( - (key) => `${key}: ${JSON.stringify(api[member][key])}` - ), + value: api[member], }; }); };