Skip to content

Commit 66f6989

Browse files
committed
Initial commit
0 parents  commit 66f6989

12 files changed

+854
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
npm-debug.log
2+
npm-debug.log.*
3+
node_modules/
4+
.DS_Store

.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
example/
2+
.gitignore

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# gulp-react-docs Change Log
2+
3+
## 0.1.0 - 2015.12.17
4+
5+
Initial publication of the `gulp-react-docs` plugin. The gulp plugin uses `[email protected]` and generates documentation for React components and the `propTypes` they expect. The plugin takes the following options at the moment:
6+
7+
- `outputPath`: If given, a link from the generated `.md` doc to the source code / file will be included in the generated output.
8+

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2015 AdRoll
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# gulp-react-docs
2+
3+
A [gulp](http://gulpjs.com/) plugin for generating documentation in Markdown format for React components based on their `propTypes`. The plugin uses [react-docgen](https://github.com/reactjs/react-docgen) to extract component prop information, and then render the markdown using [handlebars](http://handlebarsjs.com/).
4+
5+
This plugin is an extension of the automatic documentation generation tool we use at [AdRoll](http://tech.adroll.com) for our reusable component library as written about [here](http://tech.adroll.com/blog/frontend/2015/11/12/rollup-react-and-npm-at-adroll.html#automatic-documentation-generation).
6+
7+
## Installation
8+
9+
Install package with [npm](http://npmjs.org/) and add it to your development dependencies:
10+
11+
`npm install gulp-react-docs --save-dev`
12+
13+
## Information
14+
15+
<table>
16+
<tr>
17+
<td>Package</td><td>gulp-react-docs</td>
18+
</tr>
19+
<tr>
20+
<td>Description</td>
21+
<td>Generates Markdown documentation for React components based on component `propTypes`.</td>
22+
</tr>
23+
<tr>
24+
<td>Node Version</td>
25+
<td>>= 0.10</td>
26+
</tr>
27+
</table>
28+
29+
## Usage
30+
31+
```js
32+
var gulpReactDocs = require('gulp-react-docs');
33+
34+
gulp.task('react-docs', function() {
35+
var docsDest = 'docs';
36+
37+
return gulp.src('./components/**/*.jsx')
38+
.pipe(gulpReactDocs({
39+
path: docsDest
40+
}))
41+
.pipe(gulp.dest(docsDest));
42+
});
43+
```
44+
45+
For example usage, see our example `gulpfile.js` [here](./example/gulpfile.js).
46+
47+
For example output, see the generated docs [example/docs](./example/docs) generated from the files in [example/components](./example/components).
48+
49+
### Options
50+
The `gulp-react-docs` plugin can take an `options` object. The following attributes may be passed as part of the `options` object:
51+
52+
#### path
53+
54+
* Type: `string` OR `function`
55+
* Default: `undefined`
56+
57+
The path specifying the destination directory for your generated documentation files. This option is used to generate links from the output `.md` files to the source code. See the link produced below the heading [here](./example/docs/README.md#baz) for an example. If this option is not given, the link to the source code will not be generated. The `path` can be either a string or a function.
58+
59+
If you pass in a string, `path` should be the relative path from the `gulpfile.js` using the `gulp-react-docs` plugin to where the generated documentation will be output. The `path` will then be used to generate the relative path from the output documentation to the source code.
60+
61+
If you pass in a function, `path` is expected to return a string. The return value can be either a relative from where the generated documentation will be output to the source code, or an absolute path / URL pointing to the source code.
62+
63+
## Contributors
64+
65+
- [@marsjosephine](https://github.com/marsjosephine)
66+
- [@jtuulos](https://github.com/jtuulos)
67+
- [@jgrist](https://github.com/jgrist)

example/components/Baz.jsx

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
var React = require('react');
2+
3+
var Baz = React.createClass({
4+
5+
/**************************************************
6+
* Component Specs and lifecycle *
7+
**************************************************/
8+
9+
propTypes: {
10+
/**
11+
* The columns you want the Foo to have. Each column can have the
12+
* following attributes:
13+
* - `key` **(required)**: column identifier
14+
* - `label` **(required)**: Display text for the column. Should alread be
15+
* translated when passed to the Foo.
16+
* - `accessor` **(required)**: Function that returns relevant value from a
17+
* given data item. Later fed to the column `render` function.
18+
* - `render`: Function that takes the output of `accessor` and returns
19+
* what should be rendered for a given data item in that column. Should
20+
* return either a formatted value or can also be html. Columns without
21+
* `render` functions will not be displayed but can be used for filtering
22+
* (see the `filters` prop for more information).
23+
* - `widthMultiplier`: Number to multiply the width of the column relative
24+
* to other columns. By default, all columns are of equal width.
25+
*/
26+
columns: React.PropTypes.arrayOf(
27+
React.PropTypes.shape({
28+
key: React.PropTypes.string.isRequired,
29+
label: React.PropTypes.string.isRequired,
30+
accessor: React.PropTypes.func.isRequired,
31+
render: React.PropTypes.func,
32+
widthMultiplier: React.PropTypes.number
33+
})
34+
),
35+
/**
36+
* Array of data items. These data items can take whatever form or have
37+
* whatever attributes you'd like. As long as the column.accessor and
38+
* column.render functions accounts for the form or attributes of your
39+
* data items.
40+
*/
41+
data: React.PropTypes.array.isRequired,
42+
/**
43+
* CSS class(es) to add to a Baz. Can be a string or a function. If this
44+
* prop is a string, the class(es) will be attached to all Bazzes in the Foo.
45+
* If it is a function, the function will be called with the data item
46+
* to be rendered in the Baz and the classes returned will be applied
47+
* to that item's Baz when rendered. In the case of a function the return
48+
* value is expected to a string of space-separated classes.
49+
*/
50+
bazClassNames: React.PropTypes.oneOfType([
51+
React.PropTypes.string,
52+
React.PropTypes.func
53+
]),
54+
/**
55+
* An array of filters you want the Foo to have. When no filters are
56+
* given, no filter dropdowns will be shown. A filter object can have the
57+
* following properties:
58+
* - `columnKey` **(required)**: The key of the column in the `columns`
59+
* array the filter will do the filtering on.
60+
* - `options`: An array of options to display in the filter dropdown. Each
61+
* option is required to have a `value` and `label`.
62+
* - `defaultValue` **(required)**: A default value to render the filter with.
63+
* Should match one of the values in the `options` array.
64+
* - `filterCb` **(required)**: The predicate function used to filter the
65+
* items in the Foo when a selection for this filter is made. The `Foo`
66+
* will call this function with the option `value` as the first argument, and
67+
* the value of each data item (as they're being filtered) as the second
68+
* argument. The value of the data item is determined using the corresponding
69+
* columns (using `columnKey`) `accessor` function.
70+
* - `value`: Given value to the assign to the filter. If not given, this filter's
71+
* `defaultValue` will be used.
72+
*/
73+
filters: React.PropTypes.arrayOf(
74+
React.PropTypes.shape({
75+
columnKey: React.PropTypes.string.isRequired,
76+
options: React.PropTypes.arrayOf(
77+
React.PropTypes.shape({
78+
value: React.PropTypes.string.isRequired,
79+
label: React.PropTypes.string.isRequired
80+
})
81+
),
82+
defaultValue: React.PropTypes.string.isRequired,
83+
filterCb: React.PropTypes.func.isRequired,
84+
value: React.PropTypes.string
85+
})
86+
),
87+
/**
88+
* An array of column keys that designate which columns the search should
89+
* look for matches in. When no search columns are given, the search field
90+
* is not shown in the Foo.
91+
*/
92+
searchColumns: React.PropTypes.arrayOf(React.PropTypes.string),
93+
/**
94+
* The initial search query. Tokenized based on spaces in the string.
95+
*/
96+
searchQuery: React.PropTypes.string,
97+
/**
98+
* A function that when given a data item returns a list of possible actions
99+
* that can be taken for that item. if no `bazActions` are given, then no
100+
* actions will be shown. This function should return an array of either
101+
* a visual divider between action options **OR** objects with a `type` and
102+
* `label`.
103+
*/
104+
bazActions: React.PropTypes.func,
105+
/**
106+
* A React element that renders and controls secondary info.
107+
* This could be bulk action buttons, non-actionable information, etc.
108+
* Will be rendered to the right of the search and filters.
109+
*/
110+
secondaryInfo: React.PropTypes.element
111+
},
112+
113+
statics: {
114+
TEXT_ALIGN_LEFT : TEXT_ALIGN_LEFT,
115+
TEXT_ALIGN_RIGHT : TEXT_ALIGN_RIGHT,
116+
SORT_DIRECTION_DESC : SORT_DIRECTION_DESC,
117+
SORT_DIRECTION_ASC : SORT_DIRECTION_ASC
118+
},
119+
120+
getDefaultProps() {
121+
return {
122+
maxConfigOptions : 6,
123+
onSearch : _.noop,
124+
onFilter : _.noop,
125+
onSort : _.noop
126+
};
127+
},
128+
129+
/**************************************************
130+
* Rendering *
131+
**************************************************/
132+
133+
render: function() {
134+
return (
135+
<div>Hello World, this is a Baz element.</div>
136+
);
137+
}
138+
});
139+
140+
module.exports = Baz;

example/components/Foo.jsx

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var React = require('react');
2+
3+
var Foo = React.createClass({
4+
5+
/**************************************************
6+
* Component Specs && Lifecycle *
7+
**************************************************/
8+
9+
propTypes: {
10+
/**
11+
* Initial value for Foo component in Foo Bar format.
12+
* The Foo Bar format is enforced because it's the most reliable way to
13+
* parse Foos.
14+
*/
15+
initialValue: fooBarFormat,
16+
/**
17+
* Buttons for Foo presets (e.g. "Bar", "Baz"). Each button
18+
* should have the following properties:
19+
* - `label` **(required)**: Display text for the button.
20+
* - `value` **(required)**: The preset Foo or Foo range for the button.
21+
* For single mode, this function should return a string in Foo Bar format.
22+
* For `range` mode, this function should return an array
23+
* of strings in Foo Bar format.
24+
*/
25+
buttons: React.PropTypes.arrayOf(
26+
React.PropTypes.shape({
27+
label: React.PropTypes.string.isRequired,
28+
value: React.PropTypes.func.isRequired
29+
})
30+
),
31+
/**
32+
* Number of Foos to display side by side in the Baz.
33+
* - Defaults to 2.
34+
* - Max 2.
35+
*/
36+
foos: React.PropTypes.oneOf([1, 2]),
37+
/**
38+
* The first selectable Foo.
39+
*/
40+
disableBefore: React.PropTypes.string,
41+
/**
42+
* The last selectable Foo.
43+
*/
44+
disableAfter: React.PropTypes.string,
45+
/**
46+
* When a Foo is clicked in the Baz, this callback receives the
47+
* new selected range as the first argument.
48+
*
49+
* The selected range will be an array of 2 Foos, and each Foo in the
50+
* range is formatted as a Foo Bar Baz.
51+
*/
52+
onChange: React.PropTypes.func,
53+
/**
54+
* User-provided validator function for ranges a user is considering.
55+
* This prop can be used to implement custom range restrictions. This
56+
* function will be called with the range in the form of an array.
57+
*
58+
* For invalid ranges, this function should return an error message.
59+
* Otherwise, it should not return anything.
60+
*/
61+
validate: React.PropTypes.func,
62+
/**
63+
* Boolean for whether or not the Baz should include
64+
* Baz selection dropdowns.
65+
*/
66+
showBazSelector: React.PropTypes.bool
67+
},
68+
69+
statics: {
70+
FOO_BAR : BAR_BAZ,
71+
BAZ_FOO : BAR_FOO
72+
},
73+
74+
/**************************************************
75+
* Rendering *
76+
**************************************************/
77+
78+
render: function() {
79+
return (
80+
<div>Hello World, this is a Foo element.</div>
81+
);
82+
}
83+
});
84+
85+
module.exports = Foo;

0 commit comments

Comments
 (0)