|
2 | 2 |
|
3 | 3 | ## What? |
4 | 4 |
|
5 | | -Use Solid components with [mdx](https://mdxjs.com/). |
| 5 | +Use Solid components with [mdx](https://mdxjs.com/) or [xdm](http://wooorm.com/xdm/). |
| 6 | + |
| 7 | +This module is ESM only, due to [mdx](https://mdxjs.com/) or [xdm](http://wooorm.com/xdm/) being ESM only. |
| 8 | + |
| 9 | +Adding |
| 10 | + |
| 11 | +```json |
| 12 | +"type": "module" |
| 13 | +``` |
| 14 | + |
| 15 | +in package.json is one option. |
6 | 16 |
|
7 | 17 | ## Installation |
8 | 18 |
|
9 | 19 | ```sh |
10 | 20 | pnpm install --save-dev solid-jsx |
11 | 21 | ``` |
| 22 | + |
12 | 23 | ## Usage |
13 | 24 |
|
14 | | -> This library is meant to be used alongside [@mdx-js](https://mdxjs.com/), version 2, by setting the jsxImportSource to __'solid-jsx'__. |
15 | | -You can use official integration with various bundlers, and frameworks, below is a configuration sample |
| 25 | +This library can be used alongside version 2 of [@mdx-js](https://mdxjs.com/), or [xdm](http://wooorm.com/xdm/) by setting the jsxImportSource property to **'solid-jsx'**. |
| 26 | + |
| 27 | +```js |
| 28 | +pnpm i -D @mdx-js/rollup@next |
| 29 | +``` |
| 30 | + |
| 31 | +or |
| 32 | + |
| 33 | +```js |
| 34 | +pnpm i -D xdm |
| 35 | +``` |
| 36 | + |
| 37 | +You can use their official integration with various bundlers, and frameworks, below is a configuration sample |
16 | 38 | for [Vite](https://vitejs.dev), which supports rollup plugins. |
17 | 39 |
|
18 | 40 | ```js |
19 | | -import { defineConfig } from "vite"; |
20 | | -import solid from "vite-plugin-solid"; |
| 41 | +import { defineConfig } from 'vite'; |
| 42 | +import solid from 'vite-plugin-solid'; |
21 | 43 | import mdx from '@mdx-js/rollup'; |
22 | 44 | import remarkGfm from 'remark-gfm'; |
23 | 45 |
|
24 | 46 | export default defineConfig({ |
25 | | - |
26 | | - plugins: [mdx({ jsxImportSource: 'solid-jsx', remarkPlugins: [remarkGfm]}), solid()], |
| 47 | + plugins: [mdx({ jsxImportSource: 'solid-jsx', remarkPlugins: [remarkGfm] }), solid()], |
27 | 48 | build: { |
28 | | - target: "esnext", |
| 49 | + target: 'esnext', |
29 | 50 | }, |
30 | 51 | }); |
31 | 52 | ``` |
32 | | -More information -> [Integrations](https://mdxjs.com/docs/getting-started/#integrations). |
33 | 53 |
|
34 | | -> All markdown tags and custom components replacement should be supported. |
| 54 | +Draw math with [xdm](http://wooorm.com/xdm/) and [mathjax](https://www.mathjax.org/) |
35 | 55 |
|
36 | 56 | ```js |
37 | | -const options: Record<string, Component> = { |
38 | | - red: RedThing, |
39 | | - green: GreenThing, |
40 | | - blue: BlueThing |
| 57 | +import solid from 'vite-plugin-solid'; |
| 58 | +import xdm from 'xdm/rollup.js'; |
| 59 | +import remarkMath from 'remark-math'; |
| 60 | +import uno from 'unocss/vite'; |
| 61 | +import remarkMath from 'remark-math'; |
| 62 | +import rehypeMathJaxSVG from 'rehype-mathjax/svg.js'; |
| 63 | + |
| 64 | +export default defineConfig({ |
| 65 | + plugins: [ |
| 66 | + xdm({ |
| 67 | + jsxImportSource: 'solid-jsx', |
| 68 | + remarkPlugins: [remarkMath], |
| 69 | + rehypePlugins: [rehypeMathJaxSVG], |
| 70 | + }), |
| 71 | + solid(), |
| 72 | + uno(), |
| 73 | + ], |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +More information -> [Integrations](https://mdxjs.com/docs/getting-started/#integrations). |
| 78 | + |
| 79 | +All markdown tags and custom components replacement are supported. |
| 80 | + |
| 81 | +However, since the code goes through the @mdx-js/xdm compiler and not through the solid-js compiler, |
| 82 | +inline components will not be reactive. |
| 83 | + |
| 84 | +```jsx |
| 85 | +export const Counter = () => { |
| 86 | + const [count, setCount] = createSignal(0); |
| 87 | + return ( |
| 88 | + <> |
| 89 | + <button onClick={() => setCount(count() + 1)}>Increment</button> |
| 90 | + <p>{count}</p> |
| 91 | + ) |
41 | 92 | } |
42 | | -... |
43 | | -<Message |
44 | | - components={{ |
45 | | - h2: 'h6', |
46 | | - h1: () => <div>Test</div>, |
47 | | - Planet: () => <Dynamic component={options[selected()]} />, |
48 | | -}}> |
| 93 | +<Counter /> |
49 | 94 | ``` |
50 | 95 |
|
51 | | -> However, since the code goes through the @mdx-js compiler and then in the jsx function in solid-jsx, |
52 | | -this will not be reactive: |
| 96 | +This limitation is minor, since writing components inline is just one option, |
| 97 | +and by far the worst one of them all, with limited syntax and language support. |
53 | 98 |
|
54 | | -```js |
55 | | -const [tag, setTag] = createSignal('p') |
56 | | -<Message |
57 | | - components={{ |
58 | | - h2: tag(), |
59 | | -}}> |
| 99 | +You can always import directly TypeScript/JavaScript components inside .mdx |
| 100 | + |
| 101 | +```mdx |
| 102 | +import Counter from './Counter'; |
| 103 | + |
| 104 | +<Counter /> |
60 | 105 | ``` |
61 | | -You can use Dynamic tag here: |
62 | 106 |
|
63 | | -```js |
64 | | -const [tag, setTag] = createSignal('p') |
65 | | -<Message |
| 107 | +or pass the component to markdown directly |
| 108 | + |
| 109 | +```mdx |
| 110 | +Hello <Counter> |
| 111 | +``` |
| 112 | + |
| 113 | +```jsx |
| 114 | +import Message from './message.mdx'; |
| 115 | + |
| 116 | +<Message components={{ Counter }} />; |
| 117 | +``` |
| 118 | + |
| 119 | +To have dynamic tags, use solid-js Dynamic component. |
| 120 | + |
| 121 | +```jsx |
| 122 | +const options: Record<string, Component> = { |
| 123 | + red: RedThing, |
| 124 | + green: GreenThing, |
| 125 | + blue: BlueThing, |
| 126 | +}; |
| 127 | +<Message |
66 | 128 | components={{ |
67 | | - h2: (props: PropsWithChildren) => <Dynamic component={tag()} {...props} />, |
68 | | -}}> |
| 129 | + h2: 'h6', |
| 130 | + h1: () => <div>Test</div>, |
| 131 | + Planet: () => <Dynamic component={options[selected()]} />, |
| 132 | + }} |
| 133 | +/>; |
69 | 134 | ``` |
70 | 135 |
|
71 | 136 | ## Support |
|
0 commit comments