Skip to content

Commit

Permalink
feat(gatsby-theme-docz): improve code highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 6, 2019
1 parent 125c5cf commit d265444
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 153 deletions.
4 changes: 0 additions & 4 deletions core/gatsby-theme-docz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"@mdx-js/mdx": "^1.0.21",
"@mdx-js/react": "^1.0.21",
"@theme-ui/typography": "^0.2.5",
"@xstate/react": "^0.7.0",
"babel-plugin-export-metadata": "^1.2.0",
"copy-text-to-clipboard": "^2.1.0",
"docz": "^1.2.0",
Expand All @@ -37,7 +36,6 @@
"gatsby-plugin-mdx": "^1.0.7",
"gatsby-plugin-react-helmet": "^3.1.0",
"gatsby-plugin-root-import": "^2.0.5",
"hotkeys-js": "^3.6.11",
"lodash": "^4.17.11",
"mdx-utils": "^0.2.0",
"prop-types": "^15.7.2",
Expand All @@ -53,8 +51,6 @@
"theme-ui": "^0.2.6",
"typescript": "3.5.2",
"typography-theme-moraga": "^0.16.19",
"webpack-chain": "^6.0.0",
"xstate": "^4.6.4",
"yargs": "^13.2.4"
},
"devDependencies": {
Expand Down
116 changes: 28 additions & 88 deletions core/gatsby-theme-docz/src/components/Code/index.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,35 @@
/** @jsx jsx */
import { jsx, useColorMode } from 'theme-ui'
import { useConfig } from 'docz'
import { get } from 'lodash/fp'
import AceEditor from 'react-ace'
/* eslint react/jsx-key: 0 */
import Highlight, { defaultProps } from 'prism-react-renderer'
import { jsx, Styled } from 'theme-ui'

import 'brace/theme/dracula'
import 'brace/theme/textmate'
import 'brace/mode/jsx'
import 'brace/ext/language_tools'
import 'brace/ext/searchbox'
import { usePrismTheme } from '~utils/theme'

import * as styles from './styles'

const LANGUAGES = [
'javascript',
'python',
'ruby',
'sass',
'markdown',
'mysql',
'json',
'html',
'golang',
'elixir',
'typescript',
'css',
]

LANGUAGES.forEach(lang => {
require(`brace/mode/${lang}`)
require(`brace/snippets/${lang}`)
})

const themes = {
light: 'textmate',
dark: 'dracula',
}

const getLanguage = lang => {
const defaultLanguage = 'jsx'
if (typeof lang === 'string') return defaultLanguage

const language = getter(lang, 'props.props.className') || defaultLanguage
const result = language.replace('language-', '')

if (result === 'js' || result === 'javascript') return 'jsx'
if (result === 'ts' || result === 'tsx' || result === 'typescript') {
return 'text/typescript'
}
return result
}

export const Code = ({
className,
codeString,
language = 'jsx',
readOnly,
onChange,
}) => {
const [colorMode] = useColorMode()
const config = useConfig()
const lang = getLanguage(language)
const theme = get('themeConfig.editorTheme', config)
export const Code = ({ children, className: outerClassName }) => {
const [language] = outerClassName.replace(/language-/, '').split(' ')
const theme = usePrismTheme()

return (
<AceEditor
className={className}
sx={styles.editor}
value={codeString}
mode={lang}
readOnly={readOnly}
onChange={onChange}
highlightActiveLine={false}
theme={theme || themes[colorMode]}
name="code-editor"
fontSize={16}
style={{
width: 'calc(100% - 2px)',
height: 200,
}}
setOptions={{
tabSize: 2,
minLines: 5,
maxLines: 20,
wrap: true,
autoScrollEditorIntoView: true,
printMargin: false,
}}
editorProps={{
$blockScrolling: Infinity,
}}
/>
<Highlight
{...defaultProps}
code={children.trim()}
language={language}
theme={theme}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<Styled.pre className={`${outerClassName} ${className}`} style={style}>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })}>
{line.map((token, key) => (
<span
{...getTokenProps({ token, key })}
sx={{ display: 'inline-block' }}
/>
))}
</div>
))}
</Styled.pre>
)}
</Highlight>
)
}
35 changes: 23 additions & 12 deletions core/gatsby-theme-docz/src/components/Playground/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/** @jsx jsx */
import { jsx } from 'theme-ui'
import { jsx, useThemeUI } from 'theme-ui'
import { useState } from 'react'
import { LiveProvider, LiveError, LivePreview } from 'react-live'
import { LiveProvider, LiveError, LivePreview, LiveEditor } from 'react-live'
import { get, merge } from 'lodash/fp'
import copy from 'copy-text-to-clipboard'

