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

Generate prop documentation #1323

Merged
merged 13 commits into from
Jul 2, 2021
26 changes: 10 additions & 16 deletions docs/content/Avatar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
title: Avatar
---

import {Props} from '../src/props'
import {Avatar} from '@primer/components'

Avatars are images used to represent users and organizations on GitHub. Use the default round avatar for users, and the `square` prop for organizations or any other non-human avatars.

Expand All @@ -18,22 +20,14 @@ Avatars are images used to represent users and organizations on GitHub. Use the
```

## AvatarPair
```jsx live
<AvatarPair>
<Avatar src="https://github.com/avatars/github"/>
<Avatar src="https://github.com/avatars/primer"/>
</AvatarPair>
```

## System props

Avatar components get `COMMON` system props. Read our [System Props](/system-props) doc page for a full list of available props.
```jsx live
<AvatarPair>
<Avatar src="https://github.com/avatars/github" />
<Avatar src="https://github.com/avatars/primer" />
</AvatarPair>
```

## Component props
## Props

| Name | Type | Default | Description |
| :- | :- | :-: | :- |
| alt | String | | Passed through to alt on img tag |
| size | Number | 20 | adds the `avatar-small` class if less than 24 |
| src | String | | The source url of the avatar image |
| square | Boolean | false| Used to create a square avatar |
<Props of={Avatar} />
64 changes: 64 additions & 0 deletions docs/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const defines = require('../babel-defines')
const docgen = require('react-docgen-typescript')
const globby = require('globby')

exports.onCreateWebpackConfig = ({actions, plugins, loaders, getConfig}) => {
const config = getConfig()
Expand Down Expand Up @@ -35,3 +37,65 @@ exports.onCreateWebpackConfig = ({actions, plugins, loaders, getConfig}) => {

actions.replaceWebpackConfig(config)
}

exports.sourceNodes = ({actions, createNodeId, createContentDigest}) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const {createNode} = actions

// Extract compontent metadata from source files
const files = globby.sync(['../src/**/*.tsx'], {absolute: true})
const data = docgen.parse(files, {
savePropValueAsString: true,
propFilter: prop => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✍️ react-docgen-typescript’s propFilter docs: https://github.com/styleguidist/react-docgen-typescript#propfilter

if (prop.declarations !== undefined && prop.declarations.length > 0) {
const hasPropAdditionalDescription = prop.declarations.find(declaration => {
return !declaration.fileName.includes('node_modules')
})

return Boolean(hasPropAdditionalDescription)
}

return true
}
})

const components = data.map(component => {
return {
name: component.displayName,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does every component have a displayName?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

description: component.description,
props: Object.values(component.props)
.map(prop => {
return {
name: prop.name,
description: prop.description,
defaultValue: prop.defaultValue ? prop.defaultValue.value : '',
required: prop.required,
type: prop.type.name
}
})
// Move required props to beginning of the list
.sort((a, b) => {
if (a.required && !b.required) return -1
if (!a.required && b.required) return 1
return 0
})
}
})

// Add component metadata to GraphQL API
for (const component of components) {
const nodeContent = JSON.stringify(component)
const nodeMeta = {
id: createNodeId(component.name),
parent: null,
children: [],
internal: {
type: `ComponentMetadata`,
mediaType: `text/html`,
content: nodeContent,
contentDigest: createContentDigest(component)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
const node = Object.assign({}, component, nodeMeta)
createNode(node)
}
}
Loading