fumadocs-core 12.2.5 → 12.3.1
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/middleware.d.ts +22 -6
- package/dist/middleware.js +16 -5
- package/dist/search/client.d.ts +2 -1
- package/dist/search/client.js +2 -2
- package/dist/source/index.d.ts +8 -2
- package/dist/source/index.js +5 -3
- package/package.json +1 -1
package/dist/middleware.d.ts
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
import { NextMiddleware } from 'next/dist/server/web/types';
|
|
2
2
|
|
|
3
3
|
interface MiddlewareOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Supported locale codes
|
|
6
|
+
*/
|
|
4
7
|
languages: string[];
|
|
8
|
+
/**
|
|
9
|
+
* Default locale if not specified
|
|
10
|
+
*/
|
|
5
11
|
defaultLanguage: string;
|
|
12
|
+
/**
|
|
13
|
+
* A function that adds the locale prefix to path name
|
|
14
|
+
*/
|
|
6
15
|
format?: (locale: string, path: string) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Don't show the locale prefix on URL.
|
|
18
|
+
*
|
|
19
|
+
* - `always`: Always hide the prefix
|
|
20
|
+
* - `default-locale`: Only hide the default locale
|
|
21
|
+
* - `never`: Never hide the prefix
|
|
22
|
+
*
|
|
23
|
+
* This API uses `NextResponse.rewrite`.
|
|
24
|
+
*
|
|
25
|
+
* @defaultValue 'never'
|
|
26
|
+
*/
|
|
27
|
+
hideLocale?: 'always' | 'default-locale' | 'never';
|
|
7
28
|
}
|
|
8
|
-
|
|
9
|
-
* @param languages - Supported locale codes
|
|
10
|
-
* @param defaultLanguage - Default local if not specified
|
|
11
|
-
* @param format - A function that returns the redirected url with locale code
|
|
12
|
-
*/
|
|
13
|
-
declare function createI18nMiddleware({ languages, defaultLanguage, format, }: MiddlewareOptions): NextMiddleware;
|
|
29
|
+
declare function createI18nMiddleware({ languages, defaultLanguage, format, hideLocale, }: MiddlewareOptions): NextMiddleware;
|
|
14
30
|
|
|
15
31
|
export { createI18nMiddleware };
|
package/dist/middleware.js
CHANGED
|
@@ -18,20 +18,31 @@ var defaultFormat = (locale, path) => {
|
|
|
18
18
|
function createI18nMiddleware({
|
|
19
19
|
languages,
|
|
20
20
|
defaultLanguage,
|
|
21
|
-
format = defaultFormat
|
|
21
|
+
format = defaultFormat,
|
|
22
|
+
hideLocale = "never"
|
|
22
23
|
}) {
|
|
24
|
+
function shouldHideLocale(locale) {
|
|
25
|
+
return hideLocale === "always" || hideLocale === "default-locale" && locale === defaultLanguage;
|
|
26
|
+
}
|
|
23
27
|
return (request) => {
|
|
24
28
|
const { pathname } = request.nextUrl;
|
|
25
|
-
const
|
|
26
|
-
(locale) =>
|
|
29
|
+
const pathLocale = languages.find(
|
|
30
|
+
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
|
|
27
31
|
);
|
|
28
|
-
if (
|
|
32
|
+
if (!pathLocale) {
|
|
29
33
|
const locale = getLocale(request, languages, defaultLanguage);
|
|
30
34
|
let path = pathname;
|
|
31
35
|
while (path.startsWith("/")) {
|
|
32
36
|
path = path.slice(1);
|
|
33
37
|
}
|
|
34
|
-
|
|
38
|
+
const url = new URL(format(locale, path), request.url);
|
|
39
|
+
return shouldHideLocale(locale) ? NextResponse.rewrite(url) : NextResponse.redirect(url);
|
|
40
|
+
}
|
|
41
|
+
if (hideLocale === "default-locale" && pathLocale === defaultLanguage) {
|
|
42
|
+
const path = pathLocale.slice(`/${pathLocale}`.length);
|
|
43
|
+
return NextResponse.redirect(
|
|
44
|
+
new URL(path.startsWith("/") ? path : `/${path}`, request.url)
|
|
45
|
+
);
|
|
35
46
|
}
|
|
36
47
|
return NextResponse.next();
|
|
37
48
|
};
|
package/dist/search/client.d.ts
CHANGED
|
@@ -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 };
|
package/dist/search/client.js
CHANGED
|
@@ -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,
|
|
22
|
+
const debouncedValue = useDebounce(search, delayMs);
|
|
23
23
|
const query = useSWR(
|
|
24
24
|
[api, debouncedValue, locale, tag],
|
|
25
25
|
(args) => fetchDocs(...args),
|
package/dist/source/index.d.ts
CHANGED
|
@@ -88,7 +88,7 @@ interface SourceConfig {
|
|
|
88
88
|
pageData: PageData;
|
|
89
89
|
metaData: MetaData;
|
|
90
90
|
}
|
|
91
|
-
interface LoaderOptions {
|
|
91
|
+
interface LoaderOptions extends Pick<BuildPageTreeOptionsWithI18n, 'languages' | 'defaultLanguage'> {
|
|
92
92
|
/**
|
|
93
93
|
* @defaultValue `''`
|
|
94
94
|
*/
|
|
@@ -97,7 +97,6 @@ interface LoaderOptions {
|
|
|
97
97
|
* @defaultValue `'/'`
|
|
98
98
|
*/
|
|
99
99
|
baseUrl?: string;
|
|
100
|
-
languages?: string[];
|
|
101
100
|
icon?: NonNullable<BuildPageTreeOptions['resolveIcon']>;
|
|
102
101
|
slugs?: LoadOptions['getSlugs'];
|
|
103
102
|
url?: UrlFn;
|
|
@@ -197,7 +196,14 @@ interface BuildPageTreeOptions {
|
|
|
197
196
|
resolveIcon?: (icon: string | undefined) => ReactElement | undefined;
|
|
198
197
|
}
|
|
199
198
|
interface BuildPageTreeOptionsWithI18n extends BuildPageTreeOptions {
|
|
199
|
+
/**
|
|
200
|
+
* Build a page tree for each language
|
|
201
|
+
*/
|
|
200
202
|
languages?: string[];
|
|
203
|
+
/**
|
|
204
|
+
* Hide the locale prefix from URLs if it is same as the specified default locale.
|
|
205
|
+
*/
|
|
206
|
+
defaultLanguage?: string;
|
|
201
207
|
}
|
|
202
208
|
interface PageTreeBuilder {
|
|
203
209
|
build: (options: BuildPageTreeOptions) => Root;
|
package/dist/source/index.js
CHANGED
|
@@ -205,10 +205,10 @@ function createPageTreeBuilder() {
|
|
|
205
205
|
});
|
|
206
206
|
},
|
|
207
207
|
buildI18n(_a) {
|
|
208
|
-
var _b = _a, { languages = [] } = _b, options = __objRest(_b, ["languages"]);
|
|
208
|
+
var _b = _a, { languages = [], defaultLanguage } = _b, options = __objRest(_b, ["languages", "defaultLanguage"]);
|
|
209
209
|
const entries = languages.map((lang) => {
|
|
210
210
|
const tree = build({
|
|
211
|
-
lang,
|
|
211
|
+
lang: lang === defaultLanguage ? void 0 : lang,
|
|
212
212
|
options,
|
|
213
213
|
builder: this,
|
|
214
214
|
storage: options.storage
|
|
@@ -372,6 +372,7 @@ function createOutput({
|
|
|
372
372
|
baseUrl = "/",
|
|
373
373
|
slugs: slugsFn = getSlugs,
|
|
374
374
|
url: getUrl = createGetUrl(baseUrl),
|
|
375
|
+
defaultLanguage,
|
|
375
376
|
pageTree: pageTreeOptions = {}
|
|
376
377
|
}) {
|
|
377
378
|
const storage = loadFiles(
|
|
@@ -392,7 +393,8 @@ function createOutput({
|
|
|
392
393
|
languages,
|
|
393
394
|
storage,
|
|
394
395
|
resolveIcon,
|
|
395
|
-
getUrl
|
|
396
|
+
getUrl,
|
|
397
|
+
defaultLanguage
|
|
396
398
|
}, pageTreeOptions));
|
|
397
399
|
return {
|
|
398
400
|
pageTree,
|