docs: ScottAgirs/Add inline code example#113
Open
ScottAgirs wants to merge 1 commit intoFormidableLabs:masterfrom
Open
docs: ScottAgirs/Add inline code example#113ScottAgirs wants to merge 1 commit intoFormidableLabs:masterfrom
ScottAgirs wants to merge 1 commit intoFormidableLabs:masterfrom
Conversation
Signed-off-by: ScottAgirs <scott@ijs.to>
|
Whoa really old PR but a really important thing since I had no idea how else to differentiate between inline code blocks and others. I am using MDXProvider from @mdx/react and I couldn't figure out a way to pass inline vs normal code blocks so I basically checked if there's a className. Here's my code. export const SyntaxHighlighter = ({ className, children }) => {
// remove 'language-' from className
const language = className?.replace('language-', '') || 'jsx'
if (className == 'pre') console.log({ children })
// workaround to prevent inline code from being wrapped in a pre tag
// Make to sure to always add the language class to the code tag if you don't want inline code
const isInline = !className
if (isInline) {
return (
<Highlight Prism={defaultProps.Prism} code={children} language="jsx">
{({ className, style, tokens, getLineProps, getTokenProps }) =>
tokens.map((line, i) => (
<code
// eslint-disable-next-line
{...getLineProps({ key: i, line })}
style={style}
className={className}>
{line.map((token, key) => (
<span {...getTokenProps({ key, token })} />
))}
</code>
))
}
</Highlight>
)
}
return (
<Highlight Prism={defaultProps.Prism} code={children} language={language}>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={`${className}`} style={style}>
{tokens.map((line, i) => (
// eslint-disable-next-line
<div {...getLineProps({ line, key: i })}>
{line.map((token, key) => (
// eslint-disable-next-line
<span {...getTokenProps({ token, key })} />
))}
</div>
))}
</pre>
)}
</Highlight>
)usage with @mdx-js/react import { SyntaxHighlighter } from '@/lib/SyntaxHighlighter'
// ssr: false to prevent rehydration errors on the client
const MDXProvider = dynamic(
() => import('@mdx-js/react').then((mod) => mod.MDXProvider),
{
ssr: false,
}
)
const components = {
code: SyntaxHighlighter,
// code: SyntaxHighlighter,
}
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<MDXProvider components={components}>
<Component {...pageProps} />
</MDXProvider>
)
}
// custom typefaces
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If merged, this will add a usage example for inline code snippets
Why?
I couldn't find documentation on how to use highlighting for inline code snippets. Took me a few minutes to hack it together, so I thought this may be useful and save these minutes for some in future.
Please pardon the interruption, if I have missed instructions for this.
Signed-off-by: ScottAgirs scott@ijs.to