Skip to content

Commit 431097d

Browse files
committed
show results for the version you are browsing
1 parent 3b3f8cb commit 431097d

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/components/Search.res

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

62+
@val @scope(("window", "location")) external pathname: string = "pathname"
6263
let transformItems = (items: DocSearch.transformItems) => {
64+
let version = switch pathname {
65+
| "/" => Url.Latest
66+
| other => Url.parse(other).version
67+
}
68+
6369
items->Belt.Array.keepMap(item => {
6470
// Transform absolute URL into relative
6571
let url = try Util.Url.make(item.url)->Some catch {
@@ -69,12 +75,25 @@ let transformItems = (items: DocSearch.transformItems) => {
6975
}
7076
switch url {
7177
| 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)
78+
let versionStr = switch version {
79+
| Latest | NoVersion => "latest"
80+
| Version(v) => v
81+
}
82+
let urlVersion = switch Url.parse(pathname).version {
83+
| Latest | NoVersion => "latest"
84+
| Version(v) => v
85+
}
86+
87+
if urlVersion == versionStr {
88+
let (lvl1, type_) = switch item.hierarchy.lvl1->Js.Nullable.toOption {
89+
| Some(_) => (item.hierarchy.lvl1, item.type_)
90+
| None => (item.hierarchy.lvl0->Js.Nullable.return, #lvl1)
91+
}
92+
let hierarchy = {...item.hierarchy, lvl1}
93+
{...item, url: pathname ++ hash, hierarchy, type_}->Some
94+
} else {
95+
None
7596
}
76-
let hierarchy = {...item.hierarchy, lvl1}
77-
{...item, url: pathname ++ hash, hierarchy, type_}->Some
7897
| None => None
7998
}
8099
})

0 commit comments

Comments
 (0)