-
Notifications
You must be signed in to change notification settings - Fork 534
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
Changes from 12 commits
ba7bd04
0373544
74b299c
a1c48f5
19a8957
2e4381c
798f091
e8a10cb
0b68b8c
aacbed0
365c902
2c346b0
b2a1355
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
|
@@ -35,3 +37,65 @@ exports.onCreateWebpackConfig = ({actions, plugins, loaders, getConfig}) => { | |
|
||
actions.replaceWebpackConfig(config) | ||
} | ||
|
||
exports.sourceNodes = ({actions, createNodeId, createContentDigest}) => { | ||
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 => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✍️ |
||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does every component have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, according to the |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✍️ |
||
} | ||
} | ||
const node = Object.assign({}, component, nodeMeta) | ||
createNode(node) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✍️ Gatsby’s
sourceNodes
docs: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/#sourceNodes