Skip to content

Commit

Permalink
feat: add support to disable codesandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Oct 30, 2018
1 parent abe0001 commit be94b0e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/docz-core/src/bundlers/webpack/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export const mdx = (config: Config, args: Args) => {
.loader(require.resolve('@mdx-js/loader'))
.options({
...mdxConfig.config,
mdPlugins: mdPlugins.concat(mdxConfig.remarkPlugins),
hastPlugins: hastPlugins.concat(mdxConfig.rehypePlugins),
mdPlugins: mdPlugins.concat(mdxConfig.remarkPlugins()),
hastPlugins: hastPlugins.concat(mdxConfig.rehypePlugins(args)),
})
}

Expand Down
5 changes: 5 additions & 0 deletions packages/docz-core/src/commands/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface Argv {
hotPort: number
hotHost: string
native: boolean
codeSandbox: boolean
/* template args */
title: string
description: string
Expand Down Expand Up @@ -193,4 +194,8 @@ export const args = (env: Env) => (yargs: any) => {
type: 'boolean',
default: getEnv('docz.native', false),
})
yargs.positional('codeSandbox', {
type: 'boolean',
default: getEnv('docz.codeSandbox', true),
})
}
8 changes: 6 additions & 2 deletions packages/docz-core/src/config/mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import remarkDocz from 'remark-docz'
import rehypeDocz from 'rehype-docz'

import * as paths from './paths'
import { Config } from '../commands/args'

export const config = {
type: 'yaml',
marker: '-',
}

export const remarkPlugins = [matter, remarkDocz]
export const rehypePlugins = [rehypeDocz(paths.root), slug]
export const remarkPlugins = () => [matter, remarkDocz]
export const rehypePlugins = (config: Config) => [
rehypeDocz(paths.root, config.codeSandbox),
slug,
]
2 changes: 2 additions & 0 deletions packages/docz-core/src/states/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Payload {
version: string | null
repository: string | null
native: boolean
codeSandbox: boolean
}

const getInitialConfig = (config: Config): Payload => {
Expand All @@ -32,6 +33,7 @@ const getInitialConfig = (config: Config): Payload => {
version: get(pkg, 'version'),
repository: repoUrl,
native: config.native,
codeSandbox: config.codeSandbox,
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/docz-theme-default/src/components/ui/Render/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,20 @@ export class Render extends Component<RenderComponentProps, RenderState> {
<Action onClick={this.handleRefresh} title="Refresh playground">
<Refresh width={15} />
</Action>
{codesandbox !== 'undefined' && (
<ThemeConfig>
{config => (
<ThemeConfig>
{config =>
config.codeSandbox &&
codesandbox !== 'undefined' && (
<ActionLink
href={this.codesandboxUrl(config.native)}
target="_blank"
title="Open in CodeSandbox"
>
<CodeSandboxLogo style={{ height: '100%' }} width={15} />
</ActionLink>
)}
</ThemeConfig>
)}
)
}
</ThemeConfig>
<Clipboard content={showing === 'jsx' ? this.state.code : this.html} />
<Action
onClick={this.handleToggle}
Expand Down
27 changes: 20 additions & 7 deletions packages/rehype-docz/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const addPropsOnPlayground = async (
idx: number,
scopes: string[],
imports: string[],
cwd: string
cwd: string,
useCodeSandbox: boolean
) => {
const name = jsx.componentName(node.value)
const tagOpen = new RegExp(`^\\<${name}`)
Expand All @@ -23,32 +24,44 @@ const addPropsOnPlayground = async (
const code = formatted.slice(1, Infinity)
const scope = `{props,${scopes.join(',')}}`
const child = jsx.sanitizeCode(jsx.removeTags(code))
const codesandBoxInfo = await getSandboxImportInfo(child, imports, cwd)

node.value = node.value.replace(
tagOpen,
`<${name} __position={${idx}} __codesandbox={\`${codesandBoxInfo}\`} __code={\`${child}\`} __scope={${scope}}`
`<${name} __position={${idx}} __code={\`${child}\`} __scope={${scope}}`
)

if (useCodeSandbox) {
const codesandBoxInfo = await getSandboxImportInfo(child, imports, cwd)

node.value = node.value.replace(
tagOpen,
`<${name} __codesandbox={\`${codesandBoxInfo}\`}`
)
}
}
}

const addComponentsProps = (
scopes: string[],
imports: string[],
cwd: string
cwd: string,
useCodeSandbox: boolean
) => async (node: any, idx: number) => {
await addPropsOnPlayground(node, idx, scopes, imports, cwd)
await addPropsOnPlayground(node, idx, scopes, imports, cwd, useCodeSandbox)
}

export default (root: string) => () => (tree: any, fileInfo: any) => {
export default (root: string, useCodeSandbox: boolean) => () => (
tree: any,
fileInfo: any
) => {
const importNodes = tree.children.filter((node: any) => is('import', node))
const imports: string[] = flatten(importNodes.map(imps.getFullImports))
const scopes: string[] = flatten(importNodes.map(imps.getImportsVariables))
const fileCwd = path.relative(root, path.dirname(fileInfo.history[0]))

const nodes = tree.children
.filter((node: any) => is('jsx', node))
.map(addComponentsProps(scopes, imports, fileCwd))
.map(addComponentsProps(scopes, imports, fileCwd, useCodeSandbox))

return Promise.all(nodes).then(() => tree)
}

0 comments on commit be94b0e

Please sign in to comment.