Skip to content

Commit

Permalink
feat: filter results from full text search with locale (nodejs#3166)
Browse files Browse the repository at this point in the history
Co-authored-by: leapful <leapful>
Co-authored-by: Shanmughapriyan S <priyanshan03@gmail.com>
Co-authored-by: Claudio Wunder <cwunder@gnome.org>
Fixes https://github.com/nodejs/nodejs.dev/issues/3091
  • Loading branch information
leapful authored Jan 31, 2023
1 parent 15d82e4 commit d55be89
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const gatsbyConfig = {
'displayTitle',
'description',
'category',
'locale',
'tableOfContents',
],
resolvers: {
Expand All @@ -131,6 +132,7 @@ const gatsbyConfig = {
description: node => node.frontmatter.description,
slug: node => node.fields.slug,
category: node => node.fields.categoryName,
locale: node => node.fields.locale,
tableOfContents: node => {
if (node.frontmatter.category === 'api') {
// We only do the Table of Contents resolution for API pages as for Learn pages searching by the title and description should be enough
Expand Down
22 changes: 19 additions & 3 deletions src/hooks/useSearchResults.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { useMemo, useCallback } from 'react';
import { graphql, useStaticQuery } from 'gatsby';
import { Index, SerialisedIndexData } from 'elasticlunr';
import { useLocalization } from 'gatsby-theme-i18n';
import { compareTwoStrings } from 'string-similarity';
import { replaceDataTagFromString } from '../util/replaceDataTag';
import createSlug from '../../util-node/createSlug';
import { SearchResult } from '../types';

export const useSearchResults = () => {
const { locale } = useLocalization();

const { siteSearchIndex } = useStaticQuery(graphql`
query localSearchLearnPages {
siteSearchIndex {
Expand All @@ -24,9 +27,21 @@ export const useSearchResults = () => {

const searchResults = useCallback(
(currentQuery: string) => {
const currentResults = storeIndex
.search(currentQuery, { expand: true })
.map(({ ref }) => storeIndex.documentStore.getDoc(ref) as SearchResult);
const localeResults: SearchResult[] = [];
const fallbackResults: SearchResult[] = [];

storeIndex.search(currentQuery, { expand: true }).forEach(({ ref }) => {
const result = storeIndex.documentStore.getDoc(ref) as SearchResult;

if (result.locale === locale) {
localeResults.push(result);
} else if (result.locale === 'en') {
fallbackResults.push(result);
}
});

const currentResults =
localeResults.length > 0 ? localeResults : fallbackResults;

const mapResult = (result: SearchResult) => {
if (result.tableOfContents) {
Expand All @@ -45,6 +60,7 @@ export const useSearchResults = () => {
slug: `${result.slug}#${createSlug(item)}`,
category: result.category,
wrapInCode: item.startsWith('`'),
locale: result.locale,
}));
}

Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type SearchResult = {
displayTitle?: string | JSX.Element;
tableOfContents?: string;
wrapInCode?: boolean;
locale: string;
};

export type WrapPageElementBrowser =
Expand Down

0 comments on commit d55be89

Please sign in to comment.