fumadocs-core 13.3.1 → 13.3.2

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.
@@ -1,4 +1,4 @@
1
- import { NextRequest, NextResponse } from 'next/server';
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 SearchAPI {
32
- GET: (request: NextRequest) => Promise<NextResponse<SortedResult[]>>;
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 initSearchAPI({ indexes, language }: SimpleOptions): SearchAPI;
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 initSearchAPIAdvanced({ indexes, language, tag, }: AdvancedOptions): SearchAPI;
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, initSearchAPI, initSearchAPIAdvanced };
88
+ export { type AdvancedIndex, type AdvancedOptions, type Dynamic, type Index, type SearchAPI, type SearchServer, type SimpleOptions, createI18nSearchAPI, createI18nSearchAPI$1 as createI18nSearchAPIExperimental, createSearchAPI, initAdvancedSearch, initSimpleSearch };
@@ -4,14 +4,14 @@ import "../chunk-MLKGABMK.js";
4
4
  import { Document } from "flexsearch";
5
5
 
6
6
  // src/search/create-endpoint.ts
7
- import { NextResponse } from "next/server";
8
- function createEndpoint(search) {
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 NextResponse.json([]);
14
- return NextResponse.json(
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
- return createEndpoint(async (query, searchOptions) => {
28
- if (map.size === 0) {
29
- const indexes = typeof options.indexes === "function" ? await options.indexes() : options.indexes;
30
- for (const locale of options.i18n.languages) {
31
- const api = createSearchAPI(type, {
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: indexes.filter((index) => index.locale === locale)
35
- });
36
- map.set(locale, api);
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(async (query, searchOptions) => {
63
- if (searchOptions?.locale) {
64
- const handler = map.get(searchOptions.locale);
65
- if (handler) return handler.search(query, searchOptions);
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 initSearchAPI(options);
96
+ return createEndpoint(initSimpleSearch(options));
75
97
  }
76
- return initSearchAPIAdvanced(options);
98
+ return createEndpoint(initAdvancedSearch(options));
77
99
  }
78
- function initSearchAPI({ indexes, language }) {
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 createEndpoint(async (query) => {
132
- const results = (await doc).search(query, 5, {
133
- enrich: true,
134
- suggest: true
135
- });
136
- if (results.length === 0) return [];
137
- return results[0].result.map((page) => ({
138
- type: "page",
139
- content: page.doc.title,
140
- id: page.doc.url,
141
- url: page.doc.url
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 initSearchAPIAdvanced({
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 createEndpoint(async (query, options) => {
222
- const index = await doc;
223
- const results = index.search(query, 5, {
224
- enrich: true,
225
- tag: options?.tag,
226
- limit: 6
227
- });
228
- const map = /* @__PURE__ */ new Map();
229
- for (const item of results[0]?.result ?? []) {
230
- if (item.doc.type === "page") {
231
- if (!map.has(item.doc.id)) {
232
- map.set(item.doc.id, []);
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
- continue;
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 list = map.get(item.doc.page_id) ?? [];
237
- list.push({
238
- id: item.doc.id,
239
- content: item.doc.content,
240
- type: item.doc.type,
241
- url: item.doc.url
242
- });
243
- map.set(item.doc.page_id, list);
244
- }
245
- const sortedResult = [];
246
- for (const [id, items] of map.entries()) {
247
- const page = index.get(id);
248
- if (!page) continue;
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
- return sortedResult;
258
- });
287
+ };
259
288
  }
260
289
  export {
261
290
  createI18nSearchAPI2 as createI18nSearchAPI,
262
291
  createI18nSearchAPI as createI18nSearchAPIExperimental,
263
292
  createSearchAPI,
264
- initSearchAPI,
265
- initSearchAPIAdvanced
293
+ initAdvancedSearch,
294
+ initSimpleSearch
266
295
  };
@@ -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 };
@@ -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/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
- const last = active.at(-1);
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fumadocs-core",
3
- "version": "13.3.1",
3
+ "version": "13.3.2",
4
4
  "description": "The library for building a documentation website in Next.js",
5
5
  "keywords": [
6
6
  "NextJs",