Skip to content

Custom props parser support #118

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
merged 1 commit into from
Apr 10, 2016
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
14 changes: 14 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ You can change some settings in the `styleguide.config.js` file in your project
};
```

* **`propsParser`**<br>
Type: `Function`, optional<br>
Function that allows you to override the mechanism used to parse props from a source file. Default mechanism is using
[react-docgen](https://github.com/reactjs/react-docgen) to parse props.

```javascript
module.exports = {
// ...
propsParser: function(filePath, source) {
return require('react-docgen').parse(source);
}
}
```

* **`resolver`**<br>
Type: `Function`, optional<br>
Function that allows you to override the mechanism used to identify classes/components to analyze. Default
Expand Down
9 changes: 7 additions & 2 deletions loaders/props.loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ var requirePlaceholder = '<%{#require#}%>';
module.exports = function (source) {
this.cacheable && this.cacheable();

var defaultPropsParser = function(filePath, source) {
return reactDocs.parse(source, config.resolver, config.handlers);
};

var jsonProps;
try {
var props = reactDocs.parse(source, config.resolver, config.handlers);
var propsParser = config.propsParser || defaultPropsParser;
var props = propsParser(this.request.split('!').pop(), source);

jsonProps = (isArray(props) ? props : [props]).map(function(doc) {
if (doc.description) {
Expand All @@ -25,7 +30,7 @@ module.exports = function (source) {
}

return JSON.stringify(doc).replace(
'"' + requirePlaceholder + '"',
'"' + requirePlaceholder + '"',
doc.doclets.example && 'require(' + JSON.stringify('examples!' + doc.doclets.example) + ')'
);
});
Expand Down