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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions assets/js/algolia-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,29 @@ export class AlgoliaResults {
}

render(results) {
// We don't want to render pages where the URL is reference to a node.
// That means that the result is a page that has been unpublished.
const filtered_pages_hits = results.pages.hits.filter(
result => !/node\//.test(result._search_result_url)
);
const filtered_pages = Object.assign({}, results.pages, {
hits: filtered_pages_hits
});
const filtered_results = Object.assign({}, results, {
pages: filtered_pages
});

if (this._container) {
this._container.innerHTML = this._groups
.map(group => this._renderGroup(results, group))
.map(group => this._renderGroup(filtered_results, group))
.join("");

Array.from(
document.getElementsByClassName(AlgoliaResult.SELECTORS.result)
).forEach(this.addResultClickHandler);

this._groups.forEach(group => this._addShowMoreListener(group));
this._addLocationListeners(results);
this._addLocationListeners(filtered_results);
}
}

Expand Down
1 change: 1 addition & 0 deletions assets/ts/ui/autocomplete/__autocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type StopItem = {
type ContentItem = {
_content_type: string;
_content_url: string;
_search_result_url: string;
content_title: string;
} & AlgoliaItem;

Expand Down
4 changes: 4 additions & 0 deletions assets/ts/ui/autocomplete/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export function isContentItem(x: Item): x is ContentItem {
return Object.keys(x).includes("_content_type");
}

export function isSearchResultItem(x: Item): x is ContentItem {
return isContentItem(x) && x._content_type == "search_result";
}

export const getTitleAttribute = (item: Item): string[] => {
if (isStopItem(item)) {
return ["stop", "name"];
Expand Down
10 changes: 9 additions & 1 deletion assets/ts/ui/autocomplete/plugins/algolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SearchResponse } from "@algolia/client-search";
import { AutocompleteJSPlugin, debounced } from "../plugins";
import AlgoliaItemTemplate from "../templates/algolia";
import { AutocompleteItem } from "../__autocomplete";
import { isSearchResultItem } from "../helpers";

/**
* Generates a plugin for Algolia Autocomplete which enables searching for our
Expand Down Expand Up @@ -36,7 +37,14 @@ export default function createAlgoliaBackendPlugin(
.then(resp =>
(resp.results as SearchResponse[]).flatMap(
({ hits, index }) =>
hits.map(hit => ({ ...hit, index } as AutocompleteItem))
hits
.map(hit => ({ ...hit, index } as AutocompleteItem))
.filter(
item =>
!isSearchResultItem(item) ||
(isSearchResultItem(item) &&
!/node\//.test(item._search_result_url))
)
)
),
300
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;

var 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:/, "");
}

// 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
69 changes: 69 additions & 0 deletions playwright-report/index.html

Large diffs are not rendered by default.

Loading