fumadocs-core 12.3.0 → 12.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.
@@ -8,7 +8,7 @@ type DynamicLinkProps = LinkProps;
8
8
  * It supports dynamic hrefs, which means you can use `/[lang]/my-page` with `dynamicHrefs` enabled
9
9
  */
10
10
  declare const DynamicLink: react.ForwardRefExoticComponent<react.AnchorHTMLAttributes<HTMLAnchorElement> & {
11
- external?: boolean | undefined;
11
+ external?: boolean;
12
12
  } & react.RefAttributes<HTMLAnchorElement>>;
13
13
 
14
14
  export { DynamicLink, type DynamicLinkProps, DynamicLink as default };
package/dist/link.d.ts CHANGED
@@ -18,7 +18,7 @@ declare const Link: react.ForwardRefExoticComponent<AnchorHTMLAttributes<HTMLAnc
18
18
  *
19
19
  * automatically determined by default
20
20
  */
21
- external?: boolean | undefined;
21
+ external?: boolean;
22
22
  } & react.RefAttributes<HTMLAnchorElement>>;
23
23
 
24
24
  export { type LinkProps, Link as default };
@@ -12,7 +12,8 @@ interface UseDocsSearch {
12
12
  * @param locale - Filter with locale
13
13
  * @param tag - Filter with specific tag
14
14
  * @param api - The Search API URL
15
+ * @param delayMs - The debounced delay for performing a search.
15
16
  */
16
- declare function useDocsSearch(locale?: string, tag?: string, api?: string): UseDocsSearch;
17
+ declare function useDocsSearch(locale?: string, tag?: string, api?: string, delayMs?: number): UseDocsSearch;
17
18
 
18
19
  export { useDocsSearch };
@@ -17,9 +17,9 @@ function fetchDocs(api, query, locale, tag) {
17
17
  return yield res.json();
18
18
  });
19
19
  }