import { usePrismTheme } from '~utils/theme'
import * as styles from './styles'
import * as Icons from '../Icons'
import { Code } from '../Code'

export const Playground = ({ code: initialCode, scope }) => {
const [code, setCode] = useState(() => initialCode)
export const Playground = ({ code, scope }) => {
const [showingCode, setShowingCode] = useState(false)
const theme = usePrismTheme()
const ctx = useThemeUI()

const transformCode = code => {
if (code.startsWith('()') || code.startsWith('class')) return code
Expand All @@ -22,7 +24,17 @@ export const Playground = ({ code: initialCode, scope }) => {
}

return (
<LiveProvider code={code} scope={scope} transformCode={transformCode}>
<LiveProvider
code={code}
scope={scope}
transformCode={transformCode}
theme={merge(theme, {
plain: {
fontFamily: get('theme.fonts.monospace', ctx),
fontSize: 18,
},
})}
>
<div sx={styles.previewWrapper}>
<LivePreview sx={styles.preview(showingCode)} />
<div sx={styles.buttons}>
Expand All @@ -35,12 +47,11 @@ export const Playground = ({ code: initialCode, scope }) => {
</div>
</div>
<LiveError sx={styles.error} />
<Code
sx={styles.editor(showingCode)}
codeString={code}
onChange={setCode}
language="jsx"
/>
{showingCode && (
<div sx={styles.editor(theme)}>
<LiveEditor />
</div>
)}
</LiveProvider>
)
}
14 changes: 8 additions & 6 deletions core/gatsby-theme-docz/src/components/Playground/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const border = {
border: t => `1px solid ${t.colors.playground.border}`,
}

export const editor = showingCode => ({
...border,
display: showingCode ? 'block' : 'none',
m: 0,
mt: '-1px',
borderRadius: '0 0 5px 5px',
export const editor = theme => ({
p: 2,
border: t => `1px solid ${t.colors.border}`,
background: theme.plain.backgroundColor,
borderTop: 0,
'.npm__react-simple-code-editor__textarea': {
outline: 'none !important',
},
})

export const error = {
Expand Down
18 changes: 1 addition & 17 deletions core/gatsby-theme-docz/src/components/Pre/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
/** @jsx jsx */
import { jsx } from 'theme-ui'
import { preToCodeBlock } from 'mdx-utils'

import * as styles from './styles'
import { Code } from '../Code'

export const Pre = preProps => {
const props = preToCodeBlock(preProps)
if (props) {
return (
<div sx={styles.wrapper}>
<Code {...props} readOnly />
<span sx={styles.language}>{props.language}</span>
</div>
)
} else {
return <pre {...preProps} />
}
}
export const Pre = ({ children }) => <div>{children}</div>
2 changes: 1 addition & 1 deletion core/gatsby-theme-docz/src/components/Props/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const content = {
export const line = {
pt: 2,
'& + &': {
border: t => `1px solid ${t.colors.border}`,
borderTop: t => `1px solid ${t.colors.border}`,
},
}

Expand Down
2 changes: 2 additions & 0 deletions core/gatsby-theme-docz/src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as headings from './Headings'

import { Code } from './Code'
import { Layout } from './Layout'
import { Playground } from './Playground'
import { Pre } from './Pre'
import { Props } from './Props'

export const componentsMap = {
...headings,
code: Code,
playground: Playground,
pre: Pre,
layout: Layout,
Expand Down
6 changes: 5 additions & 1 deletion core/gatsby-theme-docz/src/theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { toTheme } from '@theme-ui/typography'
import { merge } from 'lodash/fp'

import * as modes from './modes'
import prism from './prism'

moraga.headerWeight = 700
const typography = toTheme(moraga)

export default merge(typography, {
prism,
initialColorMode: 'light',
colors: {
...modes.light,
Expand Down Expand Up @@ -98,8 +100,10 @@ export default merge(typography, {
},
pre: {
my: 4,
fontFamily: 'monospace',
p: 3,
variant: 'prism',
textAlign: 'left',
fontFamily: 'monospace',
borderRadius: 'radius',
'.token-line': {
lineHeight: '1.5em',
Expand Down
8 changes: 8 additions & 0 deletions core/gatsby-theme-docz/src/theme/modes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as colors from './colors'
import prismDark from './prism/dark'
import prismLight from './prism/light'

export const light = {
...colors,
Expand Down Expand Up @@ -42,6 +44,9 @@ export const light = {
border: colors.grayLight,
color: colors.gray,
},
prism: {
...prismLight,
},
}

export const dark = {
Expand Down Expand Up @@ -86,4 +91,7 @@ export const dark = {
border: colors.gray,
color: colors.gray,
},
prism: {
...prismDark,
},
}
Loading

0 comments on commit d265444

Please sign in to comment.