Coder Social home page Coder Social logo

remark-sandpack's Introduction

Power MDX with Sandpack



Usage

1. Install

npm install remark-sandpack @codesandbox/sandpack-react

2. Register remark plugin

It may be different for each MDX plugin you use. Check out examples below.

3. Write your code

## 👍Sandpack is awesome.

import { Sandpack } from '@codesandbox/sandpack-react';


<Sandpack template="vanilla">
```js src/index.js
import "./styles.css";

document.getElementById("app").innerHTML = `
<h1>Hello Sandpack</h1>
`;

```

// import code from file, path should relative to process.cwd().
```css src/styles.css file=styles/globals.css
// those code will be ignored
h1{
  background: red;
}
```

```js readonly-file.js readOnly
// I'm  readonly
```
</Sandpack>

👍Sandpack is awesome.
👍Sandpack is awesome.
👍Sandpack is awesome.

Configuration

  1. Sandpack component

All props will pass to Sandpack directly, except files.

<Sandpack
  theme={theme}
  template="react"
  customSetup={{
    dependencies: {
      react: "17.0.2",
      "react-dom": "17.0.2",
      "react-scripts": "4.0.0",
    },
  }}
>
// markdown code blocks...
</Sandpack>
  1. Code Blocks

All code blocks contained within <Sandpack>/Sandpack> will be parsed and passed to <Sandpack>/Sandpack> as file props. 

That means you can define file property in code block meta.

<Sandpack>
```js src/index.js active readOnly
console.log('Hello Sandpack')
```

```js src/hidden.js hidden
console.log('I'm hidden')
```
</Sandpack>

Code above will transform into:

<Sandpack
  files={{
    "src/index.js": {
      code: `console.log('Hello Sandpack')`,
      active: true,
      readOnly: true,
    },
    "src/hidden.js": { 
      code: `console.log('I'm hidden')`, 
      hidden: true 
    },
  }}
/>;

Compatible

  • ✅ next.js with @next/mdx. 👉🏻example
  • ✅ next.js with next-remote-mdx. 👉🏻example
  • ✅ gatsby.js . 👉🏻example
  • ✅ docusaurus. 👉🏻example
  • ✅ astro. 👉🏻example

❗️for docusaurus, you need upgrade mdx to v2, please checkout docusaurus-mdx-2



Advance Useage

Custom Sandpack component

remark-sandpack will parse <Sandpack></Sandpack> jsx statements in your MDX files. If your custom sandpack component uses a different name, such as SandpackEnhanced:

// in your mdx config
remarkPlugins: [[remarkSandpack, { componentName: 'SandpackEnhanced' }]],
// in your MDX file

import SandpackEnhanced from 'your-component-path'

<SandpackEnhanced>
// code blocks
</SandpackEnhanced>

Make sure your custom sandpack component receive files prop.

remark-sandpack's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

remark-sandpack's Issues

No support for Astro/Astro's client directives?

Hey @thecuvii! Thank you for your work on this–it's very easy to use. The issue I'm running into involves Astro. I've got Sandpack rendering as expected from MDX content, but the component is never hydrated so it's not interactive at all. Passing astro's client directives causes the entire site to fail to render.

Have you had any experience getting this to work in Astro?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.