1 | # React Styleguide Generator
|
2 |
|
3 | [![CircleCI](https://img.shields.io/circleci/project/pocotan001/react-styleguide-generator.svg)](https://circleci.com/gh/pocotan001/react-styleguide-generator)
|
4 | [![npm](https://img.shields.io/npm/v/react-styleguide-generator.svg)](https://npmjs.org/package/react-styleguide-generator)
|
5 |
|
6 | Easily generate a good-looking styleguide by adding some documentation to your React project.
|
7 |
|
8 | ![preview](https://cloud.githubusercontent.com/assets/869065/8392279/7f3811ae-1d20-11e5-9707-864d5994ba49.png)
|
9 | [Demo](http://pocotan001.github.io/react-styleguide-generator/) using the [React-Bootstrap](http://react-bootstrap.github.io/).
|
10 |
|
11 | ## Installation
|
12 |
|
13 | ``` sh
|
14 | npm install react-styleguide-generator
|
15 | ```
|
16 |
|
17 | Which requires **React 0.13.0** or newer. To install it `npm install react`.
|
18 |
|
19 | ## Quick Start
|
20 |
|
21 | ### Documenting your React components
|
22 |
|
23 | Create file for the styleguide, and then add some documentation to a static field named `styleguide`. You can use the [ES6 syntax](https://github.com/lukehoban/es6features) by [Babel](https://babeljs.io/).
|
24 |
|
25 | **NOTE:** By default Babel's `static` keyword is disabled. You can turn them on individually by passing `stage 0` as a [babelrc](https://babeljs.io/docs/usage/babelrc/) or [options.babelConfig](#babelconfig).
|
26 |
|
27 | ``` js
|
28 | import React from 'react'
|
29 | import Button from './Button'
|
30 |
|
31 | export default class extends React.Component {
|
32 | static styleguide = {
|
33 | index: '1.1',
|
34 | category: 'Elements',
|
35 | title: 'Button',
|
36 | description: 'You can use **Markdown** within this `description` field.',
|
37 | code: `<Button size='small|large' onClick={Function}>Cool Button</Button>`,
|
38 | className: 'apply the css class'
|
39 | }
|
40 |
|
41 | onClick () {
|
42 | alert('Alo!')
|
43 | }
|
44 |
|
45 | render () {
|
46 | return (
|
47 | <Button size='large' onClick={this.onClick}>Cool Button</Button>
|
48 | )
|
49 | }
|
50 | }
|
51 | ```
|
52 |
|
53 | - `index`: Reference to the element's position in the styleguide (optional)
|
54 | - `category`: Components category name
|
55 | - `title`: Components title
|
56 | - `description`: Components description (optional)
|
57 | - `code`: Code examples (optional)
|
58 | - `className`: CSS class name (optional)
|
59 |
|
60 | If necessary, visit [react-styleguide-generator/example](https://github.com/pocotan001/react-styleguide-generator/tree/master/example) to see more complete examples for the documenting syntax.
|
61 |
|
62 | ### Generating the documentation
|
63 |
|
64 | #### Command line tool
|
65 |
|
66 | A common usage example is below.
|
67 |
|
68 | ``` sh
|
69 | # The default output to `styleguide` directory
|
70 | rsg 'example/**/*.js'
|
71 | ```
|
72 |
|
73 | Type `rsg -h` or `rsg --help` to get all the available options.
|
74 |
|
75 | ```
|
76 | Usage: rsg [input] [options]
|
77 |
|
78 | Options:
|
79 | -o, --output Output directory ['styleguide']
|
80 | -t, --title Used as a page title ['Style Guide']
|
81 | -r, --root Set the root path ['.']
|
82 | -f, --files Inject references to files ['']
|
83 | -c, --config Use the config file ['styleguide.json']
|
84 | -p, --pushstate Enable HTML5 pushState [false]
|
85 | -v, --verbose Verbose output [false]
|
86 |
|
87 | Examples:
|
88 | rsg 'example/**/*.js' -t 'Great Style Guide' -f 'a.css, a.js'
|
89 | ```
|
90 |
|
91 | #### Gulp
|
92 |
|
93 | ``` js
|
94 | var gulp = require('gulp')
|
95 | var RSG = require('react-styleguide-generator')
|
96 |
|
97 | gulp.task('styleguide', function (done) {
|
98 | var rsg = RSG('example/**/*.js', {
|
99 | output: 'path/to/dir',
|
100 | files: ['a.css', 'a.js']
|
101 | })
|
102 |
|
103 | rsg.generate(function (err) {
|
104 | if (err) {
|
105 | console.error(String(err))
|
106 | }
|
107 |
|
108 | done()
|
109 | })
|
110 | })
|
111 | ```
|
112 |
|
113 | ## API
|
114 |
|
115 | ### RSG(input, [options])
|
116 |
|
117 | Returns a new RSG instance.
|
118 |
|
119 | #### input
|
120 |
|
121 | Type: `String`
|
122 |
|
123 | Refers to [glob syntax](https://github.com/isaacs/node-glob) or it can be a direct file path.
|
124 |
|
125 | #### options
|
126 |
|
127 | ##### output
|
128 |
|
129 | Type: `String`
|
130 | Default: `'styleguide'`
|
131 |
|
132 | Output directory path.
|
133 |
|
134 | ##### title
|
135 |
|
136 | Type: `String`
|
137 | Default: `'Style Guide'`
|
138 |
|
139 | Used as a page title and in the page header.
|
140 |
|
141 | ##### root
|
142 |
|
143 | Type: `String`
|
144 | Default: `'.'`
|
145 |
|
146 | Set the root path. For example, if the styleguide is hosted at `http://example.com/styleguide` the `options.root` should be `styleguide`.
|
147 |
|
148 | ##### files
|
149 |
|
150 | Type: `Array`
|
151 | Default: `null`
|
152 |
|
153 | Inject references to files. A usage example is:
|
154 |
|
155 | ``` js
|
156 | {
|
157 | files: [
|
158 | '//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css',
|
159 | 'a.css',
|
160 | 'a.js',
|
161 | 'icon.svg'
|
162 | ]
|
163 | }
|
164 | ```
|
165 |
|
166 | Check for the existence of the files and only copy the files if it exists.
|
167 |
|
168 | ```
|
169 | styleguide/files
|
170 | ├─ a.css
|
171 | ├─ a.js
|
172 | └─ icon.svg
|
173 | ```
|
174 |
|
175 | Inject file references into index.html if the files with the extension `.css` or `.js`.
|
176 |
|
177 | ``` html
|
178 | <!doctype html>
|
179 | <html>
|
180 | <head>
|
181 | …
|
182 | <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
|
183 | <link rel="stylesheet" href="files/a.css">
|
184 | </head>
|
185 | <body>
|
186 | …
|
187 | <script src="files/a.js"></script>
|
188 | </body>
|
189 | </html>
|
190 | ```
|
191 |
|
192 | ##### config
|
193 |
|
194 | Type: `String`
|
195 | Default: `styleguide.json`
|
196 |
|
197 | The entire range of RSG API options is allowed. [Usage example](https://github.com/pocotan001/react-styleguide-generator/blob/master/example/styleguide.json).
|
198 |
|
199 | ##### pushstate
|
200 |
|
201 | Type: `String`
|
202 | Default: `false`
|
203 |
|
204 | Enable HTML5 pushState. When this option is enabled, styleguide will use history API.
|
205 |
|
206 | ##### babelConfig
|
207 |
|
208 | Type: `Object`
|
209 | Default: `null`
|
210 |
|
211 | A usage example is below. See the [babel docs](http://babeljs.io/docs/usage/options/) for the complete list.
|
212 |
|
213 | ``` js
|
214 | {
|
215 | babelConfig: {
|
216 | stage: 0
|
217 | }
|
218 | }
|
219 | ```
|
220 |
|
221 | ##### browserifyConfig
|
222 |
|
223 | Type: `Object`
|
224 | Default: `{ standalone: 'Contents', debug: true }`
|
225 |
|
226 | A usage example is below. See the [browserify docs](https://github.com/substack/node-browserify#browserifyfiles--opts) for the complete list.
|
227 |
|
228 | ``` js
|
229 | {
|
230 | extensions: ['', '.js', '.jsx']
|
231 | }
|
232 | ```
|
233 |
|
234 | ### rsg.generate([callback])
|
235 |
|
236 | Generate the files and their dependencies into a styleguide output.
|
237 |
|
238 | ## Demo
|
239 |
|
240 | Get the demo running locally:
|
241 |
|
242 | ``` sh
|
243 | git clone git@github.com:pocotan001/react-styleguide-generator.git
|
244 | cd react-styleguide-generator/example/
|
245 | npm install
|
246 | npm start
|
247 | ```
|
248 |
|
249 | Visit [http://localhost:3000/](http://localhost:3000/) in your browser.
|