Skip to content

Commit 757aec8

Browse files
authored
Merge pull request #40 from joshwcomeau/main
Fix issue with MDX inline code
2 parents bb61dad + bbe97c3 commit 757aec8

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

.changeset/cool-cycles-admire.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"bright": patch
3+
---
4+
5+
Fix usage with overriden code element

lib/src/index.tsx

+45-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
BrightProps,
1414
Extension,
1515
CodeText,
16+
MdCodeText,
1617
} from "./types"
1718
import { linesToContent } from "./lines"
1819
import { tokensToContent, tokensToTokenList } from "./tokens"
@@ -190,14 +191,43 @@ function parseChildren(
190191
lang?: LanguageAlias,
191192
code?: string
192193
): Partial<BrightProps> {
193-
if (typeof children === "object" && children?.type === "code") {
194+
// the Code component can be used in many ways
195+
// this function use some heuristics to guess the correct usage
196+
197+
if (typeof children === "string" || code) {
198+
// Basic usage
199+
// either: <Code lang="js">console.log(1)</Code>
200+
// or: <Code lang="js" code="console.log(1)" />
201+
let newLang = lang || "text"
202+
if (!LANG_NAMES.includes(newLang)) {
203+
console.warn(`Bright warning: Unknown language ${JSON.stringify(lang)}`)
204+
newLang = "text"
205+
}
206+
return {
207+
code: (children as string) || code || "",
208+
lang: newLang,
209+
}
210+
}
211+
212+
if (
213+
typeof children === "object" &&
214+
typeof children?.props?.children === "string"
215+
) {
216+
// Basic MDX usage, children usually is <code className="language-js">console.log(1)</code>
217+
// 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
194218
return {
195219
code: trimTrailingNewline(children.props?.children),
196-
...getLanguageAndTitle(children.props?.className),
220+
...getLanguageAndTitle((children as MdCodeText).props?.className),
197221
}
198-
} else if (typeof children === "object") {
222+
}
223+
224+
if (typeof children === "object") {
225+
// MDX usage with multiple code blocks (for example: https://bright.codehike.org/recipes/tabs)
226+
// children is an array of <Code> components
199227
const subProps = React.Children.toArray(children as any).map((c: any) => {
200-
const codeProps = c.props?.children?.props
228+
const codeElement = c.props?.children
229+
const codeProps = codeElement?.props
230+
201231
return {
202232
code: trimTrailingNewline(codeProps.children),
203233
...getLanguageAndTitle(codeProps.className),
@@ -206,16 +236,17 @@ function parseChildren(
206236
return {
207237
subProps,
208238
}
209-
} else {
210-
let newLang = lang || "text"
211-
if (!LANG_NAMES.includes(newLang)) {
212-
console.warn(`Bright warning: Unknown language ${JSON.stringify(lang)}`)
213-
newLang = "text"
214-
}
215-
return {
216-
code: (children as string) || code || "",
217-
lang: newLang,
218-
}
239+
}
240+
241+
// unknown usage
242+
let newLang = lang || "text"
243+
if (!LANG_NAMES.includes(newLang)) {
244+
console.warn(`Bright warning: Unknown language ${JSON.stringify(lang)}`)
245+
newLang = "text"
246+
}
247+
return {
248+
code: (children as string) || code || "",
249+
lang: newLang,
219250
}
220251
}
221252

lib/src/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ export type DoubleTheme = {
5353
dark: Theme
5454
light: Theme
5555
}
56-
type MdCodeText = {
56+
export type MdCodeText = {
5757
type: "code"
5858
props: { className?: string; children: string }
5959
}
6060

61-
type MdMultiCodeText = {
61+
export type MdMultiCodeText = {
6262
type: Function
6363
props: {
6464
children: MdCodeText[]

0 commit comments

Comments
 (0)