Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempting to adjust 'code' in mdx-components breaks #37

Closed
jer-k opened this issue Jan 14, 2024 · 2 comments
Closed

Attempting to adjust 'code' in mdx-components breaks #37

jer-k opened this issue Jan 14, 2024 · 2 comments

Comments

@jer-k
Copy link

jer-k commented Jan 14, 2024

Following the instructions from https://bright.codehike.org/

import { Code } from "bright"

// You need this file to use MDX in server components
// Learn more from the Next.js docs

export function useMDXComponents(components) {
return { ...components, pre: Code }
}

I set this up and it is working great. I have .mdx files where I'll do

here is some text `some_file_name.txt` some text

and it renders the HTML as <p>here is some text<code>some_file_name.txt</code> some text</p>. I was trying to figure out how to change the styling of the <code> tags. I followed the Next Mdx guide and did something simple like

code: ({children}) => <div>{children}</div>,

This breaks when bright has been imported. When bright is no imported, it works as expected (albeit looking quite weird).

Some related issues seem to be

The error I see

Unhandled Runtime Error
Error: Cannot read properties of undefined (reading 'children')

Call Stack
children
node_modules/bright/dist/index.mjs (516:44)
Array.map
<anonymous>
map
node_modules/bright/dist/index.mjs (512:54)

I am using https://github.com/code-hike/bright/releases/tag/bright%400.8.4

@jer-k
Copy link
Author

jer-k commented Jan 17, 2024

This seems to be caused by the way that MDX renders. https://mdxjs.com/table-of-components/ we can see

image

that single backticks renders <code> as well as triple backtick doing a <pre> and a <code>. When I console log what is being the props are from here https://github.com/code-hike/bright/blob/main/lib/src/index.tsx#L200 I get

what is c.props {
  className: 'language-js',
  children: 'let hello = "hello brightness"\nconsole.log(hello, "my old friend")\n'
}

I added a change to make the code look like

    const subProps = React.Children.toArray(children as any).map((c: any) => {
      let codeProps = c.props?.children?.props
      if (!codeProps) {
        codeProps = c.props;
      }
      return {
        code: trimTrailingNewline(codeProps.children),
        ...getLanguageAndTitle(codeProps.className),
      }
    })
    return {
      subProps,
    }

In mdx-components I have

export function useMDXComponents(components: MDXComponents) {
  return { ...components, pre: Code, code: ({children}) => <code>{children}</code>  }
}

The demo page has

image

The result is
image

Not sure if this change would break other things?

joshwcomeau added a commit to joshwcomeau/bright that referenced this issue Mar 1, 2024
Implement the solution shared here: code-hike#37 (comment)
@pomber
Copy link
Contributor

pomber commented Mar 12, 2024

Thanks for reporting and researching. Should be fixed on v0.8.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants