From df006bca51864a731a5df7701d9dabe18100f6eb Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Thu, 19 Apr 2018 21:36:06 -0300 Subject: [PATCH] chore(docz): use category() instead of group --- examples/basic/src/components/Alert.doc.jsx | 3 +- examples/basic/src/components/Button.doc.jsx | 3 +- examples/basic/src/components/index.jsx | 4 -- examples/basic/src/hocs/index.jsx | 3 - examples/basic/src/hocs/withProps.doc.jsx | 4 +- packages/docz/index.d.ts | 67 ++++++++------------ packages/docz/src/Doc.ts | 54 ++++++++-------- packages/docz/src/Group.ts | 53 ---------------- packages/docz/tsconfig.json | 2 +- 9 files changed, 58 insertions(+), 135 deletions(-) delete mode 100644 packages/docz/src/Group.ts diff --git a/examples/basic/src/components/Alert.doc.jsx b/examples/basic/src/components/Alert.doc.jsx index 8a9a9e0..d0b8220 100644 --- a/examples/basic/src/components/Alert.doc.jsx +++ b/examples/basic/src/components/Alert.doc.jsx @@ -2,10 +2,9 @@ import React, { Fragment } from 'react' import { doc } from 'docz' import { Alert } from './Alert' -import { components } from './' doc('Alert') - .group(components) + .category('Components') .description('This component is used to show alerts') .section('Basic usage', () => Some message) .section('Using different kinds', () => ( diff --git a/examples/basic/src/components/Button.doc.jsx b/examples/basic/src/components/Button.doc.jsx index fe9a341..921d192 100644 --- a/examples/basic/src/components/Button.doc.jsx +++ b/examples/basic/src/components/Button.doc.jsx @@ -2,8 +2,7 @@ import React from 'react' import { doc } from 'docz' import { Button } from './Button' -import { components } from './' doc('Button') - .group(components) + .category('Components') .section(() => ) diff --git a/examples/basic/src/components/index.jsx b/examples/basic/src/components/index.jsx index 4a6bfca..daa3af0 100644 --- a/examples/basic/src/components/index.jsx +++ b/examples/basic/src/components/index.jsx @@ -1,6 +1,2 @@ -import { group } from 'docz' - export { Alert } from './Alert' export { Button } from './Button' - -export const components = group('Components') diff --git a/examples/basic/src/hocs/index.jsx b/examples/basic/src/hocs/index.jsx index 3287231..bc2ee60 100644 --- a/examples/basic/src/hocs/index.jsx +++ b/examples/basic/src/hocs/index.jsx @@ -1,4 +1 @@ -import { group } from 'docz' export { withProps } from './withProps' - -export const hocs = group('hocs') diff --git a/examples/basic/src/hocs/withProps.doc.jsx b/examples/basic/src/hocs/withProps.doc.jsx index 9f467af..71c789d 100644 --- a/examples/basic/src/hocs/withProps.doc.jsx +++ b/examples/basic/src/hocs/withProps.doc.jsx @@ -1,8 +1,6 @@ import React from 'react' import { doc } from 'docz' -import { hocs } from './' - doc('withProps') - .group(hocs) + .category('Hocs') .section(() =>
This section talk about withProps hoc
) diff --git a/packages/docz/index.d.ts b/packages/docz/index.d.ts index d296983..af92782 100644 --- a/packages/docz/index.d.ts +++ b/packages/docz/index.d.ts @@ -3,73 +3,60 @@ import { SFC, ReactNode, ComponentType } from 'react' export interface Section { id: string title?: string + code?: string render: () => ReactNode } -export interface GroupConstructorArgs { - name: string -} - -export class Group { - public constructor({ name }: GroupConstructorArgs) - - public route(value: string): Group - public order(num: number): Group - public toObject(): GroupObj - - public name: string -} - -export interface GroupObj { - id: string - name: string - order: number - route: string -} - export interface DocConstructorArgs { name: string } -export class Doc { - public constructor({ name }: DocConstructorArgs) +export interface DocObj { + readonly name: string + readonly route: string + readonly id: string | undefined + readonly order: number + readonly description: string | null + readonly filepath: string | undefined + readonly category: string | undefined + readonly sections: Section[] +} +export class Doc { + public constructor(name: string) public order(num: number): Doc - public group(group: Group): Doc + public category(name: string): Doc public route(value: string): Doc public description(value: string): Doc public section(...args: any[]): Doc public toObject(): DocObj - - public name: string } -export interface DocObj { - readonly id: string - readonly name: string - readonly group: GroupObj | null - readonly sections: Section[] - readonly description: string | null - readonly route: string - readonly order: number +export interface Entry { + id: string + name: string + filepath: string + importPath: string + sections: string[] } /** * Components */ -export function createTheme(WrappedComponent: ComponentType): ComponentType - export interface DocsRenderProps { - groups: GroupObj[] docs: DocObj[] + categories: string[] } export interface DocsProps { - children: ({ groups, docs }: DocsRenderProps) => ReactNode + children: (renderProps: DocsRenderProps) => ReactNode } -export const Docs: SFC +/** + * Api + */ export function doc(name: string): Doc -export function group(name: string): Group +export function theme(WrappedComponent: ComponentType): ComponentType +export const Docs: SFC diff --git a/packages/docz/src/Doc.ts b/packages/docz/src/Doc.ts index c904ea4..cd0f7f9 100644 --- a/packages/docz/src/Doc.ts +++ b/packages/docz/src/Doc.ts @@ -2,24 +2,23 @@ import { ulid } from 'ulid' import kebabcase from 'lodash.kebabcase' +import { cache } from './Docs' import { isFn, safeUrl } from './utils/helpers' -import { Section, DocConstructorArgs, DocObj, Group, GroupObj } from '../' -import { docsContainer } from './DocsContainer' +import { Section, DocObj, Entry } from '../' -class Doc { - private _id: string +export class Doc { private _name: string + private _id: string | undefined + private _filepath: string | undefined + private _category: string | undefined private _description: string | null - private _group: GroupObj | null private _sections: Section[] private _route: string private _order: number - constructor({ name }: DocConstructorArgs) { - this._id = ulid() + constructor(name: string) { this._name = name this._description = null - this._group = null this._sections = [] this._route = `/${kebabcase(name)}` this._order = 0 @@ -27,15 +26,9 @@ class Doc { return this } - /** - * setters - */ - - public group(group: Group): Doc { - const groupObj = group.toObject() - - this._group = groupObj - this._route = groupObj.route + this._route + public category(category: string): Doc { + this._category = category + this._route = `/${kebabcase(category)}` + this._route return this } @@ -68,21 +61,28 @@ class Doc { return this } - public get name(): string { - return this._name - } + public findEntryAndMerge(entries: Entry[]): Doc { + const entry = entries.find(entry => entry.name === this._name) - /** - * getters - */ + if (entry) { + this._id = entry.id + this._filepath = entry.filepath + this._sections.forEach((section, id) => { + section.code = entry.sections[id] + }) + } + + return this + } public toObject(): DocObj { return { - description: this._description, - group: this._group, id: this._id, name: this._name, order: this._order, + category: this._category, + description: this._description, + filepath: this._filepath, route: this._route, sections: this._sections, } @@ -90,8 +90,8 @@ class Doc { } export const doc = (name: string): Doc => { - const newDoc = new Doc({ name }) + const newDoc = new Doc(name) - docsContainer.addDoc(newDoc) + cache.set(name, newDoc) return newDoc } diff --git a/packages/docz/src/Group.ts b/packages/docz/src/Group.ts deleted file mode 100644 index e8e134d..0000000 --- a/packages/docz/src/Group.ts +++ /dev/null @@ -1,53 +0,0 @@ -/* tslint:disable:variable-name */ -import { ulid } from 'ulid' -import kebabcase from 'lodash.kebabcase' - -import { GroupConstructorArgs, GroupObj } from '../' -import { safeUrl } from './utils/helpers' -import { docsContainer } from './DocsContainer' - -class Group { - private _id: string - private _order: number - private _name: string - private _route: string - - constructor({ name }: GroupConstructorArgs) { - this._id = ulid() - this._name = name - this._route = safeUrl(`/${kebabcase(name)}`) - this._order = 0 - - return this - } - - public route(path: string): Group { - this._route = safeUrl(path) - return this - } - - public order(num: number): Group { - this._order = num - return this - } - - public toObject(): GroupObj { - return { - id: this._id, - name: this._name, - order: this._order, - route: this._route, - } - } - - public get name(): string { - return this._name - } -} - -export const group = (name: string): Group => { - const newGroup = new Group({ name }) - - docsContainer.addGroup(newGroup) - return newGroup -} diff --git a/packages/docz/tsconfig.json b/packages/docz/tsconfig.json index f248fed..c3e8d8b 100644 --- a/packages/docz/tsconfig.json +++ b/packages/docz/tsconfig.json @@ -4,7 +4,7 @@ "outDir": "dist", "rootDir": "src", "declaration": false, - "typeRoots": ["node_modules/@types", "index.d.ts"] + "typeRoots": ["node_modules/@types", "src/index.d.ts"] }, "include": ["src/**/*"], "exclude": ["node_modules/**"]