fumadocs-core 12.2.4 → 12.3.0

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,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 };
@@ -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 pathnameIsMissingLocale = languages.every(
26
- (locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
29
+ const pathLocale = languages.find(
30
+ (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
27
31
  );
28
- if (pathnameIsMissingLocale) {
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
- return NextResponse.redirect(new URL(format(locale, path), request.url));
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
  };
@@ -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;
@@ -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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fumadocs-core",
3
- "version": "12.2.4",
3
+ "version": "12.3.0",
4
4
  "description": "The library for building a documentation website in Next.js",
5
5
  "keywords": [
6
6
  "NextJs",