Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get URL for search result #1868

Merged
merged 10 commits into from
Jan 30, 2024
4 changes: 4 additions & 0 deletions assets/ts/ui/autocomplete/__autocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type ContentItem = {
content_title: string;
} & AlgoliaItem;

type SearchResultItem = {
_search_result_url: string;
} & ContentItem;

export type LocationItem = {
longitude: number;
latitude: number;
Expand Down
6 changes: 6 additions & 0 deletions assets/ts/ui/autocomplete/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
LocationItem,
PopularItem,
RouteItem,
SearchResultItem,
StopItem
} from "./__autocomplete";
import { isLGDown } from "../../helpers/media-breakpoints";
Expand All @@ -23,6 +24,11 @@ export function isContentItem(x: Item): x is ContentItem {
return Object.keys(x).includes("_content_type");
}

export function isSearchResultItem(x: Item): x is SearchResultItem {
// eslint-disable-next-line no-underscore-dangle
return isContentItem(x) && x._content_type === "search_result";
}

export const getTitleAttribute = (item: Item): string[] => {
if (isStopItem(item)) {
return ["stop", "name"];
Expand Down
9 changes: 8 additions & 1 deletion assets/ts/ui/autocomplete/templates/algolia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getTitleAttribute,
isContentItem,
isRouteItem,
isSearchResultItem,
isStopItem
} from "../helpers";
import {
Expand All @@ -22,7 +23,13 @@ interface LinkForItemProps {
}
export function LinkForItem(props: LinkForItemProps): React.ReactElement {
const { item, query, children } = props;
const url = isContentItem(item) ? item._content_url : item.url;

let url = isContentItem(item) ? item._content_url : item.url;

// Search result items are a subset of content items that point to a different URL
if (isSearchResultItem(item)) {
url = item._search_result_url.replace(/[\w]*:/g, "/");
}

// Special case: When the matching text isn't part of the page title, help the
// user locate the matching text by linking directly to / scrolling to the
Expand Down
Loading