Skip to content

Example using Remark plugins in react #31

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions examples/preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"build:vite-plugin-mdx": "cd ../../ && yarn build"
},
"dependencies": {
"vite-plugin-mdx": "file:../../",
"@mdx-js/mdx": "^1.6.22",
"@mdx-js/preact": "^1.6.22",
"preact": "^10.5.12",
"@prefresh/vite": "^2.1.0",
"vite": "^2.0.0-beta.65"
"@prefresh/vite": "^2.2.2",
"preact": "^10.5.13",
"remark-prism": "^1.3.6",
"vite": "^2.3.4",
"vite-plugin-mdx": "file:../../"
Copy link
Owner

Choose a reason for hiding this comment

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

The file:../../ breaks the README.md instructions of the example.

Otherwise looks good 👍

Copy link
Collaborator

Choose a reason for hiding this comment

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

@brillout what do you mean here?

}
}
53 changes: 53 additions & 0 deletions examples/preact/src/Hello.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Counter } from './Counter.jsx';

# Hello

This text is written in Markdown.

MDX allows Rich Preact components to be used directly in Markdown: <Counter/>

Edit `Counter.jsx` or `Hello.mdx` and save to experience HMR updates.

## Plugin Support

### Install `remark-prism`

```bash
yarn add remark-prism
```

### Include CSS

```javascript
import "prismjs/themes/prism-tomorrow.css"
```

### Declare Plugin in `vite.config.js`

```javascript
import reactRefresh from '@vitejs/plugin-react-refresh'
import mdx from 'vite-plugin-mdx'

const options = {
remarkPlugins: [
// plugin added!
require('remark-prism')
],
rehypePlugins: [],
}

export default {
plugins: [
reactRefresh(),
mdx(options),
]
}
```

### Import Markdown Files

- md or mdx extensions are acceptable

```javascript
import Hello from './Hello.md'
```
9 changes: 0 additions & 9 deletions examples/preact/src/Hello.mdx

This file was deleted.

3 changes: 2 additions & 1 deletion examples/preact/src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render } from 'preact'
import './index.css'
import Hello from './Hello.mdx'
import Hello from './Hello.md'
import "prismjs/themes/prism-tomorrow.css"

render(<Hello />, document.getElementById('preact-root'))
14 changes: 13 additions & 1 deletion examples/preact/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import prefresh from '@prefresh/vite'
import mdx from 'vite-plugin-mdx'

// `options` are passed to `@mdx-js/mdx`
const options = {
// See https://mdxjs.com/advanced/plugins
remarkPlugins: [
require('remark-prism')
],
rehypePlugins: [],
}

export default {
esbuild: {
jsxFactory: 'h',
jsxFragment: 'Fragment',
jsxInject: `import { h, Fragment } from 'preact'`
},
plugins: [prefresh(), mdx()]
plugins: [
prefresh(),
mdx(options)
]
}
Loading