forked from Autodesk/react-base-table
-
Notifications
You must be signed in to change notification settings - Fork 0
Generate API docs from ts/tsx files #3
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
Merged
kohakukun
merged 7 commits into
kohakukun:typescript_port
from
galibhassan:TSDocGen_Rebase
Oct 17, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ce0a3f1
fix cellRenderer
kohakukun db2d71c
Generate API docs from ts/tsx files. Localize the relavent gatsby plu…
galibhassan 1012a10
cleaning up
galibhassan a3839e5
addressing PR comments
galibhassan b497e78
point to the tsconfig for the parser, clean up ' * as React' importing
galibhassan 9c44422
cleaning up local plugin folder
galibhassan 5c976de
removing local 'extend-node-type.js', more cleanup
galibhassan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ styles.css | |
es/ | ||
lib/ | ||
coverage/ | ||
website/_build/_doc/sampleDoc.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
website/plugins/gatsby-transformer-react-docgen-typescript/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
# gatsby-transformer-react-docgen-typescript | ||
Parses inline component-documentation using | ||
[react-docgen](https://github.com/reactjs/react-docgen), | ||
[react-docgen-typescript](https://github.com/styleguidist/react-docgen-typescript) and | ||
[gatsby-transform-react-docgen](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-react-docgen) | ||
|
||
## Usage | ||
|
||
Add a plugin-entry to your `gatsby-config.js` | ||
|
||
```js | ||
module.exports = { | ||
plugins: [`gatsby-transformer-react-docgen-typescript`], | ||
} | ||
``` | ||
|
||
## How to query | ||
|
||
An example _graphql_ query to get nodes: | ||
|
||
```graphql | ||
{ | ||
allComponentMetadata { | ||
edges { | ||
node { | ||
displayName | ||
description | ||
props { | ||
name | ||
type | ||
required | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` |
4 changes: 4 additions & 0 deletions
4
website/plugins/gatsby-transformer-react-docgen-typescript/gatsby-node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
|
||
exports.onCreateNode = require(`./on-node-create`).default; | ||
exports.setFieldsOnGraphQLNodeType = require(`gatsby-transformer-react-docgen/extend-node-type`).default; |
206 changes: 206 additions & 0 deletions
206
website/plugins/gatsby-transformer-react-docgen-typescript/on-node-create.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
const path = require('path') | ||
var tsxParser = require('react-docgen-typescript') | ||
|
||
try { | ||
tsxParser = tsxParser.withCustomConfig( | ||
path.resolve(__dirname, '../../../tsconfig.json'), | ||
{ | ||
propFilter(prop) { | ||
if (prop.parent) { | ||
return !prop.parent.fileName.includes('node_modules') | ||
} | ||
return true | ||
}, | ||
} | ||
) | ||
} catch (err) { | ||
console.log('Error in initiating react-docgen-typescript: ', err) | ||
} | ||
|
||
var _interopRequireDefault = require(`@babel/runtime/helpers/interopRequireDefault`) | ||
|
||
exports.__esModule = true | ||
exports.default = onCreateNode | ||
|
||
const _parse = _interopRequireDefault( | ||
require('gatsby-transformer-react-docgen/parse') | ||
) | ||
|
||
const propsId = (parentId, name) => `${parentId}--ComponentProp-${name}` | ||
|
||
const descId = parentId => `${parentId}--ComponentDescription` | ||
|
||
function canParse(node) { | ||
return ( | ||
node && | ||
(node.internal.mediaType === `application/javascript` || | ||
// TypeScript doesn't really have a mime type and .ts files are a media file :/ | ||
node.internal.mediaType === `application/typescript` || | ||
node.internal.mediaType === `text/jsx` || | ||
node.internal.mediaType === `text/tsx` || | ||
node.extension === `tsx` || | ||
node.extension === `ts`) | ||
) | ||
} | ||
|
||
function createDescriptionNode( | ||
node, | ||
entry, | ||
actions, | ||
createNodeId, | ||
createContentDigest | ||
) { | ||
const { createNode } = actions | ||
delete node.description | ||
const descriptionNode = { | ||
id: createNodeId(descId(node.id)), | ||
parent: node.id, | ||
children: [], | ||
text: entry.description, | ||
internal: { | ||
type: `ComponentDescription`, | ||
mediaType: `text/markdown`, | ||
content: entry.description, | ||
contentDigest: createContentDigest(entry.description), | ||
}, | ||
} | ||
node.description___NODE = descriptionNode.id | ||
node.children = node.children.concat([descriptionNode.id]) | ||
createNode(descriptionNode) | ||
return node | ||
} | ||
|
||
function createPropNodes( | ||
node, | ||
component, | ||
actions, | ||
createNodeId, | ||
createContentDigest | ||
) { | ||
const { createNode } = actions | ||
|
||
let children = new Array(component.props.length) | ||
|
||
let propNames | ||
let currentProps | ||
if (Array.isArray(component.props)) { | ||
currentProps = component.props | ||
} else { | ||
propNames = Object.keys(component.props) | ||
currentProps = propNames.map(propName => { | ||
return component.props[propName] | ||
}) | ||
} | ||
|
||
currentProps.length > 0 && | ||
currentProps.forEach((prop, i) => { | ||
let propNodeId = propsId(node.id, prop.name) | ||
let content = JSON.stringify(prop) | ||
let propNode = Object.assign({}, prop, { | ||
id: createNodeId(propNodeId), | ||
children: [], | ||
parent: node.id, | ||
parentType: prop.type, | ||
internal: { | ||
type: `ComponentProp`, | ||
contentDigest: createContentDigest(content), | ||
}, | ||
}) | ||
children[i] = propNode.id | ||
propNode = createDescriptionNode( | ||
propNode, | ||
prop, | ||
actions, | ||
createNodeId, | ||
createContentDigest | ||
) | ||
createNode(propNode) | ||
}) | ||
node.props___NODE = children | ||
node.children = node.children.concat(children) | ||
return node | ||
} | ||
|
||
async function onCreateNode( | ||
{ | ||
node, | ||
loadNodeContent, | ||
actions, | ||
createNodeId, | ||
reporter, | ||
createContentDigest, | ||
}, | ||
pluginOptions | ||
) { | ||
const { createNode, createParentChildLink } = actions | ||
|
||
if (!canParse(node)) return | ||
|
||
const content = await loadNodeContent(node) | ||
let components | ||
|
||
const filepath = path.resolve(node.absolutePath) | ||
try { | ||
if ( | ||
!filepath.includes('examples') && | ||
(filepath.includes('.ts') || filepath.includes('.tsx')) | ||
) { | ||
console.log('parsing: ', filepath) | ||
components = tsxParser.parse(filepath) | ||
} else { | ||
components = (0, _parse.default)(content, node, pluginOptions) | ||
} | ||
} catch (err) { | ||
reporter.error( | ||
`There was a problem parsing component metadata for file: "${ | ||
node.relativePath | ||
}"`, | ||
err | ||
) | ||
return | ||
} | ||
|
||
components.forEach(component => { | ||
const strContent = JSON.stringify(component) | ||
const contentDigest = createContentDigest(strContent) | ||
const nodeId = `${node.id}--${component.displayName}--ComponentMetadata` | ||
let metadataNode = Object.assign({}, component, { | ||
props: null, | ||
// handled by the prop node creation | ||
id: createNodeId(nodeId), | ||
children: [], | ||
parent: node.id, | ||
internal: { | ||
contentDigest, | ||
type: `ComponentMetadata`, | ||
}, | ||
}) | ||
createParentChildLink({ | ||
parent: node, | ||
child: metadataNode, | ||
}) | ||
metadataNode = createPropNodes( | ||
metadataNode, | ||
component, | ||
actions, | ||
createNodeId, | ||
createContentDigest | ||
) | ||
metadataNode = createDescriptionNode( | ||
metadataNode, | ||
component, | ||
actions, | ||
createNodeId, | ||
createContentDigest | ||
) | ||
|
||
if (metadataNode.children) { | ||
for (let i = 0; i < metadataNode.children.length; i++) { | ||
if (metadataNode.children[i] === undefined) { | ||
metadataNode.children.splice(i, 1) | ||
} | ||
} | ||
} | ||
createNode(metadataNode) | ||
}) | ||
} |
3 changes: 3 additions & 0 deletions
3
website/plugins/gatsby-transformer-react-docgen-typescript/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "gatsby-transformer-react-docgen-typescript" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would skim this package to the minimum since you are not publishing or anything, I would only leave the name to avoid confusions