velite icon indicating copy to clipboard operation
velite copied to clipboard

how to render the mdx() in vue ?

Open Mikaleb opened this issue 1 year ago • 2 comments

The part taken from https://velite.js.org/guide/using-mdx is maybe not true about itself:

{
  "posts": [
    {
      "title": "Hello world",
      "code": "const{Fragment:n,jsx:e,jsxs:t}=arguments[0],o=2023;function _createMdxContent(r){const a={h1:\"h1\",p:\"p\",...r.components},{Chart:c}=a;return c||function(n,e){throw new Error(\"Expected \"+(e?\"component\":\"object\")+\" `\"+n+\"` to be defined: you likely forgot to import, pass, or provide it.\")}(\"Chart\",!0),t(n,{children:[e(a.h1,{children:\"Last year’s snowfall\"}),\"\\n\",t(a.p,{children:[\"In \",o,\", the snowfall was above average.\\nIt was followed by a warm spring which caused\\nflood conditions in many of the nearby rivers.\"]}),\"\\n\",e(c,{year:o,color:\"#fcb32c\"})]})}return{year:o,default:function(n={}){const{wrapper:t}=n.components||{};return t?e(t,{...n,children:e(_createMdxContent,{...n})}):_createMdxContent(n)}};"
    }
  ]
}

how does the code rendered can be of use within a vuejs app ? because your only rendering example use react :

import * as runtime from 'react/jsx-runtime'

const sharedComponents = {
  // Add your global components here
}

// parse the Velite generated MDX code into a React component function
const useMDXComponent = (code: string) => {
  const fn = new Function(code)
  return fn({ ...runtime }).default
}

interface MDXProps {
  code: string
  components?: Record<string, React.ComponentType>
}

// MDXContent component
export const MDXContent = ({ code, components }: MDXProps) => {
  const Component = useMDXComponent(code)
  return <Component components={{ ...sharedComponents, ...components }} />
}

The only example on mdx for vue says :

import {compile} from '@mdx-js/mdx'

const js = String(await compile('# hi', {jsxImportSource: 'vue', /* otherOptions… */}))

and yet it still produces error :

Could not parse expression with acorn
Caused by: Error: Unexpected content after expression

Mikaleb avatar Dec 04 '24 11:12 Mikaleb

It looks like you should try https://mdxjs.com/packages/vue/, but I haven't used it, you can try it first

zce avatar Jan 21 '25 10:01 zce

Will try, i went with nuxt content for now but not too happy about them hidding stuff around.

Mikaleb avatar Feb 16 '25 11:02 Mikaleb