Store typedef on the helper and use it to show a parameter list
This commit is contained in:
@@ -3,7 +3,7 @@ function getDocs() {
|
|||||||
// TODO: build if not present
|
// TODO: build if not present
|
||||||
const info = require('./build/api-info.json');
|
const info = require('./build/api-info.json');
|
||||||
|
|
||||||
return info.docs.filter(doc => !doc.ignore && (doc.api || doc.kind === 'module'));
|
return info.docs.filter(doc => !doc.ignore && (doc.api || doc.kind === 'module' || doc.kind === 'typedef'));
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPages({actions: {createPage}}) {
|
function createPages({actions: {createPage}}) {
|
||||||
|
|||||||
@@ -2,8 +2,9 @@ import {object} from 'prop-types';
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Markdown from 'react-markdown';
|
import Markdown from 'react-markdown';
|
||||||
import Code from './Code';
|
import Code from './Code';
|
||||||
|
import Parameter from './Parameter';
|
||||||
|
|
||||||
function Class({cls, module}) {
|
function Class({cls, module, helper}) {
|
||||||
const exportedName = module.getExportedName(cls.name);
|
const exportedName = module.getExportedName(cls.name);
|
||||||
let importCode;
|
let importCode;
|
||||||
if (exportedName === 'default') {
|
if (exportedName === 'default') {
|
||||||
@@ -17,13 +18,18 @@ function Class({cls, module}) {
|
|||||||
<h3>{cls.name}</h3>
|
<h3>{cls.name}</h3>
|
||||||
<Code value={importCode} />
|
<Code value={importCode} />
|
||||||
<Markdown source={cls.doc.classdesc} renderers={{code: Code}} />
|
<Markdown source={cls.doc.classdesc} renderers={{code: Code}} />
|
||||||
|
<h6>Parameters</h6>
|
||||||
|
<ul>
|
||||||
|
{cls.doc.params && cls.doc.params.map(param => <Parameter param={param} module={module} helper={helper} />)}
|
||||||
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Class.propTypes = {
|
Class.propTypes = {
|
||||||
cls: object.isRequired,
|
cls: object.isRequired,
|
||||||
module: object.isRequired
|
module: object.isRequired,
|
||||||
|
helper: object.isRequired
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Class;
|
export default Class;
|
||||||
@@ -2,8 +2,9 @@ import {object} from 'prop-types';
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Markdown from 'react-markdown';
|
import Markdown from 'react-markdown';
|
||||||
import Code from './Code';
|
import Code from './Code';
|
||||||
|
import Parameter from './Parameter';
|
||||||
|
|
||||||
function Func({func, module}) {
|
function Func({func, module, helper}) {
|
||||||
const exportedName = module.getExportedName(func.name);
|
const exportedName = module.getExportedName(func.name);
|
||||||
let importCode;
|
let importCode;
|
||||||
if (exportedName === 'default') {
|
if (exportedName === 'default') {
|
||||||
@@ -17,13 +18,18 @@ function Func({func, module}) {
|
|||||||
<h3>{func.name}</h3>
|
<h3>{func.name}</h3>
|
||||||
<Code value={importCode} />
|
<Code value={importCode} />
|
||||||
<Markdown source={func.doc.description} renderers={{code: Code}} />
|
<Markdown source={func.doc.description} renderers={{code: Code}} />
|
||||||
|
<h6>Parameters</h6>
|
||||||
|
<ul>
|
||||||
|
{func.doc.params && func.doc.params.map(param => <Parameter param={param} module={module} helper={helper} />)}
|
||||||
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Func.propTypes = {
|
Func.propTypes = {
|
||||||
func: object.isRequired,
|
func: object.isRequired,
|
||||||
module: object.isRequired
|
module: object.isRequired,
|
||||||
|
helper: object.isRequired
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Func;
|
export default Func;
|
||||||
@@ -3,23 +3,24 @@ import React from 'react';
|
|||||||
import Class from './Class';
|
import Class from './Class';
|
||||||
import Func from './Func';
|
import Func from './Func';
|
||||||
|
|
||||||
function Module({module}) {
|
function Module({module, helper}) {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<hr />
|
<hr />
|
||||||
<h2>{module.id}</h2>
|
<h2>{module.id}</h2>
|
||||||
{module.classes.map(cls => (
|
{module.classes.map(cls => (
|
||||||
<Class key={cls.name} cls={cls} module={module} />
|
<Class key={cls.name} cls={cls} module={module} helper={helper} />
|
||||||
))}
|
))}
|
||||||
{module.functions.map(func => (
|
{module.functions.map(func => (
|
||||||
<Func key={func.name} func={func} module={module} />
|
<Func key={func.name} func={func} module={module} helper={helper} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Module.propTypes = {
|
Module.propTypes = {
|
||||||
module: object.isRequired
|
module: object.isRequired,
|
||||||
|
helper: object.isRequired
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Module;
|
export default Module;
|
||||||
20
site/components/Parameter.jsx
Normal file
20
site/components/Parameter.jsx
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import {object} from 'prop-types';
|
||||||
|
import React from 'react';
|
||||||
|
import Type from './Type';
|
||||||
|
|
||||||
|
function Parameter({param, module, helper}) {
|
||||||
|
return (
|
||||||
|
<li>
|
||||||
|
<code>{param.name}</code> - {param.description} {param.optional && <span>(optional)</span>}<br/>
|
||||||
|
{param.type.names.map(longName => <Type longName={longName} module={module} helper={helper} />)}
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Parameter.propTypes = {
|
||||||
|
param: object.isRequired,
|
||||||
|
module: object.isRequired,
|
||||||
|
helper: object.isRequired
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Parameter;
|
||||||
@@ -13,7 +13,7 @@ function API({pageContext: {docs}}) {
|
|||||||
{helper.modules
|
{helper.modules
|
||||||
.filter(module => module.visible)
|
.filter(module => module.visible)
|
||||||
.map(module => (
|
.map(module => (
|
||||||
<Module key={module.id} module={module} />
|
<Module key={module.id} module={module} helper={helper} />
|
||||||
))}
|
))}
|
||||||
</Page>
|
</Page>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -5,6 +5,13 @@ class FunctionDoc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TypedefDoc {
|
||||||
|
constructor(doc) {
|
||||||
|
this.name = doc.name;
|
||||||
|
this.doc = doc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class ClassDoc {
|
class ClassDoc {
|
||||||
constructor(name) {
|
constructor(name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@@ -31,6 +38,7 @@ class ModuleDoc {
|
|||||||
processDoc(doc) {
|
processDoc(doc) {
|
||||||
if (doc.kind === 'module') {
|
if (doc.kind === 'module') {
|
||||||
this.doc = doc;
|
this.doc = doc;
|
||||||
|
//console.log('processing module: ' + doc.longname)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +102,7 @@ function moduleIDFromLongname(longname) {
|
|||||||
return match.groups.module;
|
return match.groups.module;
|
||||||
}
|
}
|
||||||
|
|
||||||
function nameFromLongname(longname) {
|
export function nameFromLongname(longname) {
|
||||||
const match = longname.match(longnameRE);
|
const match = longname.match(longnameRE);
|
||||||
if (!match) {
|
if (!match) {
|
||||||
throw new Error(`could not match name in longname: ${longname}`);
|
throw new Error(`could not match name in longname: ${longname}`);
|
||||||
@@ -146,7 +154,20 @@ class DocHelper {
|
|||||||
this.moduleLookup = {};
|
this.moduleLookup = {};
|
||||||
this.modules = [];
|
this.modules = [];
|
||||||
|
|
||||||
|
this.typedefLookup = {};
|
||||||
|
|
||||||
docs.forEach(doc => {
|
docs.forEach(doc => {
|
||||||
|
// typedef are indexed by long name
|
||||||
|
if (doc.kind === 'typedef') {
|
||||||
|
if (doc.name in this.typedefLookup) {
|
||||||
|
throw new Error(`Duplicate type definition ${doc.name} in ${this.id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const type = new TypedefDoc(doc);
|
||||||
|
this.typedefLookup[doc.longname] = type;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const moduleID = moduleIDFromLongname(doc.longname);
|
const moduleID = moduleIDFromLongname(doc.longname);
|
||||||
if (!(moduleID in this.moduleLookup)) {
|
if (!(moduleID in this.moduleLookup)) {
|
||||||
const module = new ModuleDoc(moduleID);
|
const module = new ModuleDoc(moduleID);
|
||||||
@@ -161,6 +182,11 @@ class DocHelper {
|
|||||||
this.modules.sort(byModuleId);
|
this.modules.sort(byModuleId);
|
||||||
this.modules.forEach(module => module.finalize());
|
this.modules.forEach(module => module.finalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getTypeDef(longName) {
|
||||||
|
this.typedefLookup[longName] && console.log(this.typedefLookup[longName]);
|
||||||
|
return this.typedefLookup[longName];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let cachedDocs;
|
let cachedDocs;
|
||||||
|
|||||||
Reference in New Issue
Block a user