1
0
Fork 0
freesewing/sites/shared/components/wrappers/mdx.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-12-18 09:54:53 +01:00
/*
* This is used to wrap MDX as returned from the mdxLoader
* method (see shared/mdx/loader.js)
* It is NOT for wrapping plain markdown/mdx
*/
2022-07-12 20:47:39 +02:00
import { useState, useEffect } from 'react'
2021-12-18 09:54:53 +01:00
// See: https://mdxjs.com/guides/mdx-on-demand/
import { run } from '@mdx-js/mdx'
import * as runtime from 'react/jsx-runtime'
2021-12-18 09:54:53 +01:00
// Components that are available in all MDX
2021-12-19 19:08:54 +01:00
import customComponents from 'shared/components/mdx'
2021-12-25 13:43:41 +01:00
// Previous-Next navigation
import PrevNext from '../mdx/prev-next'
2022-05-30 16:40:51 +02:00
const Null = (props) => null
const MdxWrapper = ({ mdx, app, t, components = {} }) => {
2021-12-18 09:54:53 +01:00
const [mdxModule, setMdxModule] = useState()
useEffect(() => {
;(async () => {
setMdxModule(await run(mdx, runtime))
2021-12-18 09:54:53 +01:00
})()
}, [mdx])
/*
* We use some default components that are available
* everywhere in MDX, but we also accept passing in
* extra components via props
*/
const allComponents = {
...customComponents(app, t),
...components,
2021-12-18 09:54:53 +01:00
}
// React component for MDX content
2022-05-30 16:40:51 +02:00
const MdxContent = mdxModule ? mdxModule.default : Null
2021-12-18 09:54:53 +01:00
return app ? (
2022-12-26 17:10:14 +01:00
<div className="text-primary mdx max-w-prose text-base-content max-w-prose text-base">
{mdxModule && <MdxContent components={allComponents} />}
<PrevNext app={app} />
</div>
) : (
<MdxContent components={allComponents} />
)
2021-12-18 09:54:53 +01:00
}
export default MdxWrapper