1
1
mirror of https://github.com/jxnblk/mdx-deck.git synced 2024-11-29 13:58:02 +03:00

Clean up theme package

This commit is contained in:
Brent Jackson 2019-04-11 11:23:02 -04:00
parent 1afa9f9375
commit 6cb1daa698
15 changed files with 140 additions and 59 deletions

View File

@ -25,6 +25,11 @@
"coverageReporters": [ "coverageReporters": [
"lcov", "lcov",
"html" "html"
],
"testPathIgnorePatterns": [
"/node_modules/",
"/.cache/",
"/public/"
] ]
}, },
"husky": { "husky": {

View File

@ -58,6 +58,7 @@ export class MDXDeck extends React.Component {
} }
handleKeyDown = e => { handleKeyDown = e => {
const { basepath } = this.props
const { keyCode, metaKey, ctrlKey, altKey, shiftKey } = e const { keyCode, metaKey, ctrlKey, altKey, shiftKey } = e
const { activeElement } = document const { activeElement } = document
if (inputElements.includes(activeElement.tagName)) { if (inputElements.includes(activeElement.tagName)) {
@ -68,7 +69,7 @@ export class MDXDeck extends React.Component {
const { pathname } = globalHistory.location const { pathname } = globalHistory.location
if (keyCode === keys.p && shiftKey && altKey) { if (keyCode === keys.p && shiftKey && altKey) {
navigate('/print') navigate(basepath + '/print')
this.setState({ mode: 'print' }) this.setState({ mode: 'print' })
} }
if (pathname === '/print') return if (pathname === '/print') return
@ -108,7 +109,9 @@ export class MDXDeck extends React.Component {
const { basepath } = this.props const { basepath } = this.props
const { pathname } = globalHistory.location const { pathname } = globalHistory.location
const pagepath = pathname.replace(basepath, '') const pagepath = pathname.replace(basepath, '')
return Number(pagepath.split('/')[1] || 0) const n = Number(pagepath.split('/')[1])
const index = isNaN(n) ? -1 : n
return index
} }
getMeta = i => { getMeta = i => {
@ -225,7 +228,8 @@ export class MDXDeck extends React.Component {
const { basepath } = this.props const { basepath } = this.props
const { pathname } = globalHistory.location const { pathname } = globalHistory.location
const { slides } = this.state const { slides } = this.state
const mode = pathname === '/print' ? PRINT : this.state.mode const pagepath = pathname.replace(basepath, '')
const mode = pagepath === '/print' ? PRINT : this.state.mode
const index = this.getIndex() const index = this.getIndex()
const context = { const context = {
...this.state, ...this.state,
@ -256,16 +260,15 @@ export class MDXDeck extends React.Component {
<Wrapper {...this.props} {...this.state} modes={modes} index={index}> <Wrapper {...this.props} {...this.state} modes={modes} index={index}>
<Swipeable onSwipedRight={this.previous} onSwipedLeft={this.next}> <Swipeable onSwipedRight={this.previous} onSwipedLeft={this.next}>
<Router basepath={basepath}> <Router basepath={basepath}>
<FirstSlide path={basepath + '/'} /> <Slide path="/" index={0} {...context}>
<Slide path={'/'} index={0} {...context}> <FirstSlide path="/" />
<FirstSlide path={'/'} />
</Slide> </Slide>
{slides.map((Component, i) => ( {slides.map((Component, i) => (
<Slide key={i} path={i + '/*'} index={i} {...context}> <Slide key={i} path={i + '/*'} index={i} {...context}>
<Component path={i + '/*'} /> <Component path={i + '/*'} />
</Slide> </Slide>
))} ))}
<Print path={'/print'} {...this.props} /> <Print path="print" {...this.props} />
</Router> </Router>
</Swipeable> </Swipeable>
</Wrapper> </Wrapper>

View File

@ -4,6 +4,8 @@ import Zoom from './Zoom'
import Slide from './Slide' import Slide from './Slide'
import Pre from './Pre' import Pre from './Pre'
const query = '?mode=overview'
export const Overview = props => { export const Overview = props => {
const { index, slides, basepath } = props const { index, slides, basepath } = props
const activeThumb = React.createRef() const activeThumb = React.createRef()
@ -42,7 +44,7 @@ export const Overview = props => {
key={i} key={i}
role="link" role="link"
onClick={e => { onClick={e => {
navigate(basepath + '/' + i) navigate(basepath + '/' + i + query)
}} }}
style={{ style={{
display: 'block', display: 'block',

View File

@ -0,0 +1,42 @@
# @mdx-deck/gatsby-theme
**WIP** A Gatsby theme for adding MDX Decks to your Gatsby site
```sh
npm i @mdx-deck/gatsby-theme
```
_Note:_ This theme requires MDX v1, which is currently in beta, and will not work with previous versions of MDX
```js
// gatsby-config.js
module.exports = {
__experimentalThemes: ['@mdx-deck/gatsby-theme'],
}
```
Add MDX Decks to the `src/decks/` directory.
```mdx
# Hello!
---
## Beep
```
## Using Layouts
Slide layout components must be rendered inline, _not_ using the default export syntax.
```mdx
import Layout from './my-layout'
<Layout>
# Hello Layout
</Layout>
```
MIT License

View File

@ -23,7 +23,12 @@ module.exports = {
{ {
resolve: 'gatsby-plugin-compile-es6-packages', resolve: 'gatsby-plugin-compile-es6-packages',
options: { options: {
modules: [pkg.name], modules: [
pkg.name,
'@mdx-deck/components',
'@mdx-deck/themes',
'@mdx-deck/layouts',
],
}, },
}, },
], ],

View File

@ -9,28 +9,26 @@
"build": "gatsby build" "build": "gatsby build"
}, },
"peerDependencies": { "peerDependencies": {
"@mdx-js/mdx": "^1.0.0-rc.0",
"@mdx-js/react": "^1.0.0-rc.0",
"gatsby": "^2.3.17", "gatsby": "^2.3.17",
"gatsby-mdx": "^0.5.6",
"react": "^16.8.6", "react": "^16.8.6",
"react-dom": "^16.8.6" "react-dom": "^16.8.6"
}, },
"devDependencies": { "devDependencies": {
"@mdx-js/mdx": "^1.0.0-rc.0",
"@mdx-js/react": "^1.0.0-rc.0",
"gatsby": "^2.3.17", "gatsby": "^2.3.17",
"gatsby-mdx": "^0.5.6",
"react": "^16.8.6", "react": "^16.8.6",
"react-dom": "^16.8.6" "react-dom": "^16.8.6"
}, },
"dependencies": { "dependencies": {
"gatsby-mdx": "^0.5.6",
"@mdx-js/mdx": "^1.0.0-rc.0",
"@mdx-js/react": "^1.0.0-rc.0",
"@mdx-deck/components": "^2.0.8", "@mdx-deck/components": "^2.0.8",
"@mdx-deck/themes": "^2.0.2", "@mdx-deck/themes": "^2.0.2",
"debug": "^4.1.1", "debug": "^4.1.1",
"gatsby-plugin-compile-es6-packages": "^1.1.0", "gatsby-plugin-compile-es6-packages": "^1.1.0",
"gatsby-plugin-emotion": "^4.0.6", "gatsby-plugin-emotion": "^4.0.6",
"gatsby-source-filesystem": "^2.0.29", "gatsby-source-filesystem": "^2.0.29",
"lodash.get": "^4.4.2",
"mkdirp": "^0.5.1" "mkdirp": "^0.5.1"
} }
} }

View File

@ -1,5 +1,5 @@
# Hello # Hello There! :sunglasses:
--- ---

View File

@ -1,10 +0,0 @@
# Leia
---
Is the best cat
---
:kissing_cat:

View File

@ -1,5 +1,4 @@
import React from 'react' import React from 'react'
import { Global } from '@emotion/core'
import { MDXProvider } from '@mdx-js/react' import { MDXProvider } from '@mdx-js/react'
import { MDXDeck } from '@mdx-deck/components' import { MDXDeck } from '@mdx-deck/components'
import Root from './root' import Root from './root'

View File

@ -19,37 +19,48 @@ const wrapper = props => {
) )
} }
const Thumb = props => { const Thumb = props => (
console.log(props) <div
role="link"
return <MDXProvider components={{ wrapper }}>{props.children}</MDXProvider> css={{
} cursor: 'pointer',
overflow: 'hidden',
}}
onClick={e => {
navigate(props.slug)
}}
>
<MDXProvider components={{ wrapper }}>{props.children}</MDXProvider>
<div
css={{
textAlign: 'center',
padding: 4,
}}
>
{props.title}
</div>
</div>
)
export default props => ( export default props => (
<Root> <Root>
<div css={{}}> <div
css={{
padding: 32,
}}
>
<h1>MDX Decks</h1> <h1>MDX Decks</h1>
<ul <ul
css={{ css={{
listStyle: 'none', listStyle: 'none',
padding: 0,
display: 'flex', display: 'flex',
flexWrap: 'wrap', flexWrap: 'wrap',
}} }}
> >
{props.decks.map(deck => ( {props.decks.map(deck => (
<li key={deck.id}> <li key={deck.id} style={{ width: 100 / 3 + '%' }}>
<div
role="link"
css={{
cursor: 'pointer',
}}
onClick={e => {
navigate(deck.slug)
}}
>
<Thumb {...props} {...deck} /> <Thumb {...props} {...deck} />
{deck.slug}
</div>
</li> </li>
))} ))}
</ul> </ul>

View File

@ -1,18 +1,26 @@
import React from 'react' import React from 'react'
import { graphql } from 'gatsby' import { graphql } from 'gatsby'
import MDXRenderer from 'gatsby-mdx/mdx-renderer' import MDXRenderer from 'gatsby-mdx/mdx-renderer'
import get from 'lodash.get'
import Layout from '../layouts/index' import Layout from '../layouts/index'
const getTitle = node => {
const headings = node.headings.filter(h => h.depth === 1)
return get(headings, `0.value`, node.slug)
}
export default props => { export default props => {
const decks = props.data.allMdx.edges.map(({ node }) => ({ const decks = props.data.allMdx.edges
.map(({ node }) => ({
...node, ...node,
slug: node.fields.slug, slug: node.fields.slug,
title: getTitle(node),
children: <MDXRenderer children={node.code.body} />, children: <MDXRenderer children={node.code.body} />,
})) }))
.filter(node => node.parent.sourceInstanceName === 'decks')
return <Layout {...props} decks={decks} /> return <Layout {...props} decks={decks} />
} }
// todo: filter??
export const query = graphql` export const query = graphql`
{ {
allMdx { allMdx {
@ -25,6 +33,15 @@ export const query = graphql`
code { code {
body body
} }
headings {
depth
value
}
parent {
... on File {
sourceInstanceName
}
}
} }
} }
} }

View File

@ -5,7 +5,10 @@ const mdxPlugin = require('@mdx-deck/mdx-plugin')
module.exports = async function(src) { module.exports = async function(src) {
const callback = this.async() const callback = this.async()
const options = getOptions(this) || {} const options = getOptions(this) || {}
options.remarkPlugins = options.mdPlugins || [] options.remarkPlugins = [
...options.remarkPlugins,
...(options.mdPlugins || []),
]
options.remarkPlugins.push(mdxPlugin) options.remarkPlugins.push(mdxPlugin)
const result = mdx.sync(src, options) const result = mdx.sync(src, options)

View File

@ -60,6 +60,9 @@ const cli = meow(
type: 'boolean', type: 'boolean',
default: true, default: true,
}, },
basepath: {
type: 'string',
},
}, },
} }
) )

View File

@ -6,13 +6,11 @@ const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const MiniCSSExtractPlugin = require('mini-css-extract-plugin') const MiniCSSExtractPlugin = require('mini-css-extract-plugin')
const merge = require('webpack-merge') const merge = require('webpack-merge')
const chalk = require('chalk') const chalk = require('chalk')
const remark = {
emoji: require('remark-emoji'),
unwrapImages: require('remark-unwrap-images'),
}
const HTMLPlugin = require('@mdx-deck/webpack-html-plugin') const HTMLPlugin = require('@mdx-deck/webpack-html-plugin')
const babel = require('../babel.config') const babel = require('../babel.config')
const remarkPlugins = [require('remark-emoji'), require('remark-unwrap-images')]
const rules = [ const rules = [
{ {
test: /\.jsx?$/, test: /\.jsx?$/,
@ -38,7 +36,7 @@ const rules = [
{ {
loader: require.resolve('@mdx-deck/loader'), loader: require.resolve('@mdx-deck/loader'),
options: { options: {
remarkPlugins: [remark.emoji, remark.unwrapImages], remarkPlugins,
}, },
}, },
], ],

View File

@ -13,6 +13,11 @@ export default class App extends React.Component {
} }
} }
const { basepath } = OPTIONS
App.defaultProps = {
basepath,
}
if (typeof document !== 'undefined') { if (typeof document !== 'undefined') {
const div = document.getElementById('root') const div = document.getElementById('root')
render(<App />, div) render(<App />, div)