mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-24 09:12:05 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b157e9d90c | |||
| 0f80705d3a |
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"gitbook": patch
|
||||
---
|
||||
|
||||
Apply the default language-aware search scope to legacy search links.
|
||||
@@ -15,8 +15,6 @@ export function computeRemoteSearchScope(
|
||||
}
|
||||
|
||||
switch (scope) {
|
||||
case 'all':
|
||||
return { mode: 'all' };
|
||||
case 'default':
|
||||
return { mode: 'current', siteSpaceId };
|
||||
case 'extended':
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { NuqsTestingAdapter } from 'nuqs/adapters/testing';
|
||||
import { createElement } from 'react';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
|
||||
import { shouldKeepSearchState } from './useSearch';
|
||||
import { SearchContextProvider, shouldKeepSearchState, useSearchState } from './useSearch';
|
||||
|
||||
function SearchScopeValue() {
|
||||
return useSearchState()?.scope ?? null;
|
||||
}
|
||||
|
||||
describe('shouldKeepSearchState', () => {
|
||||
it('discards an empty default search state', () => {
|
||||
@@ -23,3 +30,25 @@ describe('shouldKeepSearchState', () => {
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
['scope=all', 'default'],
|
||||
['global=true', 'default'],
|
||||
['scope=current&global=true', 'current'],
|
||||
['scope=extended&global=true', 'extended'],
|
||||
])('resolves %s to %s for a linked search', (params, scope) => {
|
||||
const search = createElement(
|
||||
SearchContextProvider,
|
||||
{ prewarmURL: '/~gitbook/search' },
|
||||
createElement(SearchScopeValue)
|
||||
);
|
||||
|
||||
expect(
|
||||
renderToString(
|
||||
createElement(NuqsTestingAdapter, {
|
||||
searchParams: `q=setup&${params}`,
|
||||
children: search,
|
||||
})
|
||||
)
|
||||
).toBe(scope);
|
||||
});
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import {
|
||||
type Values,
|
||||
parseAsBoolean,
|
||||
parseAsString,
|
||||
parseAsStringLiteral,
|
||||
useQueryStates,
|
||||
} from 'nuqs';
|
||||
import { type Values, parseAsString, parseAsStringLiteral, useQueryStates } from 'nuqs';
|
||||
import React from 'react';
|
||||
|
||||
import type { LinkProps } from '../primitives';
|
||||
@@ -16,8 +10,6 @@ import { useSearchPrewarm } from './useSearchPrewarm';
|
||||
import type { DirectSearchProps } from '@/lib/search-results';
|
||||
|
||||
export type SearchScope =
|
||||
/** Search all content on the site */
|
||||
| 'all'
|
||||
/** Search the current section's variant + matched/default variant for other sections */
|
||||
| 'default'
|
||||
/** Search all variants of the current section */
|
||||
@@ -41,9 +33,8 @@ export interface SearchState {
|
||||
const keyMap = {
|
||||
q: parseAsString,
|
||||
ask: parseAsString,
|
||||
scope: parseAsStringLiteral(['all', 'default', 'extended', 'current']).withDefault('default'),
|
||||
scope: parseAsStringLiteral(['default', 'extended', 'current']).withDefault('default'),
|
||||
section: parseAsString,
|
||||
global: parseAsBoolean, // Legacy support for global=true
|
||||
};
|
||||
|
||||
export type UpdateSearchState = (
|
||||
@@ -62,11 +53,6 @@ function normalizeRawState(values: Values<typeof keyMap>) {
|
||||
if (values.ask === 'true' && values.q) {
|
||||
return { ...values, q: null, ask: values.q };
|
||||
}
|
||||
// Handle legacy global=true
|
||||
if (values.global === true) {
|
||||
return { ...values, scope: 'all' as const, global: null };
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user