Skip to content

Commit 3e0ac90

Browse files
committed
fix: tolerate search hits without snippet metadata
Treat DocSearch highlight and snippet payloads as optional crawler fields before reading nested content. This keeps the custom hit renderer from crashing when Algolia returns plain hits without _snippetResult.
1 parent d66c312 commit 3e0ac90

3 files changed

Lines changed: 40 additions & 4 deletions

File tree

__tests__/Search_.test.res

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,36 @@ test("getContentHtml prefers crawler snippet markup over plain content", async (
220220
)
221221
})
222222

223+
test(
224+
"getContentHtml falls back to plain content when crawler hit has no snippet result",
225+
async () => {
226+
let hit: DocSearch.docSearchHit = Obj.magic(
227+
Dict.fromArray([
228+
("objectID", "crawler-hit"),
229+
("content", "map(array, fn) returns a new array."),
230+
("url", "https://rescript-lang.org/docs/manual/api/stdlib/array/#value-map"),
231+
("type", "content"),
232+
(
233+
"hierarchy",
234+
Obj.magic(
235+
Dict.fromArray([
236+
("lvl0", Obj.magic("Array")),
237+
("lvl1", Obj.magic("map")),
238+
("lvl2", Obj.magic(Nullable.null)),
239+
("lvl3", Obj.magic(Nullable.null)),
240+
("lvl4", Obj.magic(Nullable.null)),
241+
("lvl5", Obj.magic(Nullable.null)),
242+
("lvl6", Obj.magic(Nullable.null)),
243+
]),
244+
),
245+
),
246+
]),
247+
)
248+
249+
expect(Search.getContentHtml(hit))->toEqual(Some("map(array, fn) returns a new array."))
250+
},
251+
)
252+
223253
// ---------------------------------------------------------------------------
224254
// isChildHit
225255
// ---------------------------------------------------------------------------

src/bindings/DocSearch.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ type docSearchHit = {
5050
// Additional field for deprecation information
5151
deprecated: option<string>,
5252
// NOTE: docsearch need these two fields to highlight results
53-
_highlightResult: highlightResult,
54-
_snippetResult: snippetResult,
53+
_highlightResult?: highlightResult,
54+
_snippetResult?: snippetResult,
5555
}
5656
type transformItems = array<docSearchHit>
5757

src/components/Search.res

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ let markTitlePrefix = (title: string, markedText: string): string => {
112112
}
113113

114114
let getSnippetContent = (hit: DocSearch.docSearchHit): option<string> =>
115-
hit._snippetResult.content->highlightedValue
115+
switch hit._snippetResult {
116+
| Some(snippetResult) => snippetResult.content->highlightedValue
117+
| None => None
118+
}
116119

117120
let getApiTitle = (hit: DocSearch.docSearchHit): option<string> => {
118121
if hit.url->String.includes("/docs/manual/api/") {
@@ -131,7 +134,10 @@ let getApiTitle = (hit: DocSearch.docSearchHit): option<string> => {
131134
}
132135

133136
let getHighlightedTitle = (hit: DocSearch.docSearchHit): string => {
134-
let highlightedHierarchy = hit._highlightResult.hierarchy->Nullable.toOption
137+
let highlightedHierarchy =
138+
hit._highlightResult->Option.flatMap(highlightResult =>
139+
highlightResult.hierarchy->Nullable.toOption
140+
)
135141
let highlightedTitleWithMarkup = highlightedHierarchy->Option.flatMap(hierarchy =>
136142
switch hit.type_ {
137143
| Lvl0 | Lvl1 => None

0 commit comments

Comments
 (0)