Skip to content

Commit 6f43723

Browse files
committed
add search parameters
1 parent 39bb72c commit 6f43723

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

src/bindings/DocSearch.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type docSearchHit = {
2727
@as("type") type_: contentType,
2828
anchor: Js.Nullable.t<string>,
2929
hierarchy: hierarchy,
30+
// NOTE: docsearch need these two fields to highlight results
3031
_highlightResult: {.},
3132
_snippetResult: {.},
3233
}

src/components/Search.res

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,15 @@ let hit = ({hit, children}: DocSearch.hitComponent) => {
5959
</Next.Link>
6060
}
6161

62-
@val @scope(("window", "location")) external pathname: string = "pathname"
6362
let transformItems = (items: DocSearch.transformItems) => {
64-
let version = switch pathname {
65-
| "/" => Url.Latest
66-
| other => Url.parse(other).version
67-
}
6863
items->Belt.Array.keepMap(item => {
69-
// Transform absolute URL into relative
7064
let url = try Webapi.URL.make(item.url)->Some catch {
7165
| Js.Exn.Error(obj) =>
7266
Js.Console.error2(`Failed to parse URL ${item.url}`, obj)
7367
None
7468
}
7569
switch url {
76-
| Some({pathname, hash}) =>
77-
let versionStr = switch version {
78-
| Latest | NoVersion => "latest"
79-
| Version(v) => v
80-
}
81-
let urlVersion = switch Url.parse(pathname).version {
82-
| Latest | NoVersion => "latest"
83-
| Version(v) => v
84-
}
85-
if urlVersion === versionStr {
86-
{...item, url: pathname ++ hash}->Some
87-
} else {
88-
None
89-
}
70+
| Some({pathname, hash}) => {...item, url: pathname ++ hash}->Some
9071
| None => None
9172
}
9273
})
@@ -95,6 +76,12 @@ let transformItems = (items: DocSearch.transformItems) => {
9576
@react.component
9677
let make = () => {
9778
let (state, setState) = React.useState(_ => Inactive)
79+
let router = Next.Router.useRouter()
80+
81+
let version = switch Url.parse(router.route).version {
82+
| Version(v) => v
83+
| _ => "latest"
84+
}
9885

9986
let handleCloseModal = () => {
10087
let () = switch ReactDOM.querySelector(".DocSearch-Modal") {
@@ -162,6 +149,7 @@ let make = () => {
162149
appId
163150
indexName
164151
onClose
152+
searchParameters={facetFilters: ["version:" ++ version]}
165153
initialScrollY={window->scrollY}
166154
transformItems={transformItems}
167155
hitComponent=hit

0 commit comments

Comments
 (0)