An open API service indexing awesome lists of open source software.

https://github.com/steelydylan/markdown-it-safe-jsx


https://github.com/steelydylan/markdown-it-safe-jsx

Last synced: 8 months ago
JSON representation

Awesome Lists containing this project

README

          

## markdown-it-safe-jsx

```jsx
import MarkdownIt from 'markdown-it'
import { safeJsx } from 'markdown-it-safe-jsx'

import { TestComponent } from './TestComponent'

const md = new MarkdownIt()
md.use(safeJsx, {
components: {
TestComponent: (props) =>
}
})
md.render(``)
```

### hydrate component

Sometimes You need to hydrate component, for example, if you want to use `useState` hook in your component.

```jsx

import MarkdownIt from 'markdown-it'
import { safeJsx, useHydrateJsx } from 'markdown-it-safe-jsx'

import { TestComponent } from './TestComponent'

function Markdown({ text }: { text: string }) {
const ref = useRef(null);

// hydrate component
useHydrateJsx({
components: {
Test
},
ref,
}, []);

const result = useMemo(() => {
const md = markdownIt({
breaks: true,
linkify: true,
html: true,
});
md.use(safeJsx, {
Test: (props: TestProps) => {
return ;
}
})
return md.render(text);
}, [text]);

return (


)
}
```

### dangerously support function props

If you want to use function props, you can use `unsafeRenderFunction` and `unsafeHydrateFunction` options.

```jsx
import MarkdownIt from 'markdown-it'
import { safeJsx, useHydrateJsx } from 'markdown-it-safe-jsx'

import { TestComponent } from './TestComponent'

function Markdown({ text }: { text: string }) {
const ref = useRef(null);

// hydrate component
useHydrateJsx({
components: {
Test
},
ref,
unsafeHydrateFunction: true,
}, []);

const result = useMemo(() => {
const md = markdownIt({
breaks: true,
linkify: true,
html: true,
});
md.use(safeJsx, {
Test: (props: TestProps) => {
return ;
}
}, {
unsafeRenderFunction: true,
})
return md.render(text);
}, [text]);

return (


)
}
```