-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-theme-docz): improve code highlight
- Loading branch information
1 parent
125c5cf
commit d265444
Showing
15 changed files
with
276 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.