@@ -25,6 +25,61 @@ external keyboardEventPreventDefault: keyboardEventLike => unit = "preventDefaul
25
25
26
26
type state = Active | Inactive
27
27
28
+ let hit = ({hit , children }: DocSearch .hitComponent ) => {
29
+ let toTitle = str =>
30
+ str -> Js .String2 .charAt (0 )-> Js .String2 .toUpperCase ++ Js .String2 .sliceToEnd (str , ~from = 1 )
31
+
32
+ let description = switch hit .url
33
+ -> Js .String2 .split ("/" )
34
+ -> Js .Array2 .sliceFrom (1 )
35
+ -> Belt .List .fromArray {
36
+ | list {"blog" , ... _ } => "Blog"
37
+ | list {"docs" , doc , version , ... rest } =>
38
+ let path = rest -> Belt .List .toArray
39
+
40
+ let info =
41
+ path
42
+ -> Js .Array2 .slice (~start = 0 , ~end_ = Js .Array2 .length (path ) - 1 )
43
+ -> Js .Array2 .map (path =>
44
+ switch path {
45
+ | "api" => "API"
46
+ | other => toTitle (other )
47
+ }
48
+ )
49
+
50
+ [doc -> toTitle , version -> toTitle ]-> Js .Array2 .concat (info )-> Js .Array2 .joinWith (" / " )
51
+ | _ => ""
52
+ }
53
+
54
+ <Next .Link href = {hit .url } className = "flex flex-col w-full" >
55
+ <span className = "text-gray-60 captions px-4 pt-3 pb-1 block" >
56
+ {description -> React .string }
57
+ </span >
58
+ children
59
+ </Next .Link >
60
+ }
61
+
62
+ let transformItems = (items : DocSearch .transformItems ) => {
63
+ items -> Belt .Array .keepMap (item => {
64
+ // Transform absolute URL into relative
65
+ let url = try Util .Url .make (item .url )-> Some catch {
66
+ | Js .Exn .Error (obj ) =>
67
+ Js .Console .error2 (` Failed to parse URL ${item.url}` , obj )
68
+ None
69
+ }
70
+ switch url {
71
+ | Some ({pathname , hash }) =>
72
+ let (lvl1 , type_ ) = switch item .hierarchy .lvl1 -> Js .Nullable .toOption {
73
+ | Some (_ ) => (item .hierarchy .lvl1 , item .type_ )
74
+ | None => (item .hierarchy .lvl0 -> Js .Nullable .return , #lvl1 )
75
+ }
76
+ let hierarchy = {... item .hierarchy , lvl1 }
77
+ {... item , url : pathname ++ hash , hierarchy , type_ }-> Some
78
+ | None => None
79
+ }
80
+ })
81
+ }
82
+
28
83
@react.component
29
84
let make = () => {
30
85
let (state , setState ) = React .useState (_ => Inactive )
@@ -78,62 +133,8 @@ let make = () => {
78
133
indexName
79
134
onClose
80
135
initialScrollY = {window -> scrollY }
81
- transformItems = {items => {
82
- // Js.log(items)
83
- // Transform absolute URL intro relative url
84
- items -> Js .Array2 .map (item => {
85
- let url = try Util .Url .make (item .url ).pathname catch {
86
- | Js .Exn .Error (obj ) =>
87
- switch Js .Exn .message (obj ) {
88
- | Some (m ) =>
89
- Js .Console .error ("Failed to constructor URL " ++ m )
90
- item .url
91
- | None => item .url
92
- }
93
- }
94
-
95
- let (content , type_ ) = switch item .content -> Js .Nullable .toOption {
96
- | Some (c ) => (c -> Js .Nullable .return , item .type_ )
97
- | None =>
98
- let fallback = item .hierarchy .lvl0
99
- (fallback -> Js .Nullable .return , #content )
100
- }
101
-
102
- {... item , url , content , type_ }
103
- })
104
- }}
105
- hitComponent = {({hit , children }) => {
106
- let description = switch hit .url
107
- -> Js .String2 .split ("/" )
108
- -> Js .Array2 .sliceFrom (1 )
109
- -> Belt .List .fromArray {
110
- | list {"blog" , ... _ } => "BLOG"
111
- | list {"docs" , "manual" , version , ... rest } =>
112
- let path = rest -> Belt .List .toArray
113
-
114
- let info =
115
- path
116
- -> Js .Array2 .slice (~start = 0 , ~end_ = Js .Array2 .length (path ) - 1 )
117
- -> Js .Array2 .map (Js .String2 .toUpperCase )
118
-
119
- let version = if version == "latest" {
120
- "Latest"
121
- } else {
122
- version
123
- }
124
-
125
- [version ]-> Js .Array2 .concat (info )-> Js .Array2 .joinWith (" / " )
126
- | _ => ""
127
- }
128
- // <div className="flex flex-col w-full">
129
- <a href = {hit .url } className = "flex flex-col w-full" >
130
- <span className = "text-gray-60 captions px-4 py-2 block" >
131
- {description -> React .string }
132
- </span >
133
- children
134
- </a >
135
- // </div>
136
- }}
136
+ transformItems = {transformItems }
137
+ hitComponent = hit
137
138
/>,
138
139
element ,
139
140
)
0 commit comments