Skip to content

Commit

Permalink
Merge pull request #40 from joshwcomeau/main
Browse files Browse the repository at this point in the history
Fix issue with MDX inline code
  • Loading branch information
pomber authored Mar 12, 2024
2 parents bb61dad + bbe97c3 commit 757aec8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-cycles-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bright": patch
---

Fix usage with overriden code element
59 changes: 45 additions & 14 deletions lib/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BrightProps,
Extension,
CodeText,
MdCodeText,
} from "./types"
import { linesToContent } from "./lines"
import { tokensToContent, tokensToTokenList } from "./tokens"
Expand Down Expand Up @@ -190,14 +191,43 @@ function parseChildren(
lang?: LanguageAlias,
code?: string
): Partial<BrightProps> {
if (typeof children === "object" && children?.type === "code") {
// the Code component can be used in many ways
// this function use some heuristics to guess the correct usage

if (typeof children === "string" || code) {
// Basic usage
// either: <Code lang="js">console.log(1)</Code>
// or: <Code lang="js" code="console.log(1)" />
let newLang = lang || "text"
if (!LANG_NAMES.includes(newLang)) {
console.warn(`Bright warning: Unknown language ${JSON.stringify(lang)}`)
newLang = "text"
}
return {
code: (children as string) || code || "",
lang: newLang,
}
}

if (
typeof children === "object" &&
typeof children?.props?.children === "string"
) {
// Basic MDX usage, children usually is <code className="language-js">console.log(1)</code>
// the code tag can be replaced by a custom component https://github.com/code-hike/bright/issues/37, so we can't check for the tag name
return {
code: trimTrailingNewline(children.props?.children),
...getLanguageAndTitle(children.props?.className),
...getLanguageAndTitle((children as MdCodeText).props?.className),
}
} else if (typeof children === "object") {
}

if (typeof children === "object") {
// MDX usage with multiple code blocks (for example: https://bright.codehike.org/recipes/tabs)
// children is an array of <Code> components
const subProps = React.Children.toArray(children as any).map((c: any) => {
const codeProps = c.props?.children?.props
const codeElement = c.props?.children
const codeProps = codeElement?.props

return {
code: trimTrailingNewline(codeProps.children),
...getLanguageAndTitle(codeProps.className),
Expand All @@ -206,16 +236,17 @@ function parseChildren(
return {
subProps,
}
} else {
let newLang = lang || "text"
if (!LANG_NAMES.includes(newLang)) {
console.warn(`Bright warning: Unknown language ${JSON.stringify(lang)}`)
newLang = "text"
}
return {
code: (children as string) || code || "",
lang: newLang,
}
}

// unknown usage
let newLang = lang || "text"
if (!LANG_NAMES.includes(newLang)) {
console.warn(`Bright warning: Unknown language ${JSON.stringify(lang)}`)
newLang = "text"
}
return {
code: (children as string) || code || "",
lang: newLang,
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export type DoubleTheme = {
dark: Theme
light: Theme
}
type MdCodeText = {
export type MdCodeText = {
type: "code"
props: { className?: string; children: string }
}

type MdMultiCodeText = {
export type MdMultiCodeText = {
type: Function
props: {
children: MdCodeText[]
Expand Down

0 comments on commit 757aec8

Please sign in to comment.