toiljs 0.0.12 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/build/client/.tsbuildinfo +1 -1
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/generate.js +7 -1
- package/examples/basic/client/routes/search.tsx +61 -61
- package/package.json +1 -1
- package/src/client/search/search.ts +189 -189
- package/src/client/search/use-page-search.ts +73 -73
- package/src/compiler/generate.ts +391 -378
- package/src/compiler/pages.ts +2 -2
- package/test/dom/error-overlay.test.tsx +44 -44
- package/test/dom/router-loading.test.tsx +1 -1
- package/test/seo.test.ts +164 -164
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* React binding for the page-metadata {@link searchPages search}. Gives a route component reactive,
|
|
3
|
-
* memoized search results plus a `goTo` helper that navigates straight to a matched page
|
|
4
|
-
* for a site-wide "jump to page" / command-palette style search box.
|
|
5
|
-
*/
|
|
6
|
-
import { useMemo } from 'react';
|
|
7
|
-
|
|
8
|
-
import { navigate, type NavigateOptions } from '../navigation/navigation.js';
|
|
9
|
-
import type { Href } from '../types.js';
|
|
10
|
-
import {
|
|
11
|
-
getPages,
|
|
12
|
-
type PageMeta,
|
|
13
|
-
pagePath,
|
|
14
|
-
type PageSearchOptions,
|
|
15
|
-
type PageSearchResult,
|
|
16
|
-
searchPages,
|
|
17
|
-
} from './search.js';
|
|
18
|
-
|
|
19
|
-
/** What {@link usePageSearch} returns. */
|
|
20
|
-
export interface PageSearch {
|
|
21
|
-
/** Ranked matches for the current query (best first); empty when the query is blank. */
|
|
22
|
-
readonly results: readonly PageSearchResult[];
|
|
23
|
-
/** The full registered page index (handy for rendering an "all pages" listing). */
|
|
24
|
-
readonly pages: readonly PageMeta[];
|
|
25
|
-
/**
|
|
26
|
-
* Navigates to a result / page / raw path. A dynamic (`:param`) page can't be navigated to
|
|
27
|
-
* as-is, so passing one (or its result) is a no-op unless you pass a concrete path string with
|
|
28
|
-
* the params already filled in. A stable reference, safe to destructure.
|
|
29
|
-
*/
|
|
30
|
-
readonly goTo: (
|
|
31
|
-
target: string | PageMeta | PageSearchResult,
|
|
32
|
-
options?: NavigateOptions,
|
|
33
|
-
) => void;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Whether a path can be navigated to directly (no unfilled dynamic segments). */
|
|
37
|
-
function isNavigable(path: string): boolean {
|
|
38
|
-
return !/[:*]/.test(path);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Searches the project's pages by `query` and returns ranked {@link PageSearchResult}s, recomputed
|
|
43
|
-
* only when the query or options change. Use the returned `goTo` to redirect to a match:
|
|
44
|
-
*
|
|
45
|
-
* ```tsx
|
|
46
|
-
* const { results, goTo } = usePageSearch(query);
|
|
47
|
-
* return results.map((r) => (
|
|
48
|
-
* <button key={r.page.path} onClick={() => { goTo(r); }}>{r.page.metadata.title ?? r.page.path}</button>
|
|
49
|
-
* ));
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export function usePageSearch(query: string, options: PageSearchOptions = {}): PageSearch {
|
|
53
|
-
const { limit, includeDynamic, fields } = options;
|
|
54
|
-
const fieldsKey = fields?.join(',');
|
|
55
|
-
const results = useMemo(
|
|
56
|
-
() => searchPages(query, { limit, includeDynamic, fields }),
|
|
57
|
-
// `fields` is compared by content (fieldsKey) so a fresh array literal each render is fine.
|
|
58
|
-
[query, limit, includeDynamic, fieldsKey],
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
return useMemo<PageSearch>(
|
|
62
|
-
() => ({
|
|
63
|
-
results,
|
|
64
|
-
pages: getPages(),
|
|
65
|
-
goTo(target, navOptions) {
|
|
66
|
-
const path = pagePath(target);
|
|
67
|
-
if (typeof target !== 'string' && !isNavigable(path)) return;
|
|
68
|
-
navigate(path as Href, navOptions);
|
|
69
|
-
},
|
|
70
|
-
}),
|
|
71
|
-
[results],
|
|
72
|
-
);
|
|
73
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* React binding for the page-metadata {@link searchPages search}. Gives a route component reactive,
|
|
3
|
+
* memoized search results plus a `goTo` helper that navigates straight to a matched page, a drop-in
|
|
4
|
+
* for a site-wide "jump to page" / command-palette style search box.
|
|
5
|
+
*/
|
|
6
|
+
import { useMemo } from 'react';
|
|
7
|
+
|
|
8
|
+
import { navigate, type NavigateOptions } from '../navigation/navigation.js';
|
|
9
|
+
import type { Href } from '../types.js';
|
|
10
|
+
import {
|
|
11
|
+
getPages,
|
|
12
|
+
type PageMeta,
|
|
13
|
+
pagePath,
|
|
14
|
+
type PageSearchOptions,
|
|
15
|
+
type PageSearchResult,
|
|
16
|
+
searchPages,
|
|
17
|
+
} from './search.js';
|
|
18
|
+
|
|
19
|
+
/** What {@link usePageSearch} returns. */
|
|
20
|
+
export interface PageSearch {
|
|
21
|
+
/** Ranked matches for the current query (best first); empty when the query is blank. */
|
|
22
|
+
readonly results: readonly PageSearchResult[];
|
|
23
|
+
/** The full registered page index (handy for rendering an "all pages" listing). */
|
|
24
|
+
readonly pages: readonly PageMeta[];
|
|
25
|
+
/**
|
|
26
|
+
* Navigates to a result / page / raw path. A dynamic (`:param`) page can't be navigated to
|
|
27
|
+
* as-is, so passing one (or its result) is a no-op unless you pass a concrete path string with
|
|
28
|
+
* the params already filled in. A stable reference, safe to destructure.
|
|
29
|
+
*/
|
|
30
|
+
readonly goTo: (
|
|
31
|
+
target: string | PageMeta | PageSearchResult,
|
|
32
|
+
options?: NavigateOptions,
|
|
33
|
+
) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Whether a path can be navigated to directly (no unfilled dynamic segments). */
|
|
37
|
+
function isNavigable(path: string): boolean {
|
|
38
|
+
return !/[:*]/.test(path);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Searches the project's pages by `query` and returns ranked {@link PageSearchResult}s, recomputed
|
|
43
|
+
* only when the query or options change. Use the returned `goTo` to redirect to a match:
|
|
44
|
+
*
|
|
45
|
+
* ```tsx
|
|
46
|
+
* const { results, goTo } = usePageSearch(query);
|
|
47
|
+
* return results.map((r) => (
|
|
48
|
+
* <button key={r.page.path} onClick={() => { goTo(r); }}>{r.page.metadata.title ?? r.page.path}</button>
|
|
49
|
+
* ));
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export function usePageSearch(query: string, options: PageSearchOptions = {}): PageSearch {
|
|
53
|
+
const { limit, includeDynamic, fields } = options;
|
|
54
|
+
const fieldsKey = fields?.join(',');
|
|
55
|
+
const results = useMemo(
|
|
56
|
+
() => searchPages(query, { limit, includeDynamic, fields }),
|
|
57
|
+
// `fields` is compared by content (fieldsKey) so a fresh array literal each render is fine.
|
|
58
|
+
[query, limit, includeDynamic, fieldsKey],
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return useMemo<PageSearch>(
|
|
62
|
+
() => ({
|
|
63
|
+
results,
|
|
64
|
+
pages: getPages(),
|
|
65
|
+
goTo(target, navOptions) {
|
|
66
|
+
const path = pagePath(target);
|
|
67
|
+
if (typeof target !== 'string' && !isNavigable(path)) return;
|
|
68
|
+
navigate(path as Href, navOptions);
|
|
69
|
+
},
|
|
70
|
+
}),
|
|
71
|
+
[results],
|
|
72
|
+
);
|
|
73
|
+
}
|