1+ import * as React from "react"
12import { clsx } from "clsx"
23import { Code } from "nextra/components"
34import {
@@ -18,7 +19,13 @@ import json from "./data-colocation.json"
1819import Query from "./data-colocation.mdx"
1920import "./data-colocation.css"
2021import { 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+ }
2229
2330const components = {
2431 pre : ( props : ComponentPropsWithoutRef < typeof Pre > ) => (
@@ -33,10 +40,14 @@ const components = {
3340 />
3441 ) ,
3542 code : ( { children, ...rest } : ComponentPropsWithoutRef < typeof Code > ) => {
43+ let sectorIndex : number | undefined
44+ let depth = 0
45+
3646 if ( children ) {
3747 children = React . Children . map ( children , child => {
3848 if ( isSpanElement ( child ) ) {
3949 let children = ( child as ReactElement ) . props . children
50+
4051 if ( isSpanElement ( children ) ) {
4152 children = children . props . children
4253 } else if ( Array . isArray ( children ) ) {
@@ -48,17 +59,31 @@ const components = {
4859 return child
4960 } )
5061 . join ( "" )
62+ }
5163
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 ++
5768 }
5869 }
5970
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+
6084 return child
6185 }
86+
6287 return child
6388 } )
6489 }
0 commit comments