20
- function useDocsSearch(locale, tag, api = "/api/search") {
20
+ function useDocsSearch(locale, tag, api = "/api/search", delayMs = 100) {
21
21
  const [search, setSearch] = useState("");
22
- const debouncedValue = useDebounce(search, 100);
22
+ const debouncedValue = useDebounce(search, delayMs);
23
23
  const query = useSWR(
24
24
  [api, debouncedValue, locale, tag],
25
25
  (args) => fetchDocs(...args),
@@ -5,18 +5,22 @@ import 'mdast';
5
5
  import 'unified';
6
6
 
7
7
  interface SearchAPI {
8
- GET: (request: NextRequest) => NextResponse<SortedResult[]>;
8
+ GET: (request: NextRequest) => Promise<NextResponse<SortedResult[]>>;
9
9
  search: (query: string, options?: {
10
10
  locale?: string;
11
11
  tag?: string;
12
- }) => SortedResult[];
12
+ }) => Promise<SortedResult[]>;
13
13
  }
14
+ /**
15
+ * Resolve indexes dynamically
16
+ */
17
+ type Dynamic<T> = () => T[] | Promise<T[]>;
14
18
  interface SimpleOptions {
15
- indexes: Index[];
19
+ indexes: Index[] | Dynamic<Index>;
16
20
  language?: string;
17
21
  }
18
22
  interface AdvancedOptions {
19
- indexes: AdvancedIndex[];
23
+ indexes: AdvancedIndex[] | Dynamic<AdvancedIndex>;
20
24
  /**
21
25
  * Enabled custom tags
22
26
  */
@@ -1,4 +1,5 @@
1
1
  import {
2
+ __async,
2
3
  __spreadProps,
3
4
  __spreadValues
4
5
  } from "../chunk-CWMXXUWU.js";
@@ -10,15 +11,17 @@ function create(search) {
10
11
  return {
11
12
  search,
12
13
  GET(request) {
13
- var _a, _b;
14
- const query = request.nextUrl.searchParams.get("query");
15
- if (!query) return NextResponse.json([]);
16
- return NextResponse.json(
17
- search(query, {
18
- tag: (_a = request.nextUrl.searchParams.get("tag")) != null ? _a : void 0,
19
- locale: (_b = request.nextUrl.searchParams.get("locale")) != null ? _b : void 0
20
- })
21
- );
14
+ return __async(this, null, function* () {
15
+ var _a2, _b;
16
+ const query = request.nextUrl.searchParams.get("query");
17
+ if (!query) return NextResponse.json([]);
18
+ return NextResponse.json(
19
+ yield search(query, {
20
+ tag: (_a2 = request.nextUrl.searchParams.get("tag")) != null ? _a2 : void 0,
21
+ locale: (_b = request.nextUrl.searchParams.get("locale")) != null ? _b : void 0
22
+ })
23
+ );
24
+ });
22
25
  }
23
26
  };
24
27
  }
@@ -34,62 +37,69 @@ function createI18nSearchAPI(type, options) {
34
37
  const v = Array.isArray(entry) ? { language: entry[0], indexes: entry[1] } : entry;
35
38
  map.set(
36
39
  v.language,
40
+ // @ts-expect-error -- Index depends on generic types
37
41
  createSearchAPI(type, __spreadProps(__spreadValues({}, options), {
38
42
  language: v.language,
39
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment -- Avoid complicated types
40
43
  indexes: v.indexes
41
44
  }))
42
45
  );
43
46
  }
44
- return create((query, searchOptions) => {
47
+ return create((query, searchOptions) => __async(this, null, function* () {
45
48
  if (searchOptions == null ? void 0 : searchOptions.locale) {
46
49
  const handler = map.get(searchOptions.locale);
47
50
  if (handler) return handler.search(query, searchOptions);
48
51
  }
49
52
  return [];
50
- });
53
+ }));
51
54
  }
52
55
  function initSearchAPI({ indexes, language }) {
53
56
  const store = ["title", "url"];
54
- const index = new Document({
55
- language,
56
- optimize: true,
57
- cache: 100,
58
- document: {
59
- id: "url",
60
- store,
61
- index: [
62
- {
63
- field: "title",
64
- tokenize: "forward",
65
- resolution: 9
66
- },
67
- {
68
- field: "content",
69
- tokenize: "strict",
70
- context: {
71
- depth: 1,
72
- resolution: 9
73
- }
74
- },
75
- {
76
- field: "keywords",
77
- tokenize: "strict",
78
- resolution: 9
57
+ function getDocument() {
58
+ return __async(this, null, function* () {
59
+ const items = typeof indexes === "function" ? yield indexes() : indexes;
60
+ const index = new Document({
61
+ language,
62
+ optimize: true,
63
+ cache: 100,
64
+ document: {
65
+ id: "url",
66
+ store,
67
+ index: [
68
+ {
69
+ field: "title",
70
+ tokenize: "forward",
71
+ resolution: 9
72
+ },
73
+ {
74
+ field: "content",
75
+ tokenize: "strict",
76
+ context: {
77
+ depth: 1,
78
+ resolution: 9
79
+ }
80
+ },
81
+ {
82
+ field: "keywords",
83
+ tokenize: "strict",
84
+ resolution: 9
85
+ }
86
+ ]
79
87
  }
80
- ]
81
- }
82
- });
83
- for (const page of indexes) {
84
- index.add({
85
- title: page.title,
86
- url: page.url,
87
- content: page.content,
88
- keywords: page.keywords
88
+ });
89
+ for (const page of items) {
90
+ index.add({
91
+ title: page.title,
92
+ url: page.url,
93
+ content: page.content,
94
+ keywords: page.keywords
95
+ });
96
+ }
97
+ return index;
89
98
  });
90
99
  }
91
- return create((query) => {
92
- const results = index.search(query, 5, {
100
+ const doc = getDocument();
101
+ return create((query) => __async(this, null, function* () {
102
+ const results = (yield doc).search(query, 5, {
93
103
  enrich: true,
94
104
  suggest: true
95
105
  });
@@ -100,7 +110,7 @@ function initSearchAPI({ indexes, language }) {
100
110
  id: page.doc.url,
101
111
  url: page.doc.url
102
112
  }));
103
- });
113
+ }));
104
114
  }
105
115
  function initSearchAPIAdvanced({
106
116
  indexes,
@@ -108,64 +118,71 @@ function initSearchAPIAdvanced({
108
118
  tag = false
109
119
  }) {
110
120
  const store = ["id", "url", "content", "page_id", "type"];
111
- const index = new Document({
112
- language,
113
- cache: 100,
114
- tokenize: "forward",
115
- optimize: true,
116
- context: {
117
- depth: 2,
118
- bidirectional: true,
119
- resolution: 9
120
- },
121
- document: {
122
- id: "id",
123
- tag: tag ? "tag" : void 0,
124
- store,
125
- index: ["content"]
126
- }
127
- });
128
- for (const page of indexes) {
129
- const data = page.structuredData;
130
- let id = 0;
131
- index.add({
132
- id: page.id,
133
- page_id: page.id,
134
- type: "page",
135
- content: page.title,
136
- tag: page.tag,
137
- url: page.url
138
- });
139
- for (const heading of data.headings) {
140
- index.add({
141
- id: page.id + (id++).toString(),
142
- page_id: page.id,
143
- type: "heading",
144
- tag: page.tag,
145
- url: `${page.url}#${heading.id}`,
146
- content: heading.content
147
- });
148
- }
149
- for (const content of data.contents) {
150
- index.add({
151
- id: page.id + (id++).toString(),
152
- page_id: page.id,
153
- tag: page.tag,
154
- type: "text",
155
- url: content.heading ? `${page.url}#${content.heading}` : page.url,
156
- content: content.content
121
+ function getDocument() {
122
+ return __async(this, null, function* () {
123
+ const items = typeof indexes === "function" ? yield indexes() : indexes;
124
+ const index = new Document({
125
+ language,
126
+ cache: 100,
127
+ tokenize: "forward",
128
+ optimize: true,
129
+ context: {
130
+ depth: 2,
131
+ bidirectional: true,
132
+ resolution: 9
133
+ },
134
+ document: {
135
+ id: "id",
136
+ tag: tag ? "tag" : void 0,
137
+ store,
138
+ index: ["content"]
139
+ }
157
140
  });
158
- }
141
+ for (const page of items) {
142
+ const data = page.structuredData;
143
+ let id = 0;
144
+ index.add({
145
+ id: page.id,
146
+ page_id: page.id,
147
+ type: "page",
148
+ content: page.title,
149
+ tag: page.tag,
150
+ url: page.url
151
+ });
152
+ for (const heading of data.headings) {
153
+ index.add({
154
+ id: page.id + (id++).toString(),
155
+ page_id: page.id,
156
+ type: "heading",
157
+ tag: page.tag,
158
+ url: `${page.url}#${heading.id}`,
159
+ content: heading.content
160
+ });
161
+ }
162
+ for (const content of data.contents) {
163
+ index.add({
164
+ id: page.id + (id++).toString(),
165
+ page_id: page.id,
166
+ tag: page.tag,
167
+ type: "text",
168
+ url: content.heading ? `${page.url}#${content.heading}` : page.url,
169
+ content: content.content
170
+ });
171
+ }
172
+ }
173
+ return index;
174
+ });
159
175
  }
160
- return create((query, options) => {
176
+ const doc = getDocument();
177
+ return create((query, options) => __async(this, null, function* () {
161
178
  var _a, _b, _c;
179
+ const index = yield doc;
162
180
  const results = index.search(query, 5, {
163
181
  enrich: true,
164
182
  tag: options == null ? void 0 : options.tag,
165
183
  limit: 6
166
184
  });
167
185
  const map = /* @__PURE__ */ new Map();
168
- const sortedResult = [];
169
186
  for (const item of (_b = (_a = results[0]) == null ? void 0 : _a.result) != null ? _b : []) {
170
187
  if (item.doc.type === "page") {
171
188
  if (!map.has(item.doc.page_id)) {
@@ -173,18 +190,16 @@ function initSearchAPIAdvanced({
173
190
  }
174
191
  continue;
175
192
  }
176
- const i = {
193
+ const list = (_c = map.get(item.doc.page_id)) != null ? _c : [];
194
+ list.push({
177
195
  id: item.doc.id,
178
196
  content: item.doc.content,
179
197
  type: item.doc.type,
180
198
  url: item.doc.url
181
- };
182
- if (map.has(item.doc.page_id)) {
183
- (_c = map.get(item.doc.page_id)) == null ? void 0 : _c.push(i);
184
- } else {
185
- map.set(item.doc.page_id, [i]);
186
- }
199
+ });
200
+ map.set(item.doc.page_id, list);
187
201
  }
202
+ const sortedResult = [];
188
203
  for (const [id, items] of map.entries()) {
189
204
  const page = index.get(id);
190
205
  if (!page) continue;
@@ -197,7 +212,7 @@ function initSearchAPIAdvanced({
197
212
  sortedResult.push(...items);
198
213
  }
199
214
  return sortedResult;
200
- });
215
+ }));
201
216
  }
202
217
  export {
203
218
  createI18nSearchAPI,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fumadocs-core",
3
- "version": "12.3.0",
3
+ "version": "12.3.2",
4
4
  "description": "The library for building a documentation website in Next.js",
5
5
  "keywords": [
6
6
  "NextJs",
@@ -120,8 +120,8 @@
120
120
  ],
121
121
  "dependencies": {
122
122
  "@formatjs/intl-localematcher": "^0.5.4",
123
- "@shikijs/rehype": "^1.6.5",
124
- "@shikijs/transformers": "^1.6.5",
123
+ "@shikijs/rehype": "^1.10.0",
124
+ "@shikijs/transformers": "^1.10.0",
125
125
  "flexsearch": "0.7.21",
126
126
  "github-slugger": "^2.0.0",
127
127
  "negotiator": "^0.6.3",
@@ -131,12 +131,12 @@
131
131
  "remark-gfm": "^4.0.0",
132
132
  "remark-mdx": "^3.0.1",
133
133
  "scroll-into-view-if-needed": "^3.1.0",
134
- "shiki": "^1.6.5",
134
+ "shiki": "^1.10.0",
135
135
  "swr": "^2.2.5",
136
136
  "unist-util-visit": "^5.0.0"
137
137
  },
138
138
  "devDependencies": {
139
- "@algolia/client-search": "^4.23.3",
139
+ "@algolia/client-search": "^4.24.0",
140
140
  "@mdx-js/mdx": "^3.0.1",
141
141
  "@types/estree-jsx": "^1.0.5",
142
142
  "@types/flexsearch": "0.7.6",
@@ -146,9 +146,9 @@
146
146
  "@types/node": "18.17.5",
147
147
  "@types/react": "^18.3.3",
148
148
  "@types/react-dom": "^18.3.0",
149
- "algoliasearch": "^4.23.3",
149
+ "algoliasearch": "^4.24.0",
150
150
  "next": "^14.2.4",
151
- "unified": "^11.0.4",
151
+ "unified": "^11.0.5",
152
152
  "eslint-config-custom": "0.0.0",
153
153
  "tsconfig": "0.0.0"
154
154
  },