1
1
mirror of https://github.com/jxnblk/mdx-deck.git synced 2024-11-26 11:55:33 +03:00

Add code highlighting

This commit is contained in:
busypeoples 2018-08-04 12:51:59 +02:00
parent 4f8adbaae9
commit b6ce48bba8
7 changed files with 63 additions and 4 deletions

View File

@ -1,6 +1,7 @@
export { future as theme } from '../src/themes' export { future as theme } from '../src/themes'
import { Image, Notes, Appear } from '../src' import { Image, Notes, Appear, Code } from '../src'
import Layout from './Layout' import Layout from './Layout'
export highlight from 'react-syntax-highlighter/styles/prism/solarizedlight'
# mdx-deck # mdx-deck
@ -25,6 +26,26 @@ import Box from 'superbox'
- To help make your point - To help make your point
--- ---
## Code Examples
```elixir
# Elixir example
x = 2; y = 3
x + y
```
```javascript
/* JavaScript example */
const foo = a => a * a;
function bar(a) {
return a * a;
}
```
---
### Appear ### Appear
<ul> <ul>

BIN
docs/state_4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

View File

@ -35,6 +35,7 @@
"puppeteer": "^1.6.1", "puppeteer": "^1.6.1",
"react": "^16.4.1", "react": "^16.4.1",
"react-dev-utils": "^5.0.1", "react-dev-utils": "^5.0.1",
"react-syntax-highlighter": "^8.0.1",
"remark-emoji": "^2.0.1", "remark-emoji": "^2.0.1",
"stringify-object": "^3.2.2", "stringify-object": "^3.2.2",
"styled-components": ">=3.0.0", "styled-components": ">=3.0.0",

35
src/Code.js Normal file
View File

@ -0,0 +1,35 @@
import React from 'react'
import PropTypes from 'prop-types'
import { withTheme } from 'styled-components'
import SyntaxHighlighter, { registerLanguage } from "react-syntax-highlighter/prism-light"
import defaultTheme from 'react-syntax-highlighter/styles/prism/atom-dark'
import javascript from 'react-syntax-highlighter/languages/prism/javascript'
import php from 'react-syntax-highlighter/languages/prism/php'
import elixir from 'react-syntax-highlighter/languages/prism/elixir'
registerLanguage('javascript', javascript);
// registerLanguage('php', php);
registerLanguage('elixir', elixir);
export default withTheme(class Code extends React.Component {
static propTypes = {
children: PropTypes.string.isRequired,
className: PropTypes.string
}
getLangauge = (lang) => {
return lang ? lang.replace('language-', '') : 'javascript'
}
render() {
const { className, children, theme } = this.props
const language = this.getLangauge(className)
const style = theme.highlight ? theme.highlight : defaultTheme
return (
<SyntaxHighlighter language={language} style={style}>
{children}
</SyntaxHighlighter>
)
}
})

View File

@ -8,6 +8,7 @@ import {
} from 'styled-system' } from 'styled-system'
import Notes from './Notes' import Notes from './Notes'
import Mono from './Mono' import Mono from './Mono'
import Code from './Code'
const css = key => props => props.theme[key] const css = key => props => props.theme[key]
@ -155,7 +156,7 @@ const code = props => {
</Notes> </Notes>
) )
default: default:
return <Pre {...props} /> return <Code {...props} />
} }
} }

View File

@ -4,14 +4,14 @@ import SlideDeck from './index'
const mod = require(DOC_FILENAME) const mod = require(DOC_FILENAME)
const slides = mod.default const slides = mod.default
const { theme, components, Provider } = mod const { theme, highlight, components, Provider } = mod
export default class App extends React.Component { export default class App extends React.Component {
render () { render () {
return ( return (
<SlideDeck <SlideDeck
slides={slides} slides={slides}
theme={theme} theme={{...theme, highlight}}
components={components} components={components}
Provider={Provider} Provider={Provider}
/> />

View File

@ -18,6 +18,7 @@ import defaultComponents from './components'
export { default as Image } from './Image' export { default as Image } from './Image'
export { default as Notes } from './Notes' export { default as Notes } from './Notes'
export { default as Appear } from './Appear' export { default as Appear } from './Appear'
export { default as Code } from './Code'
export { default as components } from './components' export { default as components } from './components'
// themes // themes