Skip to content

Commit

Permalink
docs(CodeSandbox): fix paths for images in exported examples (#3758)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored Aug 22, 2019
1 parent a6e5824 commit 2720a2d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as _ from 'lodash'
import PropTypes from 'prop-types'
import * as React from 'react'
import CodeSandboxer from 'react-codesandboxer'
import { Menu } from 'semantic-ui-react'

import { externals } from 'docs/src/components/ComponentDoc/ExampleEditor/renderConfig'
import createPackageJson from './createPackageJson'
import enhanceExampleCode from './enhanceExampleCode'

const appTemplate = `import React from "react";
import ReactDOM from "react-dom";
Expand Down Expand Up @@ -47,7 +47,6 @@ ReactDOM.render(
document.getElementById("root")
);
`
const dependencies = _.mapValues(externals, () => 'latest')

class ComponentControlsCodeSandbox extends React.Component {
static propTypes = {
Expand Down Expand Up @@ -94,11 +93,10 @@ class ComponentControlsCodeSandbox extends React.Component {
<CodeSandboxer
afterDeploy={this.handleDeploy}
examplePath='/'
example={exampleCode}
dependencies={dependencies}
name='semantic-ui-react-example'
example={enhanceExampleCode(exampleCode)}
providedFiles={{
'index.js': { content: appTemplate },
'package.json': createPackageJson(),
}}
skipRedirect
template='create-react-app'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as _ from 'lodash'
import { externals } from 'docs/src/components/ComponentDoc/ExampleEditor/renderConfig'

const name = 'semantic-ui-example'
const description = 'An exported example from Semantic UI React, https://react.semantic-ui.com/'
const dependencies = {
..._.mapValues(externals, () => 'latest'),
// required to enable all features due old templates in https://github.com/codesandbox/codesandbox-importers
'react-scripts': 'latest',
}

const createPackageJson = () => ({
content: JSON.stringify(
{
name,
version: '1.0.0',
description,
main: 'index.js',
dependencies,
},
null,
2,
),
})

export default createPackageJson
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const imagesRegex = /\/images\//gm

const enhanceExampleCode = (code) => {
// To have absolute paths on CodeSandbox for images
return code.replace(imagesRegex, 'https://react.semantic-ui.com/images/')
}

export default enhanceExampleCode
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as docsComponents from '@stardust-ui/docs-components'
import faker from 'faker'
import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
import * as SUIR from 'semantic-ui-react'

Expand All @@ -20,8 +21,9 @@ export const externals = {
'@stardust-ui/docs-components': docsComponents,
faker,
lodash: require('lodash'),
react: React,
'prop-types': PropTypes,
react: React,
'react-dom': ReactDOM,
'semantic-ui-react': SUIR,
}

Expand Down

0 comments on commit 2720a2d

Please sign in to comment.