Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constants section #121

Merged
merged 7 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/components/ConstantReference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import Markdown from 'react-markdown';
import PropTypes from 'prop-types';
import styles from './ConstantReference.module.scss';

const ConstantReference = ({ constant }) => {
const { name, values, type } = constant;
return (
<div className={styles.container}>
<div className={styles.name}>{name}</div>
<div>{type === 'array' ? '[' : '{'}</div>
<div className={styles.constantContainer}>
{values.map((value, i) => (
<Markdown key={i} source={value} />
))}
</div>
<div>{type === 'array' ? ']' : '}'}</div>
</div>
);
};

ConstantReference.propTypes = {
constant: PropTypes.shape({
type: PropTypes.string,
name: PropTypes.string,
values: PropTypes.array,
}),
};

export default ConstantReference;
22 changes: 22 additions & 0 deletions src/components/ConstantReference.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.container {
color: var(--color-neutrals-500);
font-family: var(--code-font);
font-size: 0.875rem;
line-height: 1.5;
margin-top: 2rem;
}

.name {
font-size: 1rem;
font-family: var(--primary-font-family);
color: var(--color-neutrals-800);
margin-bottom: 0.5rem;
}

.block {
margin-left: 0.5rem;
}

.constantContainer {
margin: 0 0 1rem 1.5rem;
}
22 changes: 22 additions & 0 deletions src/hooks/useApiDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,32 @@ const useApiDoc = (name) => {
return structuredTypeDefs;
};

const getConstants = (api) => {
return Object.getOwnPropertyNames(api)
.filter(
(member) =>
!IGNORED_METHODS.includes(member) &&
typeof api[member] !== 'function'
)
.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])}`
),
};
});
};

return {
description: apiDocs?.text,
usage: `import { ${name} } from 'nr1'`,
typeDefs: getTypeDefs(api),
constants: getConstants(api),
methods: Object.getOwnPropertyNames(api)
.filter(
(member) =>
Expand Down
11 changes: 11 additions & 0 deletions src/templates/ApiReferenceTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Container from '../components/Container';
import Layout from '../components/Layout';
import MethodReference from '../components/MethodReference';
import TypeDefReference from '../components/TypeDefReference';
import ConstantReference from '../components/ConstantReference';
import Sidebar from '../components/Sidebar';
import SEO from '../components/Seo';
import pages from '../data/sidenav.json';
Expand All @@ -24,6 +25,7 @@ const ApiReferenceTemplate = ({ data }) => {
methods = [],
usage = '',
typeDefs = [],
constants = [],
} = useApiDoc(api) ?? {};

return (
Expand Down Expand Up @@ -67,6 +69,15 @@ const ApiReferenceTemplate = ({ data }) => {
))}
</section>
)}

{constants.length > 0 && (
<section className={templateStyles.section}>
<h2>Constants</h2>
{constants.map((constant, i) => (
<ConstantReference key={i} constant={constant} />
))}
</section>
)}
</main>
</Container>
</Layout>
Expand Down