question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is this possible passing function to mdx file ?

See original GitHub issue

Hi, Doing a page with ChakraUI, I’ve struggling for the whole day trying to passing function to mdx file.

On the mdx file, I was trying to achieve something like

<Image
    src="xxx"
    borderColor={useColorModeValue('gray.300', 'gray.700')}
    borderWidth="1px"
    shadow="md"
    mb={8}
  />

The Image component here is the https://chakra-ui.com/docs/media-and-icons/image chakraUI component.

And here is how I passed the useColorModeValue function down(in tsx page):

import { useColorModeValue } from '@chakra-ui/react';

///...
const pageScecificComponent = {
  ...mdxComponents,
  useColorModeValue // I'm struggling here
};

export const getStaticProps: GetStaticProps = async () => {
  const postFilePath = path.join(POSTS_PATH, 'About.mdx');
  const source = fs.readFileSync(postFilePath);

  const { content, data } = matter(source);

  const mdxSource = await serialize(content, {
    // Optionally pass remark/rehype plugins
    mdxOptions: {
      remarkPlugins: [],
      rehypePlugins: [],
    },
    scope: data,
  });

  return {
    props: {
      source: mdxSource,
      frontMatter: data,
    },
  };
};

const Home: AppNextPage<InferGetStaticPropsType<typeof getStaticProps>> = ({ source, frontMatter }) => {
  return <MDXRemote {...source} components={pageScecificComponent} />;
};

Everything was working fine if the useColorModeValue function on mdx file is not present. So, is there anyway I can do this, googling things through various example but everything I found was it implemented using next-mdx-enhanced (which I want to avoid).

I would appreciate even just some glue.

Thank you guys.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
khanhnacommented, Oct 13, 2021

I think you merged the scope property directly into the source object here on this line, rather than under the scope key:

const processedSource = { ...addingPropScope, ...theRest };

it would have to be instead:

const processedSource = { scope: addingPropScope, ...theRest };

I did exactly as you re-correct the code… and magic happened 👍

Now this syntax worked:

// Mdx File
<Note status="info" icon={FiActivity}>
  <p>
    Checking here {InvokeSample()}
  </p>
</Note>

Many thanks @jescalan, you made my day 🎉

2reactions
khanhnacommented, Oct 2, 2021

Thanks for a quick reply @payapula,

I was though that was possible, but seems like I would take this approach as long as I could find anything that more helpful.

And ofcourse this should work right away. Have a good day 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting started - MDX
This article explains how to integrate MDX into your project. It shows how to use MDX with your bundler and JSX runtime of...
Read more >
How to /Can I export function to MDXProvider? #1120 - GitHub
I want to create and export some global utility function to MDX Context. ... could use rather than import it every time in...
Read more >
Advanced Features: Using MDX - Next.js
MDX is a superset of markdown that lets you write JSX directly in your markdown files. It is a powerful way to add...
Read more >
Guide to Writing MDX and Markdown | Chicago Docs
JSX attributes are passed to the React component as props. In an MDX file, a block of Markdown text between JSX tags is...
Read more >
MDX - Storybook
MDX is a standard file format that combines Markdown with JSX. It means you can use Markdown's terse syntax (such as # heading)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found