@@ -13,6 +13,7 @@ import {
13
13
BrightProps ,
14
14
Extension ,
15
15
CodeText ,
16
+ MdCodeText ,
16
17
} from "./types"
17
18
import { linesToContent } from "./lines"
18
19
import { tokensToContent , tokensToTokenList } from "./tokens"
@@ -190,14 +191,43 @@ function parseChildren(
190
191
lang ?: LanguageAlias ,
191
192
code ?: string
192
193
) : 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
194
218
return {
195
219
code : trimTrailingNewline ( children . props ?. children ) ,
196
- ...getLanguageAndTitle ( children . props ?. className ) ,
220
+ ...getLanguageAndTitle ( ( children as MdCodeText ) . props ?. className ) ,
197
221
}
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
199
227
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
+
201
231
return {
202
232
code : trimTrailingNewline ( codeProps . children ) ,
203
233
...getLanguageAndTitle ( codeProps . className ) ,
@@ -206,16 +236,17 @@ function parseChildren(
206
236
return {
207
237
subProps,
208
238
}
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 ,
219
250
}
220
251
}
221
252
0 commit comments