1
+ import * as React from "react"
1
2
import { clsx } from "clsx"
2
3
import { Code } from "nextra/components"
3
4
import {
@@ -18,7 +19,13 @@ import json from "./data-colocation.json"
18
19
import Query from "./data-colocation.mdx"
19
20
import "./data-colocation.css"
20
21
import { useOnClickOutside } from "@/app/conf/_design-system/utils/useOnClickOutside"
21
- import React from "react"
22
+
23
+ const highlightedFragments = {
24
+ GetFriendList : 1 ,
25
+ FriendList : 2 ,
26
+ FriendListItem : 3 ,
27
+ FriendInfo : 4 ,
28
+ }
22
29
23
30
const components = {
24
31
pre : ( props : ComponentPropsWithoutRef < typeof Pre > ) => (
@@ -33,10 +40,14 @@ const components = {
33
40
/>
34
41
) ,
35
42
code : ( { children, ...rest } : ComponentPropsWithoutRef < typeof Code > ) => {
43
+ let sectorIndex : number | undefined
44
+ let depth = 0
45
+
36
46
if ( children ) {
37
47
children = React . Children . map ( children , child => {
38
48
if ( isSpanElement ( child ) ) {
39
49
let children = ( child as ReactElement ) . props . children
50
+
40
51
if ( isSpanElement ( children ) ) {
41
52
children = children . props . children
42
53
} else if ( Array . isArray ( children ) ) {
@@ -48,17 +59,31 @@ const components = {
48
59
return child
49
60
} )
50
61
. join ( "" )
62
+ }
51
63
52
- if ( children . includes ( "fragment FriendInfo" ) ) {
53
- return cloneElement ( child , {
54
- ...child . props ,
55
- "data-sector" : "4" ,
56
- } as React . HTMLAttributes < HTMLSpanElement > )
64
+ if ( / q u e r y | f r a g m e n t / . test ( children ) ) {
65
+ for ( const [ name , index ] of Object . entries ( highlightedFragments ) ) {
66
+ if ( children . includes ( ` ${ name } ` ) ) sectorIndex = index
67
+ depth ++
57
68
}
58
69
}
59
70
71
+ if ( children . includes ( "{" ) ) depth ++
72
+ if ( children . includes ( "}" ) ) {
73
+ depth --
74
+ if ( depth === 0 ) sectorIndex = undefined
75
+ }
76
+
77
+ if ( sectorIndex ) {
78
+ return cloneElement ( child , {
79
+ ...child . props ,
80
+ "data-sector" : sectorIndex ,
81
+ } as React . HTMLAttributes < HTMLSpanElement > )
82
+ }
83
+
60
84
return child
61
85
}
86
+
62
87
return child
63
88
} )
64
89
}
0 commit comments