fumadocs-core 13.3.1 → 13.3.3
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/dist/search/server.d.ts +12 -6
- package/dist/search/server.js +106 -77
- package/dist/server/index.d.ts +5 -1
- package/dist/server/index.js +2 -0
- package/dist/source/index.js +9 -2
- package/dist/toc.js +1 -2
- package/package.json +1 -1
package/dist/search/server.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NextRequest
|
|
1
|
+
import { NextRequest } from 'next/server';
|
|
2
2
|
import { S as StructuredData } from '../remark-structure-Dj8oa5Ba.js';
|
|
3
3
|
import { SortedResult } from './shared.js';
|
|
4
4
|
import { I as I18nConfig } from '../config-inq6kP6y.js';
|
|
@@ -28,8 +28,14 @@ type ToI18n<T extends {
|
|
|
28
28
|
};
|
|
29
29
|
declare function createI18nSearchAPI<T extends 'simple' | 'advanced'>(type: T, options: T extends 'simple' ? ToI18n<SimpleOptions> : ToI18n<AdvancedOptions>): SearchAPI;
|
|
30
30
|
|
|
31
|
-
interface
|
|
32
|
-
|
|
31
|
+
interface SearchServer {
|
|
32
|
+
search: (query: string, options?: {
|
|
33
|
+
locale?: string;
|
|
34
|
+
tag?: string;
|
|
35
|
+
}) => Promise<SortedResult[]>;
|
|
36
|
+
}
|
|
37
|
+
interface SearchAPI extends SearchServer {
|
|
38
|
+
GET: (request: NextRequest) => Promise<Response>;
|
|
33
39
|
search: (query: string, options?: {
|
|
34
40
|
locale?: string;
|
|
35
41
|
tag?: string;
|
|
@@ -61,7 +67,7 @@ interface Index {
|
|
|
61
67
|
url: string;
|
|
62
68
|
keywords?: string;
|
|
63
69
|
}
|
|
64
|
-
declare function
|
|
70
|
+
declare function initSimpleSearch({ indexes, language, }: SimpleOptions): SearchServer;
|
|
65
71
|
interface AdvancedIndex {
|
|
66
72
|
id: string;
|
|
67
73
|
title: string;
|
|
@@ -77,6 +83,6 @@ interface AdvancedIndex {
|
|
|
77
83
|
structuredData: StructuredData;
|
|
78
84
|
url: string;
|
|
79
85
|
}
|
|
80
|
-
declare function
|
|
86
|
+
declare function initAdvancedSearch({ indexes, language, tag, }: AdvancedOptions): SearchServer;
|
|
81
87
|
|
|
82
|
-
export { type AdvancedIndex, type AdvancedOptions, type Dynamic, type Index, type SearchAPI, type SimpleOptions, createI18nSearchAPI, createI18nSearchAPI$1 as createI18nSearchAPIExperimental, createSearchAPI,
|
|
88
|
+
export { type AdvancedIndex, type AdvancedOptions, type Dynamic, type Index, type SearchAPI, type SearchServer, type SimpleOptions, createI18nSearchAPI, createI18nSearchAPI$1 as createI18nSearchAPIExperimental, createSearchAPI, initAdvancedSearch, initSimpleSearch };
|
package/dist/search/server.js
CHANGED
|
@@ -4,14 +4,14 @@ import "../chunk-MLKGABMK.js";
|
|
|
4
4
|
import { Document } from "flexsearch";
|
|
5
5
|
|
|
6
6
|
// src/search/create-endpoint.ts
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
function createEndpoint(server) {
|
|
8
|
+
const { search } = server;
|
|
9
9
|
return {
|
|
10
10
|
search,
|
|
11
11
|
async GET(request) {
|
|
12
12
|
const query = request.nextUrl.searchParams.get("query");
|
|
13
|
-
if (!query) return
|
|
14
|
-
return
|
|
13
|
+
if (!query) return Response.json([]);
|
|
14
|
+
return Response.json(
|
|
15
15
|
await search(query, {
|
|
16
16
|
tag: request.nextUrl.searchParams.get("tag") ?? void 0,
|
|
17
17
|
locale: request.nextUrl.searchParams.get("locale") ?? void 0
|
|
@@ -24,23 +24,43 @@ function createEndpoint(search) {
|
|
|
24
24
|
// src/search/i18n-api.ts
|
|
25
25
|
function createI18nSearchAPI(type, options) {
|
|
26
26
|
const map = /* @__PURE__ */ new Map();
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
async function init() {
|
|
28
|
+
if (options.i18n.languages.length === 0) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const indexes = typeof options.indexes === "function" ? await options.indexes() : options.indexes;
|
|
32
|
+
for (const locale of options.i18n.languages) {
|
|
33
|
+
const localeIndexes = indexes.filter((index) => index.locale === locale);
|
|
34
|
+
if (type === "simple") {
|
|
35
|
+
map.set(
|
|
36
|
+
locale,
|
|
37
|
+
initSimpleSearch({
|
|
38
|
+
...options,
|
|
39
|
+
language: locale,
|
|
40
|
+
indexes: localeIndexes
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
map.set(
|
|
46
|
+
locale,
|
|
47
|
+
initAdvancedSearch({
|
|
32
48
|
...options,
|
|
33
49
|
language: locale,
|
|
34
|
-
indexes:
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
|
|
50
|
+
indexes: localeIndexes
|
|
51
|
+
})
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return createEndpoint({
|
|
56
|
+
search: async (query, searchOptions) => {
|
|
57
|
+
if (map.size === 0) await init();
|
|
58
|
+
const handler = map.get(
|
|
59
|
+
searchOptions?.locale ?? options.i18n.defaultLanguage
|
|
60
|
+
);
|
|
61
|
+
if (handler) return handler.search(query, searchOptions);
|
|
62
|
+
return [];
|
|
38
63
|
}
|
|
39
|
-
const handler = map.get(
|
|
40
|
-
searchOptions?.locale ?? options.i18n.defaultLanguage
|
|
41
|
-
);
|
|
42
|
-
if (handler) return handler.search(query, searchOptions);
|
|
43
|
-
return [];
|
|
44
64
|
});
|
|
45
65
|
}
|
|
46
66
|
|
|
@@ -59,23 +79,28 @@ function createI18nSearchAPI2(type, options) {
|
|
|
59
79
|
})
|
|
60
80
|
);
|
|
61
81
|
}
|
|
62
|
-
return createEndpoint(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
82
|
+
return createEndpoint({
|
|
83
|
+
search: async (query, searchOptions) => {
|
|
84
|
+
if (searchOptions?.locale) {
|
|
85
|
+
const handler = map.get(searchOptions.locale);
|
|
86
|
+
if (handler) return handler.search(query, searchOptions);
|
|
87
|
+
}
|
|
88
|
+
return [];
|
|
66
89
|
}
|
|
67
|
-
return [];
|
|
68
90
|
});
|
|
69
91
|
}
|
|
70
92
|
|
|
71
93
|
// src/search/server.ts
|
|
72
94
|
function createSearchAPI(type, options) {
|
|
73
95
|
if (type === "simple") {
|
|
74
|
-
return
|
|
96
|
+
return createEndpoint(initSimpleSearch(options));
|
|
75
97
|
}
|
|
76
|
-
return
|
|
98
|
+
return createEndpoint(initAdvancedSearch(options));
|
|
77
99
|
}
|
|
78
|
-
function
|
|
100
|
+
function initSimpleSearch({
|
|
101
|
+
indexes,
|
|
102
|
+
language
|
|
103
|
+
}) {
|
|
79
104
|
const store = ["title", "url"];
|
|
80
105
|
async function getDocument() {
|
|
81
106
|
const items = typeof indexes === "function" ? await indexes() : indexes;
|
|
@@ -128,21 +153,23 @@ function initSearchAPI({ indexes, language }) {
|
|
|
128
153
|
return index;
|
|
129
154
|
}
|
|
130
155
|
const doc = getDocument();
|
|
131
|
-
return
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
156
|
+
return {
|
|
157
|
+
search: async (query) => {
|
|
158
|
+
const results = (await doc).search(query, 5, {
|
|
159
|
+
enrich: true,
|
|
160
|
+
suggest: true
|
|
161
|
+
});
|
|
162
|
+
if (results.length === 0) return [];
|
|
163
|
+
return results[0].result.map((page) => ({
|
|
164
|
+
type: "page",
|
|
165
|
+
content: page.doc.title,
|
|
166
|
+
id: page.doc.url,
|
|
167
|
+
url: page.doc.url
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
};
|
|
144
171
|
}
|
|
145
|
-
function
|
|
172
|
+
function initAdvancedSearch({
|
|
146
173
|
indexes,
|
|
147
174
|
language,
|
|
148
175
|
tag = false
|
|
@@ -218,49 +245,51 @@ function initSearchAPIAdvanced({
|
|
|
218
245
|
return index;
|
|
219
246
|
}
|
|
220
247
|
const doc = getDocument();
|
|
221
|
-
return
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if (
|
|
232
|
-
map.
|
|
248
|
+
return {
|
|
249
|
+
search: async (query, options) => {
|
|
250
|
+
const index = await doc;
|
|
251
|
+
const results = index.search(query, 5, {
|
|
252
|
+
enrich: true,
|
|
253
|
+
tag: options?.tag,
|
|
254
|
+
limit: 6
|
|
255
|
+
});
|
|
256
|
+
const map = /* @__PURE__ */ new Map();
|
|
257
|
+
for (const item of results[0]?.result ?? []) {
|
|
258
|
+
if (item.doc.type === "page") {
|
|
259
|
+
if (!map.has(item.doc.id)) {
|
|
260
|
+
map.set(item.doc.id, []);
|
|
261
|
+
}
|
|
262
|
+
continue;
|
|
233
263
|
}
|
|
234
|
-
|
|
264
|
+
const list = map.get(item.doc.page_id) ?? [];
|
|
265
|
+
list.push({
|
|
266
|
+
id: item.doc.id,
|
|
267
|
+
content: item.doc.content,
|
|
268
|
+
type: item.doc.type,
|
|
269
|
+
url: item.doc.url
|
|
270
|
+
});
|
|
271
|
+
map.set(item.doc.page_id, list);
|
|
235
272
|
}
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
sortedResult.push({
|
|
250
|
-
id: page.id,
|
|
251
|
-
content: page.content,
|
|
252
|
-
type: "page",
|
|
253
|
-
url: page.url
|
|
254
|
-
});
|
|
255
|
-
sortedResult.push(...items);
|
|
273
|
+
const sortedResult = [];
|
|
274
|
+
for (const [id, items] of map.entries()) {
|
|
275
|
+
const page = index.get(id);
|
|
276
|
+
if (!page) continue;
|
|
277
|
+
sortedResult.push({
|
|
278
|
+
id: page.id,
|
|
279
|
+
content: page.content,
|
|
280
|
+
type: "page",
|
|
281
|
+
url: page.url
|
|
282
|
+
});
|
|
283
|
+
sortedResult.push(...items);
|
|
284
|
+
}
|
|
285
|
+
return sortedResult;
|
|
256
286
|
}
|
|
257
|
-
|
|
258
|
-
});
|
|
287
|
+
};
|
|
259
288
|
}
|
|
260
289
|
export {
|
|
261
290
|
createI18nSearchAPI2 as createI18nSearchAPI,
|
|
262
291
|
createI18nSearchAPI as createI18nSearchAPIExperimental,
|
|
263
292
|
createSearchAPI,
|
|
264
|
-
|
|
265
|
-
|
|
293
|
+
initAdvancedSearch,
|
|
294
|
+
initSimpleSearch
|
|
266
295
|
};
|
package/dist/server/index.d.ts
CHANGED
|
@@ -35,6 +35,10 @@ interface GetGithubLastCommitOptions {
|
|
|
35
35
|
* GitHub access token
|
|
36
36
|
*/
|
|
37
37
|
token?: string;
|
|
38
|
+
/**
|
|
39
|
+
* SHA or ref (branch or tag) name.
|
|
40
|
+
*/
|
|
41
|
+
sha?: string;
|
|
38
42
|
/**
|
|
39
43
|
* Custom query parameters
|
|
40
44
|
*/
|
|
@@ -47,6 +51,6 @@ interface GetGithubLastCommitOptions {
|
|
|
47
51
|
* By default, this will cache the result forever.
|
|
48
52
|
* Set `options.next.revalidate` to customise this.
|
|
49
53
|
*/
|
|
50
|
-
declare function getGithubLastEdit({ repo, token, owner, path, options, params: customParams, }: GetGithubLastCommitOptions): Promise<Date | null>;
|
|
54
|
+
declare function getGithubLastEdit({ repo, token, owner, path, sha, options, params: customParams, }: GetGithubLastCommitOptions): Promise<Date | null>;
|
|
51
55
|
|
|
52
56
|
export { type GetGithubLastCommitOptions, findNeighbour, flattenTree, getGithubLastEdit, separatePageTree };
|
package/dist/server/index.js
CHANGED
|
@@ -55,6 +55,7 @@ async function getGithubLastEdit({
|
|
|
55
55
|
token,
|
|
56
56
|
owner,
|
|
57
57
|
path,
|
|
58
|
+
sha,
|
|
58
59
|
options = {},
|
|
59
60
|
params: customParams = {}
|
|
60
61
|
}) {
|
|
@@ -63,6 +64,7 @@ async function getGithubLastEdit({
|
|
|
63
64
|
params.set("path", path);
|
|
64
65
|
params.set("page", "1");
|
|
65
66
|
params.set("per_page", "1");
|
|
67
|
+
if (sha) params.set("sha", sha);
|
|
66
68
|
for (const [key, value] of Object.entries(customParams)) {
|
|
67
69
|
params.set(key, value);
|
|
68
70
|
}
|
package/dist/source/index.js
CHANGED
|
@@ -13,7 +13,7 @@ function parseFilePath(path) {
|
|
|
13
13
|
const dotIdx = base.lastIndexOf(".");
|
|
14
14
|
const nameWithLocale = dotIdx !== -1 ? base.slice(0, dotIdx) : base;
|
|
15
15
|
const flattenedPath = [dirname, nameWithLocale].filter((p) => p.length > 0).join("/");
|
|
16
|
-
const [name, locale] = nameWithLocale
|
|
16
|
+
const [name, locale] = getLocale(nameWithLocale);
|
|
17
17
|
return {
|
|
18
18
|
dirname,
|
|
19
19
|
name,
|
|
@@ -25,7 +25,7 @@ function parseFilePath(path) {
|
|
|
25
25
|
function parseFolderPath(path) {
|
|
26
26
|
const segments = splitPath(slash(path));
|
|
27
27
|
const base = segments.at(-1) ?? "";
|
|
28
|
-
const [name, locale] = base
|
|
28
|
+
const [name, locale] = getLocale(base);
|
|
29
29
|
const flattenedPath = segments.join("/");
|
|
30
30
|
return {
|
|
31
31
|
dirname: segments.slice(0, -1).join("/"),
|
|
@@ -35,6 +35,13 @@ function parseFolderPath(path) {
|
|
|
35
35
|
path: flattenedPath
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
|
+
function getLocale(name) {
|
|
39
|
+
const sep = name.lastIndexOf(".");
|
|
40
|
+
if (sep === -1) return [name];
|
|
41
|
+
const locale = name.slice(sep + 1);
|
|
42
|
+
if (/\d+/.exec(locale)) return [name];
|
|
43
|
+
return [name.slice(0, sep), locale];
|
|
44
|
+
}
|
|
38
45
|
function normalizePath(path) {
|
|
39
46
|
const segments = splitPath(slash(path));
|
|
40
47
|
if (segments[0] === "." || segments[0] === "..")
|
package/dist/toc.js
CHANGED
|
@@ -49,8 +49,7 @@ function useAnchorObserver(watch, single) {
|
|
|
49
49
|
if (element.scrollTop === 0 && single) setActiveAnchor(watch.slice(0, 1));
|
|
50
50
|
else if (element.scrollTop + element.clientHeight >= element.scrollHeight - 6) {
|
|
51
51
|
setActiveAnchor((active) => {
|
|
52
|
-
|
|
53
|
-
return last ? watch.slice(watch.indexOf(last)) : active;
|
|
52
|
+
return active.length > 0 ? watch.slice(watch.indexOf(active[0])) : watch.slice(-1);
|
|
54
53
|
});
|
|
55
54
|
}
|
|
56
55
|
}
|