Skip to content

Commit

Permalink
Get URL for search result (#1868)
Browse files Browse the repository at this point in the history
* search result links should go to referenced url

* filter out pages without a real url

* filter out search results that point to nodes

* change to use _search_result_url

* revert filtering

* type

* cover entity reference too

* add type so ts stops complaining

* linting

* only change url if internal or entity
  • Loading branch information
anthonyshull committed Feb 5, 2024
1 parent 1aefd79 commit 202eab2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
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(/(internal|entity):/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

0 comments on commit 202eab2

Please sign in to comment.