Skip to content

Commit

Permalink
feat(docz): add routes from parsed entries
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 15, 2018
1 parent add17ad commit fc37f73
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
7 changes: 4 additions & 3 deletions packages/playgrodd-core/src/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export class Entry {
constructor({ src, file }: IConstructorParams) {
const ast = convertToAst(file)
const name = getNameFromDoc(ast) || ''
const route = path.join('/', path.parse(file).dir, name)
const source = path.relative(paths.root, src)
const filepath = path.relative(source, file)
const srcPath = path.resolve(paths.root, src)
const filepath = path.relative(path.relative(paths.root, src), file)
const dir = path.relative(srcPath, path.parse(file).dir)
const route = path.join('/', dir, name)

this.name = name
this.route = route
Expand Down
4 changes: 3 additions & 1 deletion packages/playgrodd-core/templates/app.tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import React from 'react'
import { hot } from 'react-hot-loader'
import { Theme } from '<%- THEME %>'

window.__DOCZ_ROUTES__ = <%- ROUTES %>

const _wrappers = [<%- WRAPPERS %>]

const recursiveWrappers = ([Wrapper, ...rest], props) => (
Expand All @@ -17,7 +19,7 @@ const Wrapper = props =>

const WrappedTheme = () => (
<Wrapper>
<Theme routes={<%- ROUTES %>} />
<Theme />
</Wrapper>
)

Expand Down
2 changes: 1 addition & 1 deletion packages/playgrodd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"fix:tslint": "tslint --fix --project ."
},
"dependencies": {
"playgrodd-core": "^0.0.1",
"playgrodd-bundler-webpack": "^0.0.1",
"playgrodd-core": "^0.0.1",
"playgrodd-theme-default": "^0.0.1",
"prop-types": "^15.6.1",
"react": "^16.3.1",
Expand Down
10 changes: 7 additions & 3 deletions packages/playgrodd/src/Doc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ulid } from 'ulid'
import { Section, DocConstructorArgs } from 'playgrodd'
import { container } from './DocsContainer'
import { docsContainer } from './DocsContainer'

const isFn = (value: any): boolean => typeof value === 'function'

Expand Down Expand Up @@ -72,7 +72,11 @@ export class Doc {
}

public get docRoute(): string {
return this._route
const ROUTES =
window && typeof window !== 'undefined' && (window as any).__DOCZ_ROUTES__

if (this._route !== `/${this._name}`) return this._route
return ROUTES ? ROUTES[this._name] : this._route
}

public get docOrder(): number {
Expand All @@ -83,6 +87,6 @@ export class Doc {
export const doc = (name: string): Doc => {
const newDoc = new Doc({ name })

container.addDoc(newDoc)
docsContainer.addDoc(newDoc)
return newDoc
}
6 changes: 4 additions & 2 deletions packages/playgrodd/src/DocsContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ interface DocsState {
export class DocsContainer extends Container<DocsState> {
constructor() {
super()
this.state = { docs: {} }
this.state = {
docs: {},
}
}

public addDoc(doc: Doc) {
Expand All @@ -20,4 +22,4 @@ export class DocsContainer extends Container<DocsState> {
}
}

export const container = new DocsContainer()
export const docsContainer = new DocsContainer()
13 changes: 10 additions & 3 deletions packages/playgrodd/src/components/create-theme.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { ComponentType } from 'react'
import * as React from 'react'
import { Router } from 'react-router-dom'
import { createBrowserHistory, History } from 'history'
import { Provider } from 'unstated'

import { container } from '../DocsContainer'
import { docsContainer } from '../DocsContainer'

export const history: History = createBrowserHistory()

interface ICreateThemeProps {
routes: {
[key: string]: string
}
}

interface ICreateTheme {
(WrappedComponent: React.ComponentType): React.ComponentType
(WrappedComponent: ComponentType): ComponentType<ICreateThemeProps>
}

export const createTheme: ICreateTheme = WrappedComponent => () => (
<Router history={history}>
<Provider inject={[container]}>
<Provider inject={[docsContainer]}>
<WrappedComponent />
</Provider>
</Router>
Expand Down

0 comments on commit fc37f73

Please sign in to comment.