Skip to content

Commit f7c8227

Browse files
committed
fix: handle crawler search hits without url_without_anchor
Treat DocSearch crawler hit url_without_anchor as optional and derive it from the hit URL when absent so transformItems does not throw before rendering results.
1 parent ccc9d1f commit f7c8227

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

__tests__/Search_.test.res

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let makeHit = (~type_: DocSearch.contentType, ~url: string): DocSearch.docSearch
88
objectID: "test",
99
content: Nullable.null,
1010
url,
11-
url_without_anchor: url,
11+
url_without_anchor: Nullable.make(url),
1212
type_,
1313
anchor: Nullable.null,
1414
hierarchy: {
@@ -262,11 +262,31 @@ test("normalizeHitUrls rewrites absolute site URLs to relative paths", async ()
262262
expect(result[0]->Option.map(hit => hit.url))->toEqual(
263263
Some("/docs/manual/typescript-integration#gentype"),
264264
)
265-
expect(result[0]->Option.map(hit => hit.url_without_anchor))->toEqual(
265+
expect(result[0]->Option.flatMap(hit => hit.url_without_anchor->Nullable.toOption))->toEqual(
266266
Some("/docs/manual/typescript-integration#gentype"),
267267
)
268268
})
269269

270+
test("normalizeHitUrls tolerates crawler hits without url_without_anchor", async () => {
271+
let hit: DocSearch.docSearchHit = Obj.magic(
272+
Dict.fromArray([
273+
("objectID", "crawler-hit"),
274+
("content", "map(array, fn) returns a new array."),
275+
("url", "https://rescript-lang.org/docs/manual/api/stdlib/array/#value-map"),
276+
("type", "content"),
277+
]),
278+
)
279+
280+
let result = Search.normalizeHitUrls([hit], ~siteUrl="https://rescript-lang.org/")
281+
282+
expect(result[0]->Option.map(hit => hit.url))->toEqual(
283+
Some("/docs/manual/api/stdlib/array/#value-map"),
284+
)
285+
expect(result[0]->Option.flatMap(hit => hit.url_without_anchor->Nullable.toOption))->toEqual(
286+
Some("/docs/manual/api/stdlib/array/"),
287+
)
288+
})
289+
270290
test("renders disabled search copy when Algolia config is missing", async () => {
271291
await viewport(1440, 500)
272292

src/bindings/DocSearch.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type docSearchHit = {
2323
objectID: string,
2424
content: Nullable.t<string>,
2525
url: string,
26-
url_without_anchor: string,
26+
url_without_anchor: Nullable.t<string>,
2727
@as("type") type_: contentType,
2828
anchor: Nullable.t<string>,
2929
hierarchy: hierarchy,

src/components/Search.res

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ let toRelativeSiteUrl = (url: string, ~siteUrl: string): string => {
2222
let normalizeHitUrls = (items: array<DocSearch.docSearchHit>, ~siteUrl: string) =>
2323
items->Array.map(hit => {
2424
let url = toRelativeSiteUrl(hit.url, ~siteUrl)
25-
let url_without_anchor = toRelativeSiteUrl(hit.url_without_anchor, ~siteUrl)
25+
let urlWithoutAnchor =
26+
hit.url_without_anchor
27+
->Nullable.toOption
28+
->Option.getOr(hit.url->String.split("#")->Array.get(0)->Option.getOr(hit.url))
29+
let url_without_anchor = toRelativeSiteUrl(urlWithoutAnchor, ~siteUrl)->Nullable.make
2630
{...hit, url, url_without_anchor}
2731
})
2832

0 commit comments

Comments
 (0)