Skip to content

Commit 82f8bc1

Browse files
committed
initial commit
0 parents  commit 82f8bc1

File tree

8 files changed

+206
-0
lines changed

8 files changed

+206
-0
lines changed

.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Node template
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
13+
# Directory for instrumented libs generated by jscoverage/JSCover
14+
lib-cov
15+
16+
# Coverage directory used by tools like istanbul
17+
coverage
18+
19+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
20+
.grunt
21+
22+
# node-waf configuration
23+
.lock-wscript
24+
25+
# Compiled binary addons (http://nodejs.org/api/addons.html)
26+
build/Release
27+
28+
# Dependency directory
29+
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
30+
node_modules
31+

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013-2015 SourceJS
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 all
13+
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 THE
21+
SOFTWARE.
22+

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# SourceJS auto React doc builder middleware.
2+
3+
[react-docgen](https://github.com/reactjs/react-docgen) integration plugin, renders React components information into [SourceJS](http://sourcejs.com) Spec page.
4+
5+
Compatible with [SourceJS](http://sourcejs.com) 0.6.0+.
6+
7+
## Install
8+
9+
To install middleware, run npm command in `sourcejs/user` folder:
10+
11+
```
12+
npm install sourcejs-react-docgen --save
13+
```
14+
15+
After restarting your app, middleware will be loaded automatically. To disable it, remove npm module and restart the app.
16+
17+
## Usage
18+
19+
After installing the middleware, during spec load, plugin will try to find `<specPath>/index.jsx` or `<specPath>/src/index.jsx` file, analyze it and expose raw and rendered data objects. Data will be then available within [EJS Spec pre-rendering](http://sourcejs.com/docs/spec-helpers/#native-templating) (enabled by default).
20+
21+
Insert these code snippets anywhere you want in your Spec file
22+
23+
```html
24+
<h1>My Spec</h1>
25+
26+
<section class="source_section">
27+
<h2>Default Example</h2>
28+
29+
<p><%- info.__docGenRaw.description %></p>
30+
31+
<%- info.__docGenHTML %>
32+
33+
<div class="source_example"></div>
34+
</section>
35+
```
36+
37+
Other custom Spec file syntax with [sourcejs-react](https://github.com/szarouski/sourcejs-react) and [sourcejs-md-react](https://github.com/mik01aj/sourcejs-md-react) plugins is also supported.
38+
39+
Check usage examples in [react-styleguide-example](https://github.com/sourcejs/react-styleguide-example).
40+
41+
### Exposed Data
42+
43+
* **info.__docGenRaw** - raw JSON from react-docgen
44+
* **info.__docGenHTML** - rendered table with properties
45+
46+
## TODO
47+
48+
* Add more configuration options
49+
50+
## Other SourceJS Middlewares
51+
52+
* https://github.com/sourcejs/sourcejs-jade
53+
* https://github.com/sourcejs/sourcejs-smiles
54+
55+
To create own SourceJS Middleware, we recommend using the official generator - https://github.com/sourcejs/generator-sourcejs.

core/middleware/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Node.js back-end part, included from main `app.js`.

core/middleware/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var reactDocs = require('react-docgen');
2+
var path = require('path');
3+
var fs = require('fs');
4+
var ejs = require('ejs');
5+
var specUtils = require(path.join(global.pathToApp,'core/lib/specUtils'));
6+
var currentDir = path.dirname(__filename);
7+
8+
// Module configuration
9+
var globalConfig = global.opts.plugins && global.opts.plugins.reactDocgen ? global.opts.plugins.reactDocgen : {};
10+
var config = {
11+
enabled: true,
12+
13+
// Public object is exposed to Front-end via options API.
14+
public: {}
15+
};
16+
17+
/*
18+
* @param {object} req - Request object
19+
* @param {object} res - Response object
20+
* @param {function} next - The callback function
21+
* */
22+
var processRequest = function (req, res, next) {
23+
if (!config.enabled) {
24+
next();
25+
return;
26+
}
27+
28+
// Check if request is targeting Spec
29+
if (req.specData && req.specData.renderedHtml) {
30+
var specPath = specUtils.getFullPathToSpec(req.path);
31+
var componentPath = path.join(specPath, 'src/index.jsx');
32+
var componentPathAlt = path.join(specPath, 'index.jsx');
33+
var componentPathExists = fs.existsSync(componentPath);
34+
var componentPathAltExists = fs.existsSync(componentPathAlt);
35+
36+
if (!(componentPathExists || componentPathAltExists)) {
37+
next();
38+
return;
39+
}
40+
41+
var componentPathToUse = componentPath ? componentPath : componentPathAlt;
42+
var componentContent = fs.readFileSync(componentPathToUse, 'utf-8');
43+
var propsTpl = fs.readFileSync(path.join(currentDir, '../templates/props.ejs'), 'utf-8');
44+
45+
var componentInfo = reactDocs.parse(componentContent);
46+
47+
req.specData.info.__docGenRaw = componentInfo;
48+
49+
try {
50+
req.specData.info.__docGenHTML = ejs.render(propsTpl, componentInfo);
51+
} catch(e) {
52+
console.log('error rendering docgen props', e);
53+
}
54+
55+
next();
56+
} else {
57+
next();
58+
}
59+
};
60+
61+
exports.process = processRequest;

core/templates/props.ejs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<table>
2+
<tr>
3+
<td>Property name</td>
4+
<td>Type</td>
5+
<td>Required</td>
6+
<td>Default value</td>
7+
<td>Description</td>
8+
</tr>
9+
<% for(var key in props) { %>
10+
<% var val = props[key] %>
11+
<tr>
12+
<td><%- key %></td>
13+
<td><%- val.type.name %></td>
14+
<td><%- val.required %></td>
15+
<td><%- val.defaultValue.value %></td>
16+
<td><%- val.description %></td>
17+
</tr>
18+
<% }; %>
19+
</table>

options.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
group: 'request'
3+
};

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "sourcejs-react-docgen",
3+
"version": "0.1.0",
4+
"description": "SourceJS auto React doc builder middleware.",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/sourcejs/sourcejs-react-docgen.git"
8+
},
9+
"homepage": "https://github.com/sourcejs/sourcejs-react-docgen",
10+
"dependencies": {
11+
"ejs": "^2.3.4",
12+
"react-docgen": "^2.2.0"
13+
}
14+
}

0 commit comments

Comments
 (0)