/**
* Copyright (c) 2015-present, 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.
*
* @providesModule AutodocsLayout
*/
var DocsSidebar = require('DocsSidebar');
var H = require('Header');
var Header = require('Header');
var Marked = require('Marked');
var Prism = require('Prism');
var React = require('React');
var Site = require('Site');
var slugify = require('slugify');
var styleReferencePattern = /^[^.]+\.propTypes\.style$/;
var ComponentDoc = React.createClass({
renderType: function(type) {
if (type.name === 'enum') {
if (typeof type.value === 'string') {
return type.value;
}
return 'enum(' + type.value.map((v => v.value)).join(', ') + ')';
}
if (type.name === 'shape') {
return '{' + Object.keys(type.value).map((key => key + ': ' + this.renderType(type.value[key]))).join(', ') + '}';
}
if (type.name == 'union') {
return type.value.map(this.renderType).join(', ');
}
if (type.name === 'arrayOf') {
return '[' + this.renderType(type.value) + ']';
}
if (type.name === 'instanceOf') {
return type.value;
}
if (type.name === 'custom') {
if (styleReferencePattern.test(type.raw)) {
var name = type.raw.substring(0, type.raw.indexOf('.'));
return {name}#style
}
if (type.raw === 'EdgeInsetsPropType') {
return '{top: number, left: number, bottom: number, right: number}';
}
return type.raw;
}
if (type.name === 'stylesheet') {
return 'style';
}
if (type.name === 'func') {
return 'function';
}
return type.name;
},
renderProp: function(name, prop) {
return (
{prop.platforms && prop.platforms.map(platform =>
{platform}
)}
{name}
{' '}
{prop.type &&
{this.renderType(prop.type)}
}
{prop.type && prop.type.name === 'stylesheet' &&
this.renderStylesheetProps(prop.type.value)}
{prop.description && {prop.description}}
);
},
renderCompose: function(name) {
return (
);
},
renderStylesheetProps: function(stylesheetName) {
var style = this.props.content.styles[stylesheetName];
return (
{(style.composes || []).map((name) => {
var link;
if (name === 'LayoutPropTypes') {
name = 'Flexbox';
link =
{name}...;
} else if (name === 'TransformPropTypes') {
name = 'Transforms';
link =
{name}...;
} else {
name = name.replace('StylePropTypes', '');
link =
{name}#style...;
}
return (
{link}
);
})}
{Object.keys(style.props).map((name) =>
{name}
{' '}
{style.props[name].type &&
{this.renderType(style.props[name].type)}
}
)}
);
},
renderProps: function(props, composes) {
return (
{(composes || []).map((name) =>
this.renderCompose(name)
)}
{Object.keys(props)
.sort((a, b) => {
a = props[a];
b = props[b];
if (a.platforms && !b.platforms) {
return 1;
}
if (b.platforms && !a.platforms) {
return -1;
}
return a.name < b.name;
})
.map((name) =>
this.renderProp(name, props[name])
)}
);
},
extractPlatformFromProps: function(props) {
for (var key in props) {
var prop = props[key];
var description = prop.description || '';
var platforms = description.match(/\@platform (.+)/);
platforms = platforms && platforms[1].replace(/ /g, '').split(',');
description = description.replace(/\@platform (.+)/, '');
prop.description = description;
prop.platforms = platforms;
}
},
render: function() {
var content = this.props.content;
this.extractPlatformFromProps(content.props);
return (
{content.description}
{this.renderProps(content.props, content.composes)}
);
}
});
var APIDoc = React.createClass({
removeCommentsFromDocblock: function(docblock) {
return docblock
.trim('\n ')
.replace(/^\/\*+/, '')
.replace(/\*\/$/, '')
.split('\n')
.map(function(line) {
return line.trim().replace(/^\* ?/, '');
})
.join('\n');
},
renderTypehintRec: function(typehint) {
if (typehint.type === 'simple') {
return typehint.value;
}
if (typehint.type === 'generic') {
return this.renderTypehintRec(typehint.value[0]) + '<' + this.renderTypehintRec(typehint.value[1]) + '>';
}
return JSON.stringify(typehint);
},
renderTypehint: function(typehint) {
try {
var typehint = JSON.parse(typehint);
} catch(e) {
return typehint;
}
return this.renderTypehintRec(typehint);
},
renderMethod: function(method) {
return (
{method.modifiers.length &&
{method.modifiers.join(' ') + ' '}
|| ''}
{method.name}
({method.params
.map((param) => {
var res = param.name;
if (param.typehint) {
res += ': ' + this.renderTypehint(param.typehint);
}
return res;
})
.join(', ')})
{method.docblock &&
{this.removeCommentsFromDocblock(method.docblock)}
}
);
},
renderMethods: function(methods) {
if (!methods.length) {
return null;
}
return (
Methods
{methods.filter((method) => {
return method.name[0] !== '_';
}).map(this.renderMethod)}
);
},
render: function() {
var content = this.props.content;
if (!content.methods) {
throw new Error(
'No component methods found for ' + content.componentName
);
}
return (
{this.removeCommentsFromDocblock(content.docblock)}
{this.renderMethods(content.methods)}
);
}
});
var HeaderWithGithub = React.createClass({
renderRunnableLink: function() {
if (this.props.metadata && this.props.metadata.runnable) {
return (
Run this example
);
}
},
render: function() {
return (
Edit on GitHub
{this.renderRunnableLink()}
{this.props.title}
);
}
});
var Autodocs = React.createClass({
renderFullDescription: function(docs) {
if (!docs.fullDescription) {
return;
}
return (
{docs.fullDescription}
);
},
renderExample: function(docs, metadata) {
if (!docs.example) {
return;
}
return (
{docs.example.content.replace(/^[\s\S]*?\*\//, '').trim()}
);
},
render: function() {
var metadata = this.props.metadata;
var docs = JSON.parse(this.props.children);
var content = docs.type === 'component' || docs.type === 'style' ?
:
;
return (
{metadata.title}
{content}
{this.renderFullDescription(docs)}
{this.renderExample(docs, metadata)}
);
}
});
module.exports = Autodocs;