@tuwaio/docs-ui 0.5.7 → 0.7.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.
- package/README.md +93 -3
- package/bin/i18n-check.js +19 -0
- package/dist/components/DocsLayout.cjs +105 -0
- package/dist/components/DocsLayout.d.cts +93 -0
- package/dist/components/DocsLayout.d.ts +93 -0
- package/dist/components/DocsLayout.js +80 -0
- package/dist/components/Footer.cjs +1 -6
- package/dist/components/Footer.d.cts +3 -2
- package/dist/components/Footer.d.ts +3 -2
- package/dist/components/Footer.js +1 -6
- package/dist/components/LocalizedPage.cjs +42 -0
- package/dist/components/LocalizedPage.d.cts +37 -0
- package/dist/components/LocalizedPage.d.ts +37 -0
- package/dist/components/LocalizedPage.js +18 -0
- package/dist/docsI18n.cjs +33 -0
- package/dist/docsI18n.d.cts +16 -0
- package/dist/docsI18n.d.ts +16 -0
- package/dist/docsI18n.js +8 -0
- package/dist/docsLabels.cjs +140 -0
- package/dist/docsLabels.d.cts +72 -0
- package/dist/docsLabels.d.ts +72 -0
- package/dist/docsLabels.js +116 -0
- package/dist/docsPage.cjs +70 -0
- package/dist/docsPage.d.cts +56 -0
- package/dist/docsPage.d.ts +56 -0
- package/dist/docsPage.js +46 -0
- package/dist/i18n.d.cts +1 -1
- package/dist/i18n.d.ts +1 -1
- package/dist/index.cjs +4 -0
- package/dist/index.css +3 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/nextra.cjs +26 -0
- package/dist/nextra.d.cts +11 -0
- package/dist/nextra.d.ts +11 -0
- package/dist/nextra.js +3 -0
- package/dist/pageMap.cjs +66 -0
- package/dist/pageMap.d.cts +33 -0
- package/dist/pageMap.d.ts +33 -0
- package/dist/pageMap.js +42 -0
- package/dist/translationCheck.cjs +191 -0
- package/dist/translationCheck.d.cts +90 -0
- package/dist/translationCheck.d.ts +90 -0
- package/dist/translationCheck.js +162 -0
- package/package.json +15 -3
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { Metadata } from 'next';
|
|
3
|
+
import { EvaluateResult } from 'nextra';
|
|
4
|
+
import { TuwaLocale } from './locale.cjs';
|
|
5
|
+
|
|
6
|
+
/** A translated language of a page: a function that imports its MDX file (`() => import('./uk/index.mdx')`). */
|
|
7
|
+
type LocalizedPageLoaders = Partial<Record<Exclude<TuwaLocale, 'en'>, () => Promise<EvaluateResult>>>;
|
|
8
|
+
/** Props of the `[[...mdxPath]]` page of a documentation site. */
|
|
9
|
+
interface DocsPageProps {
|
|
10
|
+
/** The route segments after the site root. */
|
|
11
|
+
params: Promise<{
|
|
12
|
+
mdxPath?: string[];
|
|
13
|
+
}>;
|
|
14
|
+
/** Query parameters of the request. */
|
|
15
|
+
searchParams?: Promise<Record<string, string | string[] | undefined>>;
|
|
16
|
+
}
|
|
17
|
+
/** Options of {@link createDocsPage}. */
|
|
18
|
+
interface CreateDocsPageOptions {
|
|
19
|
+
/**
|
|
20
|
+
* The translated pages of the site by route (`/` for the Introduction, `/packages` for the packages overview).
|
|
21
|
+
* Pages that are not listed (package READMEs, the generated reference) stay in English in every language.
|
|
22
|
+
*/
|
|
23
|
+
localizedPages?: Readonly<Record<string, LocalizedPageLoaders>>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates the `[[...mdxPath]]` route of a TUWA documentation site (the pages of `src/content`): the static params, the
|
|
27
|
+
* metadata of each page and the page component, which renders the English page and its translations into a
|
|
28
|
+
* `LocalizedPage`. Re-export them from `src/app/[[...mdxPath]]/page.tsx`; the layout must be `DocsLayout`
|
|
29
|
+
* (`@tuwaio/docs-ui/nextra`). Side effects: imports the MDX files of the requested page and of its translations; the
|
|
30
|
+
* metadata stays English.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* import { createDocsPage } from '@tuwaio/docs-ui/nextra/page';
|
|
35
|
+
*
|
|
36
|
+
* import { localizedPages } from '@/i18n/pages';
|
|
37
|
+
*
|
|
38
|
+
* const { generateStaticParams, generateMetadata, Page } = createDocsPage({ localizedPages });
|
|
39
|
+
*
|
|
40
|
+
* export { generateMetadata, generateStaticParams };
|
|
41
|
+
* export default Page;
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @param options - See {@link CreateDocsPageOptions}.
|
|
45
|
+
* @returns `generateStaticParams`, `generateMetadata` and `Page`.
|
|
46
|
+
*/
|
|
47
|
+
declare function createDocsPage({ localizedPages }?: CreateDocsPageOptions): {
|
|
48
|
+
generateStaticParams: () => Promise<{
|
|
49
|
+
[x: string]: string | string[];
|
|
50
|
+
}[]>;
|
|
51
|
+
generateMetadata: (props: DocsPageProps) => Promise<Metadata>;
|
|
52
|
+
Page: (props: DocsPageProps) => Promise<react.JSX.Element>;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export { createDocsPage };
|
|
56
|
+
export type { CreateDocsPageOptions, DocsPageProps, LocalizedPageLoaders };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { Metadata } from 'next';
|
|
3
|
+
import { EvaluateResult } from 'nextra';
|
|
4
|
+
import { TuwaLocale } from './locale.js';
|
|
5
|
+
|
|
6
|
+
/** A translated language of a page: a function that imports its MDX file (`() => import('./uk/index.mdx')`). */
|
|
7
|
+
type LocalizedPageLoaders = Partial<Record<Exclude<TuwaLocale, 'en'>, () => Promise<EvaluateResult>>>;
|
|
8
|
+
/** Props of the `[[...mdxPath]]` page of a documentation site. */
|
|
9
|
+
interface DocsPageProps {
|
|
10
|
+
/** The route segments after the site root. */
|
|
11
|
+
params: Promise<{
|
|
12
|
+
mdxPath?: string[];
|
|
13
|
+
}>;
|
|
14
|
+
/** Query parameters of the request. */
|
|
15
|
+
searchParams?: Promise<Record<string, string | string[] | undefined>>;
|
|
16
|
+
}
|
|
17
|
+
/** Options of {@link createDocsPage}. */
|
|
18
|
+
interface CreateDocsPageOptions {
|
|
19
|
+
/**
|
|
20
|
+
* The translated pages of the site by route (`/` for the Introduction, `/packages` for the packages overview).
|
|
21
|
+
* Pages that are not listed (package READMEs, the generated reference) stay in English in every language.
|
|
22
|
+
*/
|
|
23
|
+
localizedPages?: Readonly<Record<string, LocalizedPageLoaders>>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates the `[[...mdxPath]]` route of a TUWA documentation site (the pages of `src/content`): the static params, the
|
|
27
|
+
* metadata of each page and the page component, which renders the English page and its translations into a
|
|
28
|
+
* `LocalizedPage`. Re-export them from `src/app/[[...mdxPath]]/page.tsx`; the layout must be `DocsLayout`
|
|
29
|
+
* (`@tuwaio/docs-ui/nextra`). Side effects: imports the MDX files of the requested page and of its translations; the
|
|
30
|
+
* metadata stays English.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* import { createDocsPage } from '@tuwaio/docs-ui/nextra/page';
|
|
35
|
+
*
|
|
36
|
+
* import { localizedPages } from '@/i18n/pages';
|
|
37
|
+
*
|
|
38
|
+
* const { generateStaticParams, generateMetadata, Page } = createDocsPage({ localizedPages });
|
|
39
|
+
*
|
|
40
|
+
* export { generateMetadata, generateStaticParams };
|
|
41
|
+
* export default Page;
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @param options - See {@link CreateDocsPageOptions}.
|
|
45
|
+
* @returns `generateStaticParams`, `generateMetadata` and `Page`.
|
|
46
|
+
*/
|
|
47
|
+
declare function createDocsPage({ localizedPages }?: CreateDocsPageOptions): {
|
|
48
|
+
generateStaticParams: () => Promise<{
|
|
49
|
+
[x: string]: string | string[];
|
|
50
|
+
}[]>;
|
|
51
|
+
generateMetadata: (props: DocsPageProps) => Promise<Metadata>;
|
|
52
|
+
Page: (props: DocsPageProps) => Promise<react.JSX.Element>;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export { createDocsPage };
|
|
56
|
+
export type { CreateDocsPageOptions, DocsPageProps, LocalizedPageLoaders };
|
package/dist/docsPage.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { notFound } from "next/navigation";
|
|
3
|
+
import { generateStaticParamsFor, importPage } from "nextra/pages";
|
|
4
|
+
import { LocalizedPage } from "./components/LocalizedPage";
|
|
5
|
+
function isAssetOrInternal(mdxPath) {
|
|
6
|
+
if (!mdxPath || mdxPath.length === 0) return false;
|
|
7
|
+
if (mdxPath[0] === "_next") return true;
|
|
8
|
+
return Boolean(mdxPath[mdxPath.length - 1]?.includes("."));
|
|
9
|
+
}
|
|
10
|
+
const routeOf = (mdxPath) => `/${(mdxPath ?? []).join("/")}`;
|
|
11
|
+
function variantOf(module, props, params) {
|
|
12
|
+
const { default: MDXContent, toc, metadata, sourceCode } = module;
|
|
13
|
+
return { content: /* @__PURE__ */ jsx(MDXContent, { ...props, params }), toc, metadata, sourceCode };
|
|
14
|
+
}
|
|
15
|
+
function createDocsPage({ localizedPages = {} } = {}) {
|
|
16
|
+
const generateStaticParams = generateStaticParamsFor("mdxPath");
|
|
17
|
+
async function generateMetadata(props) {
|
|
18
|
+
const { mdxPath } = await props.params;
|
|
19
|
+
if (isAssetOrInternal(mdxPath)) return {};
|
|
20
|
+
try {
|
|
21
|
+
const { metadata } = await importPage(mdxPath);
|
|
22
|
+
return metadata;
|
|
23
|
+
} catch {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function Page(props) {
|
|
28
|
+
const params = await props.params;
|
|
29
|
+
if (isAssetOrInternal(params.mdxPath)) notFound();
|
|
30
|
+
let english;
|
|
31
|
+
try {
|
|
32
|
+
english = await importPage(params.mdxPath);
|
|
33
|
+
} catch {
|
|
34
|
+
notFound();
|
|
35
|
+
}
|
|
36
|
+
const loaders = Object.entries(localizedPages[routeOf(params.mdxPath)] ?? {});
|
|
37
|
+
const translations = await Promise.all(
|
|
38
|
+
loaders.map(async ([locale, load]) => [locale, variantOf(await load(), props, params)])
|
|
39
|
+
);
|
|
40
|
+
return /* @__PURE__ */ jsx(LocalizedPage, { variants: { ...Object.fromEntries(translations), en: variantOf(english, props, params) } });
|
|
41
|
+
}
|
|
42
|
+
return { generateStaticParams, generateMetadata, Page };
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
createDocsPage
|
|
46
|
+
};
|
package/dist/i18n.d.cts
CHANGED
package/dist/i18n.d.ts
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -42,8 +42,10 @@ __reExport(index_exports, require("./components/Orb"), module.exports);
|
|
|
42
42
|
__reExport(index_exports, require("./components/PolicyDocument"), module.exports);
|
|
43
43
|
var import_StarryBackground = __toESM(require("./components/StarryBackground"), 1);
|
|
44
44
|
__reExport(index_exports, require("./components/ThemeSwitcher"), module.exports);
|
|
45
|
+
__reExport(index_exports, require("./docsLabels"), module.exports);
|
|
45
46
|
__reExport(index_exports, require("./i18n"), module.exports);
|
|
46
47
|
__reExport(index_exports, require("./locale"), module.exports);
|
|
48
|
+
__reExport(index_exports, require("./pageMap"), module.exports);
|
|
47
49
|
__reExport(index_exports, require("./policies"), module.exports);
|
|
48
50
|
__reExport(index_exports, require("./preferences"), module.exports);
|
|
49
51
|
__reExport(index_exports, require("./utils"), module.exports);
|
|
@@ -58,8 +60,10 @@ __reExport(index_exports, require("./utils"), module.exports);
|
|
|
58
60
|
...require("./components/Orb"),
|
|
59
61
|
...require("./components/PolicyDocument"),
|
|
60
62
|
...require("./components/ThemeSwitcher"),
|
|
63
|
+
...require("./docsLabels"),
|
|
61
64
|
...require("./i18n"),
|
|
62
65
|
...require("./locale"),
|
|
66
|
+
...require("./pageMap"),
|
|
63
67
|
...require("./policies"),
|
|
64
68
|
...require("./preferences"),
|
|
65
69
|
...require("./utils")
|
package/dist/index.css
CHANGED
package/dist/index.d.cts
CHANGED
|
@@ -7,9 +7,12 @@ export { Orb, OrbProps, PackageType } from './components/Orb.cjs';
|
|
|
7
7
|
export { PolicyDocument, PolicyDocumentProps } from './components/PolicyDocument.cjs';
|
|
8
8
|
export { default as StarryBackground } from './components/StarryBackground.cjs';
|
|
9
9
|
export { ThemeSwitcher, ThemeSwitcherProps } from './components/ThemeSwitcher.cjs';
|
|
10
|
+
export { DocsLabels, docsLabels } from './docsLabels.cjs';
|
|
10
11
|
export { I18nContextValue, I18nKit, I18nProviderProps, LocaleState, LocaleStateOptions, LocaleVariant, createI18n, useLocaleState } from './i18n.cjs';
|
|
11
12
|
export { DEFAULT_LOCALE, LOCALE_STORAGE_KEY, LocaleSignals, TUWA_LOCALES, TuwaLocale, detectLocale, formatLabel, normalizeLocale, policyLanguageOf, resolveClientLocale } from './locale.cjs';
|
|
13
|
+
export { LocalizePageMapOptions, localizePageMap } from './pageMap.cjs';
|
|
12
14
|
export { POLICIES_INDEX_URL, POLICY_DOCUMENT_LABELS, POLICY_LANGUAGES, POLICY_LANGUAGE_NAMES, POLICY_SITE_URL, POLICY_SOURCE, Policy, PolicyContent, PolicyDocumentLabels, PolicyLanguage, PolicySection, PolicySlug, footerPolicies, formatPolicyDate, getPolicyFileUrl, getPolicyPageUrl, loadPolicy, policies, policyLanguageFrom, publishedPolicies } from './policies.cjs';
|
|
13
15
|
export { PREFERENCE_DAYS, PreferenceExpiry, expireStoredPreferences } from './preferences.cjs';
|
|
14
16
|
export { baseFooterLinks, baseNavLinks } from './utils.cjs';
|
|
15
17
|
import 'react';
|
|
18
|
+
import 'nextra';
|
package/dist/index.d.ts
CHANGED
|
@@ -7,9 +7,12 @@ export { Orb, OrbProps, PackageType } from './components/Orb.js';
|
|
|
7
7
|
export { PolicyDocument, PolicyDocumentProps } from './components/PolicyDocument.js';
|
|
8
8
|
export { default as StarryBackground } from './components/StarryBackground.js';
|
|
9
9
|
export { ThemeSwitcher, ThemeSwitcherProps } from './components/ThemeSwitcher.js';
|
|
10
|
+
export { DocsLabels, docsLabels } from './docsLabels.js';
|
|
10
11
|
export { I18nContextValue, I18nKit, I18nProviderProps, LocaleState, LocaleStateOptions, LocaleVariant, createI18n, useLocaleState } from './i18n.js';
|
|
11
12
|
export { DEFAULT_LOCALE, LOCALE_STORAGE_KEY, LocaleSignals, TUWA_LOCALES, TuwaLocale, detectLocale, formatLabel, normalizeLocale, policyLanguageOf, resolveClientLocale } from './locale.js';
|
|
13
|
+
export { LocalizePageMapOptions, localizePageMap } from './pageMap.js';
|
|
12
14
|
export { POLICIES_INDEX_URL, POLICY_DOCUMENT_LABELS, POLICY_LANGUAGES, POLICY_LANGUAGE_NAMES, POLICY_SITE_URL, POLICY_SOURCE, Policy, PolicyContent, PolicyDocumentLabels, PolicyLanguage, PolicySection, PolicySlug, footerPolicies, formatPolicyDate, getPolicyFileUrl, getPolicyPageUrl, loadPolicy, policies, policyLanguageFrom, publishedPolicies } from './policies.js';
|
|
13
15
|
export { PREFERENCE_DAYS, PreferenceExpiry, expireStoredPreferences } from './preferences.js';
|
|
14
16
|
export { baseFooterLinks, baseNavLinks } from './utils.js';
|
|
15
17
|
import 'react';
|
|
18
|
+
import 'nextra';
|
package/dist/index.js
CHANGED
|
@@ -7,8 +7,10 @@ export * from "./components/Orb";
|
|
|
7
7
|
export * from "./components/PolicyDocument";
|
|
8
8
|
import { default as default3 } from "./components/StarryBackground";
|
|
9
9
|
export * from "./components/ThemeSwitcher";
|
|
10
|
+
export * from "./docsLabels";
|
|
10
11
|
export * from "./i18n";
|
|
11
12
|
export * from "./locale";
|
|
13
|
+
export * from "./pageMap";
|
|
12
14
|
export * from "./policies";
|
|
13
15
|
export * from "./preferences";
|
|
14
16
|
export * from "./utils";
|
package/dist/nextra.cjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var nextra_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(nextra_exports);
|
|
18
|
+
__reExport(nextra_exports, require("./components/DocsLayout"), module.exports);
|
|
19
|
+
__reExport(nextra_exports, require("./components/LocalizedPage"), module.exports);
|
|
20
|
+
__reExport(nextra_exports, require("./docsI18n"), module.exports);
|
|
21
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
22
|
+
0 && (module.exports = {
|
|
23
|
+
...require("./components/DocsLayout"),
|
|
24
|
+
...require("./components/LocalizedPage"),
|
|
25
|
+
...require("./docsI18n")
|
|
26
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { DocsLayout, DocsLayoutProps, NextraDocsLayout, NextraDocsLayoutProps } from './components/DocsLayout.cjs';
|
|
2
|
+
export { LocalizedPage, LocalizedPageProps, LocalizedPageVariant } from './components/LocalizedPage.cjs';
|
|
3
|
+
export { DocsI18nProvider, useDocsI18n } from './docsI18n.cjs';
|
|
4
|
+
import 'react';
|
|
5
|
+
import 'nextra';
|
|
6
|
+
import 'nextra-theme-docs';
|
|
7
|
+
import './docsLabels.cjs';
|
|
8
|
+
import './locale.cjs';
|
|
9
|
+
import './policies.cjs';
|
|
10
|
+
import './components/NavBar.cjs';
|
|
11
|
+
import './i18n.cjs';
|
package/dist/nextra.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { DocsLayout, DocsLayoutProps, NextraDocsLayout, NextraDocsLayoutProps } from './components/DocsLayout.js';
|
|
2
|
+
export { LocalizedPage, LocalizedPageProps, LocalizedPageVariant } from './components/LocalizedPage.js';
|
|
3
|
+
export { DocsI18nProvider, useDocsI18n } from './docsI18n.js';
|
|
4
|
+
import 'react';
|
|
5
|
+
import 'nextra';
|
|
6
|
+
import 'nextra-theme-docs';
|
|
7
|
+
import './docsLabels.js';
|
|
8
|
+
import './locale.js';
|
|
9
|
+
import './policies.js';
|
|
10
|
+
import './components/NavBar.js';
|
|
11
|
+
import './i18n.js';
|
package/dist/nextra.js
ADDED
package/dist/pageMap.cjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var pageMap_exports = {};
|
|
20
|
+
__export(pageMap_exports, {
|
|
21
|
+
localizePageMap: () => localizePageMap
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(pageMap_exports);
|
|
24
|
+
function localizeMetaValue(value, routeTitle, titles) {
|
|
25
|
+
if (typeof value === "string") return routeTitle ?? titles[value] ?? value;
|
|
26
|
+
if (!value || typeof value !== "object" || !("title" in value)) return value;
|
|
27
|
+
const title = value.title;
|
|
28
|
+
const translated = routeTitle ?? (typeof title === "string" ? titles[title] : void 0);
|
|
29
|
+
return translated ? { ...value, title: translated } : value;
|
|
30
|
+
}
|
|
31
|
+
function localizeItems(items, titles, routes, parent) {
|
|
32
|
+
const routeTitle = (name) => routes[parent ? `${parent}/${name}` : name] ?? routes[name];
|
|
33
|
+
return items.map((item) => {
|
|
34
|
+
if ("data" in item) {
|
|
35
|
+
const data = Object.fromEntries(
|
|
36
|
+
Object.entries(item.data).map(([key, value]) => [key, localizeMetaValue(value, routeTitle(key), titles)])
|
|
37
|
+
);
|
|
38
|
+
return { ...item, data };
|
|
39
|
+
}
|
|
40
|
+
const copy = { ...item };
|
|
41
|
+
const name = item.name;
|
|
42
|
+
const route = typeof name === "string" ? parent ? `${parent}/${name}` : name : parent;
|
|
43
|
+
const byRoute = typeof name === "string" ? routeTitle(name) : void 0;
|
|
44
|
+
const title = item.title;
|
|
45
|
+
if (byRoute) copy.title = byRoute;
|
|
46
|
+
else if (typeof title === "string" && titles[title]) copy.title = titles[title];
|
|
47
|
+
if ("frontMatter" in item && item.frontMatter) {
|
|
48
|
+
const frontTitle = item.frontMatter.title;
|
|
49
|
+
const translated = byRoute ?? (typeof frontTitle === "string" ? titles[frontTitle] : void 0);
|
|
50
|
+
if (translated) copy.frontMatter = { ...item.frontMatter, title: translated };
|
|
51
|
+
}
|
|
52
|
+
if ("children" in item && Array.isArray(item.children)) {
|
|
53
|
+
copy.children = localizeItems(item.children, titles, routes, route);
|
|
54
|
+
}
|
|
55
|
+
return copy;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function localizePageMap(pageMap, titles, options = {}) {
|
|
59
|
+
const routes = options.routes ?? {};
|
|
60
|
+
if (Object.keys(titles).length === 0 && Object.keys(routes).length === 0) return pageMap;
|
|
61
|
+
return localizeItems(pageMap, titles, routes);
|
|
62
|
+
}
|
|
63
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
64
|
+
0 && (module.exports = {
|
|
65
|
+
localizePageMap
|
|
66
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { PageMapItem } from 'nextra';
|
|
2
|
+
|
|
3
|
+
/** Options of {@link localizePageMap}. */
|
|
4
|
+
interface LocalizePageMapOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Translated titles by route instead of English title: `<folder>/<name>` (for example `guides/full-stack-react`),
|
|
7
|
+
* then the bare `<name>` (`guides`, `index`). A route title wins over an English title and also replaces the front
|
|
8
|
+
* matter `title`. The docs hub keys its sidebar this way.
|
|
9
|
+
*/
|
|
10
|
+
routes?: Readonly<Record<string, string>>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Translates the titles of a Nextra page map: the sidebar, the top-level menu, the breadcrumbs and the previous / next
|
|
14
|
+
* links all read them. The project sites match titles by their English text (the `_meta` titles, the `title` of each
|
|
15
|
+
* item and the front matter `title`), so a site lists each English title once, whatever the page; titles without a
|
|
16
|
+
* translation, such as package names, stay as they are. The docs hub matches them by route instead
|
|
17
|
+
* (`options.routes`). The page map is not modified: changed items are copied.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* localizePageMap(pageMap, { Introduction: 'Вступ', Packages: 'Пакети' });
|
|
22
|
+
* localizePageMap(pageMap, {}, { routes: { guides: 'Посібники', 'guides/starter-templates': 'Стартові шаблони' } });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @param pageMap - The page map of the site, the result of `getPageMap()` of `nextra/page-map`.
|
|
26
|
+
* @param titles - Translated titles by English title.
|
|
27
|
+
* @param options - See {@link LocalizePageMapOptions}.
|
|
28
|
+
* @returns The page map with translated titles, or `pageMap` itself when there is nothing to translate.
|
|
29
|
+
*/
|
|
30
|
+
declare function localizePageMap(pageMap: PageMapItem[], titles: Readonly<Record<string, string>>, options?: LocalizePageMapOptions): PageMapItem[];
|
|
31
|
+
|
|
32
|
+
export { localizePageMap };
|
|
33
|
+
export type { LocalizePageMapOptions };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { PageMapItem } from 'nextra';
|
|
2
|
+
|
|
3
|
+
/** Options of {@link localizePageMap}. */
|
|
4
|
+
interface LocalizePageMapOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Translated titles by route instead of English title: `<folder>/<name>` (for example `guides/full-stack-react`),
|
|
7
|
+
* then the bare `<name>` (`guides`, `index`). A route title wins over an English title and also replaces the front
|
|
8
|
+
* matter `title`. The docs hub keys its sidebar this way.
|
|
9
|
+
*/
|
|
10
|
+
routes?: Readonly<Record<string, string>>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Translates the titles of a Nextra page map: the sidebar, the top-level menu, the breadcrumbs and the previous / next
|
|
14
|
+
* links all read them. The project sites match titles by their English text (the `_meta` titles, the `title` of each
|
|
15
|
+
* item and the front matter `title`), so a site lists each English title once, whatever the page; titles without a
|
|
16
|
+
* translation, such as package names, stay as they are. The docs hub matches them by route instead
|
|
17
|
+
* (`options.routes`). The page map is not modified: changed items are copied.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* localizePageMap(pageMap, { Introduction: 'Вступ', Packages: 'Пакети' });
|
|
22
|
+
* localizePageMap(pageMap, {}, { routes: { guides: 'Посібники', 'guides/starter-templates': 'Стартові шаблони' } });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @param pageMap - The page map of the site, the result of `getPageMap()` of `nextra/page-map`.
|
|
26
|
+
* @param titles - Translated titles by English title.
|
|
27
|
+
* @param options - See {@link LocalizePageMapOptions}.
|
|
28
|
+
* @returns The page map with translated titles, or `pageMap` itself when there is nothing to translate.
|
|
29
|
+
*/
|
|
30
|
+
declare function localizePageMap(pageMap: PageMapItem[], titles: Readonly<Record<string, string>>, options?: LocalizePageMapOptions): PageMapItem[];
|
|
31
|
+
|
|
32
|
+
export { localizePageMap };
|
|
33
|
+
export type { LocalizePageMapOptions };
|
package/dist/pageMap.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
function localizeMetaValue(value, routeTitle, titles) {
|
|
2
|
+
if (typeof value === "string") return routeTitle ?? titles[value] ?? value;
|
|
3
|
+
if (!value || typeof value !== "object" || !("title" in value)) return value;
|
|
4
|
+
const title = value.title;
|
|
5
|
+
const translated = routeTitle ?? (typeof title === "string" ? titles[title] : void 0);
|
|
6
|
+
return translated ? { ...value, title: translated } : value;
|
|
7
|
+
}
|
|
8
|
+
function localizeItems(items, titles, routes, parent) {
|
|
9
|
+
const routeTitle = (name) => routes[parent ? `${parent}/${name}` : name] ?? routes[name];
|
|
10
|
+
return items.map((item) => {
|
|
11
|
+
if ("data" in item) {
|
|
12
|
+
const data = Object.fromEntries(
|
|
13
|
+
Object.entries(item.data).map(([key, value]) => [key, localizeMetaValue(value, routeTitle(key), titles)])
|
|
14
|
+
);
|
|
15
|
+
return { ...item, data };
|
|
16
|
+
}
|
|
17
|
+
const copy = { ...item };
|
|
18
|
+
const name = item.name;
|
|
19
|
+
const route = typeof name === "string" ? parent ? `${parent}/${name}` : name : parent;
|
|
20
|
+
const byRoute = typeof name === "string" ? routeTitle(name) : void 0;
|
|
21
|
+
const title = item.title;
|
|
22
|
+
if (byRoute) copy.title = byRoute;
|
|
23
|
+
else if (typeof title === "string" && titles[title]) copy.title = titles[title];
|
|
24
|
+
if ("frontMatter" in item && item.frontMatter) {
|
|
25
|
+
const frontTitle = item.frontMatter.title;
|
|
26
|
+
const translated = byRoute ?? (typeof frontTitle === "string" ? titles[frontTitle] : void 0);
|
|
27
|
+
if (translated) copy.frontMatter = { ...item.frontMatter, title: translated };
|
|
28
|
+
}
|
|
29
|
+
if ("children" in item && Array.isArray(item.children)) {
|
|
30
|
+
copy.children = localizeItems(item.children, titles, routes, route);
|
|
31
|
+
}
|
|
32
|
+
return copy;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function localizePageMap(pageMap, titles, options = {}) {
|
|
36
|
+
const routes = options.routes ?? {};
|
|
37
|
+
if (Object.keys(titles).length === 0 && Object.keys(routes).length === 0) return pageMap;
|
|
38
|
+
return localizeItems(pageMap, titles, routes);
|
|
39
|
+
}
|
|
40
|
+
export {
|
|
41
|
+
localizePageMap
|
|
42
|
+
};
|