Skip to content

Commit

Permalink
geosolutions-it#1475 Create markdown reference docs from react-docgen…
Browse files Browse the repository at this point in the history
… output
  • Loading branch information
fernandinand committed Feb 20, 2017
1 parent 7f13a44 commit bfcf52c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
66 changes: 66 additions & 0 deletions genDocs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env node

/**
* This example script expects a path do components/plugins as input,
* generating MD documentation files to docs/developer-guide/reference
* e.g. node genDocs web/client/components
*/

var fs = require('fs');
var path = require('path');
var docsToMarkdown = require('react-docs-markdown');
var reactDocs = require('react-docgen');
var rawLoader = require('raw-loader');

var dir = process.argv[2]
filelist = []


var walkSync = function(dir, filelist) {
files = fs.readdirSync(dir);
files.forEach(function(file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = walkSync(path.join(dir, file), filelist);
}
else {
// FIXME: check only jsx files?
checkExt(path.join(dir, file), '.jsx')
}
});
return filelist;
}

function checkExt(file, ext) {
if (path.extname(file) == ext) {
filelist.concat(file);
process.stdout.write(file + '\n');
var fileContent = require("raw-loader!./" + file);
var componentInfo = reactDocs.parse(fileContent);
buildDocs(JSON.parse(componentInfo));
}
}

function buildDocs(api) {
// api is an object keyed by filepath. We use the file name as component name.
for (var filepath in api) {
var name = getComponentName(filepath);
// Use fs to write the markdown to dist
var md = docsToMarkdown(api[filepath], name);
// FIXME: static output folder?
fs.writeFile('docs/developer-guide/reference/' + name +'.md', md, (err) => {
if (err) throw err;
});
process.stdout.write(filepath + ' -> ' + name + '.md\n');
}
}

function getComponentName(filepath) {
var name = path.basename(filepath);
var ext;
while ((ext = path.extname(name))) {
name = name.substring(0, name.length - ext.length);
}
return name;
}

walkSync(dir, filelist)
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"pdfviewer": "0.3.2",
"proj4": "2.3.14",
"qrcode.react": "0.6.1",
"raw-loader": "0.5.1",
"react": "0.14.8",
"react-addons-shallow-compare": "0.14.8",
"react-bootstrap": "0.28.1",
Expand All @@ -114,6 +115,8 @@
"react-copy-to-clipboard": "4.1.0",
"react-dnd": "2.1.3",
"react-dnd-html5-backend": "2.1.2",
"react-docgen": "2.13.0",
"react-docs-markdown": "0.1.7",
"react-dock": "0.2.3",
"react-dom": "0.14.8",
"react-draggable": "1.3.4",
Expand Down
6 changes: 6 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ module.exports = {
loader: "babel-loader",
include: path.join(__dirname, "web", "client")
}
],
rules: [
{
test: /\.jsx$/,
use: 'raw-loader'
}
]
},
devServer: {
Expand Down

0 comments on commit bfcf52c

Please sign in to comment.