From d4de2a574c8ad0265f1f6bea52bd80ca9b1aab03 Mon Sep 17 00:00:00 2001 From: Glenn Date: Sun, 24 Jan 2016 22:36:51 -0800 Subject: [PATCH 1/2] Added ability to pass custom resolver function through to react-docgen Added ability for markdown renderer to handle more than one component per file --- README.md | 6 ++++++ index.js | 3 ++- src/react-docgen-md.js | 38 ++++++++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 058dfa1..d894d17 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,12 @@ If you pass in a string, `path` should be the relative path from the `gulpfile.j If you pass in a function, `path` is expected to return a string. The return value can be either a relative from where the generated documentation will be output to the source code, or an absolute path / URL pointing to the source code. +#### resolver + +* Type: `function` + +A resolver function to pass into react-docgen for identifying React Components from source code. The default resolver recognizes all React Components exported from a module, to supply your own custom resolver function, see the [react-docgen](https://github.com/reactjs/react-docgen) docs for more information. + ## Contributors - [@marsjosephine](https://github.com/marsjosephine) diff --git a/index.js b/index.js index 1bf22e3..b964baa 100644 --- a/index.js +++ b/index.js @@ -41,7 +41,8 @@ module.exports = function(options) { // get the markdown documentation for the file var markdownDoc = reactDocgenMarkdown(file.contents, { componentName : gUtil.replaceExtension(file.relative, ''), - srcLink : srcLink + srcLink : srcLink, + resolver : options.resolver }); // replace the file contents and extension diff --git a/src/react-docgen-md.js b/src/react-docgen-md.js index ff545dd..1d522cb 100644 --- a/src/react-docgen-md.js +++ b/src/react-docgen-md.js @@ -63,6 +63,9 @@ Handlebars.registerHelper('whichPartial', function(type) { return type && _.contains(partials, type.name) ? type.name : 'catchAll'; }); +Handlebars.registerHelper('name', function(componentName, displayName) { + return displayName || componentName; +}); /******************************************************** * General helpers * ********************************************************/ @@ -86,6 +89,12 @@ Handlebars.registerHelper('indent', function(indentLevel, options) { return lines.join('\n'); }); +Handlebars.registerHelper('each_with_sort', function(obj, opts) { + return _(obj).keys().sort().reduce(function(s, key) { + return s + opts.fn({ key: key, value: obj[key]}); + }, ''); +}); + /******************************************************** * Top-level handlebars template * ********************************************************/ @@ -93,29 +102,34 @@ Handlebars.registerHelper('indent', function(indentLevel, options) { var reactDocgenTemplate = Handlebars.compile('\ ## {{componentName}}\n\n\ {{#if srcLink }}From [`{{srcLink}}`]({{srcLink}})\n\n\{{/if}}\ -{{#if description}}{{{description}}}\n\n{{/if}}\ -{{#each props}}\ -#### {{@key}}\n\n\ +{{#each components}}\ +{{#if this.displayName}}### {{this.displayName}}\n\n\{{/if}}\ +{{#if this.description}}{{{this.description}}}\n\n{{/if}}\ +{{#each_with_sort this.props}}\ +#### {{key}}\n\n\ ```js\n\ -{{#if this.required}}// Required\n{{/if}}\ -{{#if this.defaultValue}}// Default: {{{this.defaultValue.value}}}\n{{/if}}\ -{{@key}}: {{> (whichPartial this.type) this.type level=0}}\n\ +{{#if value.required}}// Required\n{{/if}}\ +{{#if value.defaultValue}}// Default: {{{value.defaultValue.value}}}\n{{/if}}\ +{{key}}: {{> (whichPartial value.type) value.type level=0}}\n\ ```\n\n\ -{{#if this.description}}{{{this.description}}}\n\n{{/if}}\ -{{/each}}\ -

\n'); +{{#if value.description}}{{{value.description}}}\n\n{{/if}}\ +{{/each_with_sort}}\ +

\n\ +{{/each}}'); /******************************************************** * Documentation generator using react-docgen * ********************************************************/ var reactDocgenMarkdown = function(componentSrc, options) { - var docs = reactDocs.parse(componentSrc); + var docs = reactDocs.parse(componentSrc,options.resolver); + if (!docs instanceof Array) { + docs = [docs]; + } return reactDocgenTemplate({ srcLink : options.srcLink, componentName : options.componentName, - description : docs.description, - props : sortObjectByKey(docs.props) + components : docs }); }; From 8b03c3b108b429705eca7810a6cdf28aba614585 Mon Sep 17 00:00:00 2001 From: Glenn Date: Sun, 24 Jan 2016 22:41:34 -0800 Subject: [PATCH 2/2] Removed unused helper function --- src/react-docgen-md.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/react-docgen-md.js b/src/react-docgen-md.js index 1d522cb..e3a30c7 100644 --- a/src/react-docgen-md.js +++ b/src/react-docgen-md.js @@ -63,9 +63,6 @@ Handlebars.registerHelper('whichPartial', function(type) { return type && _.contains(partials, type.name) ? type.name : 'catchAll'; }); -Handlebars.registerHelper('name', function(componentName, displayName) { - return displayName || componentName; -}); /******************************************************** * General helpers * ********************************************************/