Skip to content

Make CodeSurfer compatible with mdx-deck 2.x #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fixtures/sample/sample.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CodeSurfer } from "mdx-deck-code-surfer"
import { dark } from "mdx-deck/themes"
import { dark } from "@mdx-deck/themes"
import nightOwl from "prism-react-renderer/themes/nightOwl"

export { components } from "mdx-deck-code-surfer"
Expand Down Expand Up @@ -124,7 +124,7 @@ And you can still add regular notes the ol' fashioned way!
---

```jsx Global Theming
import { dark } from "mdx-deck/themes"
import { dark } from "@mdx-deck/themes"
import nightOwl from "prism-react-renderer/themes/nightOwl"

export const theme = {
Expand Down
20 changes: 9 additions & 11 deletions packages/mdx-deck-code-surfer/src/deck-code-surfer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CodeSurfer from "code-surfer";
import React from "react";
import { withDeck, updaters } from "mdx-deck";
import { withTheme } from "styled-components";
import { withContext } from "@mdx-deck/components";
import { withTheme } from "emotion-theming";
import memoizeOne from "memoize-one";

const Notes = ({ notes }) =>
Expand All @@ -17,14 +17,10 @@ const Title = ({ title }) =>
class InnerCodeSurfer extends React.Component {
constructor(props) {
super(props);
const { update, index } = props.deck;
const { register, index } = props.context;
if (typeof register !== "function") return;
const parsedSteps = this.parseSteps(props.steps);
const maxStep = parsedSteps.length - 1;
update(updaters.setSteps(index, maxStep));
}

shouldComponentUpdate(nextProps) {
return !!nextProps.deck.active;
register(index, { steps: parsedSteps.length - 1 });
}

parseSteps = memoizeOne((steps, notes) => {
Expand All @@ -50,6 +46,7 @@ class InnerCodeSurfer extends React.Component {

render() {
let {
context,
code,
steps,
title,
Expand All @@ -60,7 +57,8 @@ class InnerCodeSurfer extends React.Component {
...rest
} = this.props;

const stepIndex = this.props.deck.step || 0;
const { step } = context;
const stepIndex = step || 0;
const mdxDeckTheme = theme;
prismTheme = prismTheme || mdxDeckTheme.codeSurfer;
showNumbers = showNumbers || (prismTheme && prismTheme.showNumbers);
Expand Down Expand Up @@ -121,7 +119,7 @@ class InnerCodeSurfer extends React.Component {
}

// Things I need to do to avoid props name collisions
const EnhancedCodeSurfer = withDeck(withTheme(InnerCodeSurfer));
const EnhancedCodeSurfer = withContext(withTheme(InnerCodeSurfer));
export default ({ theme, ...rest }) => (
<EnhancedCodeSurfer {...rest} prismTheme={theme} />
);
2 changes: 1 addition & 1 deletion packages/mdx-deck-code-surfer/src/deck-components.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { Notes } from "mdx-deck";
import { Notes } from "@mdx-deck/components";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mdx-deck package actually exports all of the components in @mdx-deck/components. See here.

import DeckCodeSurfer from "./deck-code-surfer";

class Code extends React.PureComponent {
Expand Down