Skip to content

Commit

Permalink
Adds modified react-docgen scripts and md docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrem committed May 15, 2017
1 parent caec9e3 commit ecc29aa
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/components/Link.md
@@ -0,0 +1,22 @@
`Link` (component)
==================



Props
-----

### `linkData` (required)

type: `object`


### `orientation` (required)

type: `enum('horizontal'|'vertical')`


### `pathFunc` (required)

type: `enum('diagonal'|'elbow')`

73 changes: 73 additions & 0 deletions docs/components/Node.md
@@ -0,0 +1,73 @@
`Node` (component)
==================



Props
-----

### `circleRadius`

type: `number`
defaultValue: `10`


### `circleStyle`

type: `object`
defaultValue: `{
stroke: '#000',
strokeWidth: 2,
fill: 'grey',
}`


### `leafCircleStyle`

type: `object`
defaultValue: `{
stroke: '#000',
strokeWidth: 2,
fill: 'transparent',
}`


### `nodeData` (required)

type: `object`


### `onClick`

type: `func`


### `orientation` (required)

type: `enum('horizontal'|'vertical')`


### `primaryLabel`

type: `string`


### `primaryLabelStyle`

type: `object`


### `secondaryLabels`

type: `object`


### `secondaryLabelsStyle`

type: `object`


### `textAnchor`

type: `string`

42 changes: 42 additions & 0 deletions docs/components/Tree.md
@@ -0,0 +1,42 @@
`Tree` (component)
==================



Props
-----

### `collapsible`

type: `bool`
defaultValue: `true`


### `data` (required)

type: `array`


### `initialDepth`

type: `number`
defaultValue: `undefined`


### `orientation`

type: `enum('horizontal'|'vertical')`
defaultValue: `'horizontal'`


### `pathFunc`

type: `enum('diagonal'|'elbow')`
defaultValue: `'diagonal'`


### `translate`

type: `shape[object Object]`
defaultValue: `{ x: 0, y: 0 }`

42 changes: 42 additions & 0 deletions scripts/buildDocs.sh
@@ -0,0 +1,42 @@
#!/usr/bin/env node

/**
* This example script expects a JSON blob generated by react-docgen as input,
* e.g. react-docgen components/* | buildDocs.sh
*/

var fs = require('fs');
var generateMarkdown = require('./generateMarkdown');
var path = require('path');

var json = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
json += chunk;
}
});

process.stdin.on('end', function() {
buildDocs(JSON.parse(json));
});

function buildDocs(api) {
// api is an object keyed by filepath. We use the file name as component name.
var i = 0;
for (var filepath in api) {
console.log(filepath)
var targetDir = 'docs/components/';
var name = getComponentName(filepath);
var markdown = generateMarkdown(name, api[filepath]);
fs.writeFileSync(targetDir + name + '.md', markdown);
process.stdout.write(filepath + ' -> ' + targetDir + name + '.md\n');
i++;
}
}

function getComponentName(filepath) {
var name = path.dirname(filepath).split(path.sep).pop();
return name;
}
76 changes: 76 additions & 0 deletions scripts/generateMarkdown.js
@@ -0,0 +1,76 @@
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/


function stringOfLength(string, length) {
let newString = '';
for (let i = 0; i < length; i++) {
newString += string;
}
return newString;
}

function generateTitle(name) {
const title = `\`${name}\` (component)`;
return `${title}\n${stringOfLength('=', title.length)}\n`;
}

function generateDesciption(description) {
return `${description}\n`;
}

function generatePropType(type) {
let values;
if (Array.isArray(type.value)) {
values = `(${
type.value.map((typeValue) => typeValue.name || typeValue.value).join('|')
})`;
} else {
values = type.value;
}

return `type: \`${type.name}${values || ''}\`\n`;
}

function generatePropDefaultValue(value) {
return `defaultValue: \`${value.value}\`\n`;
}

function generateProp(propName, prop) {
return (
`### \`${propName}\`${prop.required ? ' (required)' : ''}\n` +
`\n${
prop.description ? `${prop.description}\n\n` : ''
}${prop.type ? generatePropType(prop.type) : ''
}${prop.defaultValue ? generatePropDefaultValue(prop.defaultValue) : ''
}\n`
);
}

function generateProps(props) {
const title = 'Props';

return (
`${title}\n${
stringOfLength('-', title.length)}\n` +
`\n${
Object.keys(props).sort().map((propName) => generateProp(propName, props[propName])).join('\n')}`
);
}

function generateMarkdown(name, reactAPI) {
const markdownString =
`${generateTitle(name)}\n${
generateDesciption(reactAPI.description)}\n${
generateProps(reactAPI.props)}`;

return markdownString;
}

module.exports = generateMarkdown;

0 comments on commit ecc29aa

Please sign in to comment.