@redocly/theme 0.52.0-next.4 → 0.52.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/lib/components/Navbar/NavbarItem.js +2 -2
- package/lib/core/hooks/use-language-picker.js +1 -1
- package/lib/core/utils/menu.js +2 -3
- package/lib/core/utils/urls.d.ts +9 -2
- package/lib/core/utils/urls.js +11 -5
- package/package.json +2 -2
- package/src/components/Navbar/NavbarItem.tsx +2 -1
- package/src/core/hooks/use-language-picker.ts +2 -2
- package/src/core/utils/menu.ts +7 -3
- package/src/core/utils/urls.ts +12 -8
|
@@ -46,7 +46,7 @@ function NavbarItem({ navItem, className }) {
|
|
|
46
46
|
if (navItem.type !== 'link' && !navItem.items)
|
|
47
47
|
return null;
|
|
48
48
|
const item = navItem;
|
|
49
|
-
const normalizedPath = item.link && item.link !== '/' ? (0, utils_1.removeTrailingSlash)(item.link) : item.link;
|
|
49
|
+
const normalizedPath = (item.link && item.link !== '/' ? (0, utils_1.removeTrailingSlash)(item.link) : item.link) || '';
|
|
50
50
|
const isActive = pathname ===
|
|
51
51
|
(0, utils_1.withPathPrefix)((0, utils_1.getPathnameForLocale)(normalizedPath, defaultLocale, currentLocale, locales));
|
|
52
52
|
const itemContent = (react_1.default.createElement(NavbarMenuItem, { as: item.link ? Link_1.Link : undefined, active: isActive, className: className, onClick: () => telemetry.send('navbar_menu_item_clicked', { type: item.type }), external: item.external, target: item.target, to: item.link },
|
|
@@ -59,7 +59,7 @@ function NavbarItem({ navItem, className }) {
|
|
|
59
59
|
const groupItemsComponents = groupItems.map((item, index) => {
|
|
60
60
|
if (item.type !== 'link' && item.type !== 'separator')
|
|
61
61
|
return null;
|
|
62
|
-
return (react_1.default.createElement(DropdownMenuItem_1.DropdownMenuItem, { key: `${item.label}_${index}`, to: item.link, separator: item.type === 'separator', separatorLine: item.separatorLine, "data-translation-key": item.labelTranslationKey },
|
|
62
|
+
return (react_1.default.createElement(DropdownMenuItem_1.DropdownMenuItem, { key: `${item.label}_${index}`, to: item.link, separator: item.type === 'separator', separatorLine: item.separatorLine, "data-translation-key": item.labelTranslationKey, external: item.external },
|
|
63
63
|
react_1.default.createElement(NavbarIcon, { url: item.icon }),
|
|
64
64
|
react_1.default.createElement(NavbarLabel, { "data-translation-key": item.labelTranslationKey }, translate(item.labelTranslationKey, item.label)),
|
|
65
65
|
item.external ? react_1.default.createElement(ExternalLinkIcon, { size: "10px" }) : null));
|
|
@@ -11,7 +11,7 @@ function useLanguagePicker() {
|
|
|
11
11
|
const { currentLocale, locales, defaultLocale } = useL10nConfig();
|
|
12
12
|
const locale = locales.find((l) => l.code === currentLocale);
|
|
13
13
|
function setLocale(value) {
|
|
14
|
-
const newLangPathname = (0, utils_1.withPathPrefix)((0, utils_1.getPathnameForLocale)(location.pathname, defaultLocale, value, locales));
|
|
14
|
+
const newLangPathname = (0, utils_1.withPathPrefix)((0, utils_1.getPathnameForLocale)((0, utils_1.withoutPathPrefix)(location.pathname), defaultLocale, value, locales));
|
|
15
15
|
const newUrlWithLanguage = `${newLangPathname}${location.search}${location.hash}`;
|
|
16
16
|
loadAndNavigate({ navigate, to: newUrlWithLanguage });
|
|
17
17
|
}
|
package/lib/core/utils/menu.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildVersionSection = exports.buildLanguagesGroup = exports.isItemActive = exports.mapNavbarItems = void 0;
|
|
4
4
|
exports.getMenuItemType = getMenuItemType;
|
|
5
|
-
const urls_1 = require("../../core/utils/urls");
|
|
6
5
|
const utils_1 = require("../../core/utils");
|
|
7
6
|
const constants_1 = require("../../core/constants");
|
|
8
7
|
const TRANSLATION_KEYS = {
|
|
@@ -23,7 +22,7 @@ const isItemActive = (item, defaultLocale, currentLocale, locales, location) =>
|
|
|
23
22
|
const link = item.languageInsensitive
|
|
24
23
|
? item.link || ''
|
|
25
24
|
: (0, utils_1.getPathnameForLocale)(item.link || '/', defaultLocale, currentLocale, locales);
|
|
26
|
-
return pathHash === (0,
|
|
25
|
+
return pathHash === (0, utils_1.withPathPrefix)(link);
|
|
27
26
|
};
|
|
28
27
|
exports.isItemActive = isItemActive;
|
|
29
28
|
const buildLanguagesGroup = (locales, defaultLocale, currentLocale) => {
|
|
@@ -37,7 +36,7 @@ const buildLanguagesGroup = (locales, defaultLocale, currentLocale) => {
|
|
|
37
36
|
items: locales
|
|
38
37
|
.filter((locale) => locale.code !== currentLocale)
|
|
39
38
|
.map((locale) => {
|
|
40
|
-
const newLangPathname = (0,
|
|
39
|
+
const newLangPathname = (0, utils_1.withPathPrefix)((0, utils_1.getPathnameForLocale)((0, utils_1.withoutPathPrefix)(location.pathname), defaultLocale, locale.code, locales));
|
|
41
40
|
const newUrlWithLanguage = `${newLangPathname}${location.search}${location.hash}`;
|
|
42
41
|
return {
|
|
43
42
|
type: 'link',
|
package/lib/core/utils/urls.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { Locale } from '../../core/types';
|
|
2
|
-
export declare function combineUrls(baseURL: string, ...relativeURLs: string[]): string;
|
|
2
|
+
export declare function combineUrls(baseURL: string, ...relativeURLs: (string | undefined)[]): string;
|
|
3
3
|
export declare function withPathPrefix(url: string): string;
|
|
4
4
|
export declare function withoutPathPrefix(pathname: string): string;
|
|
5
5
|
export declare function withoutHash(url: undefined | null): undefined;
|
|
6
6
|
export declare function withoutHash(url: string): string;
|
|
7
7
|
export declare function withoutHash(url: string | undefined): string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @returns url with leading and without trailing slash or empty string, e.g. '/prefix'
|
|
11
|
+
*/
|
|
8
12
|
export declare function getPathPrefix(): string;
|
|
9
13
|
export declare function normalizePathPrefix(prefix: string): string;
|
|
10
14
|
export declare function addLeadingSlash(url: string): string;
|
|
@@ -21,7 +25,10 @@ export declare function slash(path: string): string;
|
|
|
21
25
|
export declare function addTrailingSlash(url: string): string;
|
|
22
26
|
/**
|
|
23
27
|
* Adds locale to pathname, or replaces current locale in pathname with a new one
|
|
24
|
-
*
|
|
28
|
+
* @param originalPathname - Pathname without api prefix
|
|
29
|
+
* @param defaultLocale - Default locale code
|
|
30
|
+
* @param newLocale - New locale code to apply
|
|
31
|
+
* @param allLocales - Array of all available locales
|
|
25
32
|
*/
|
|
26
33
|
export declare function getPathnameForLocale(originalPathname: string, defaultLocale: string, newLocale: string, allLocales: Locale[]): string;
|
|
27
34
|
export declare function getLocaleFromPathname(pathname: string, defaultLocale?: string, allLocales?: Locale[]): string;
|
package/lib/core/utils/urls.js
CHANGED
|
@@ -38,6 +38,10 @@ function withoutHash(url) {
|
|
|
38
38
|
return undefined;
|
|
39
39
|
return url.split('#')[0];
|
|
40
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* @returns url with leading and without trailing slash or empty string, e.g. '/prefix'
|
|
44
|
+
*/
|
|
41
45
|
function getPathPrefix() {
|
|
42
46
|
if (process.env.REDOCLY_PREFIX_PATHS) {
|
|
43
47
|
return normalizePathPrefix(process.env.REDOCLY_PREFIX_PATHS);
|
|
@@ -80,17 +84,19 @@ function addTrailingSlash(url) {
|
|
|
80
84
|
}
|
|
81
85
|
/**
|
|
82
86
|
* Adds locale to pathname, or replaces current locale in pathname with a new one
|
|
83
|
-
*
|
|
87
|
+
* @param originalPathname - Pathname without api prefix
|
|
88
|
+
* @param defaultLocale - Default locale code
|
|
89
|
+
* @param newLocale - New locale code to apply
|
|
90
|
+
* @param allLocales - Array of all available locales
|
|
84
91
|
*/
|
|
85
92
|
function getPathnameForLocale(originalPathname, defaultLocale, newLocale, allLocales) {
|
|
86
|
-
const pathnameWithoutPrefix = withoutPathPrefix(originalPathname);
|
|
87
93
|
const currentLocale = getLocaleFromPathname(originalPathname, defaultLocale, allLocales);
|
|
88
94
|
if (currentLocale === newLocale) {
|
|
89
|
-
return
|
|
95
|
+
return originalPathname;
|
|
90
96
|
}
|
|
91
97
|
const pathnameWithoutLocale = currentLocale === defaultLocale
|
|
92
|
-
?
|
|
93
|
-
:
|
|
98
|
+
? originalPathname
|
|
99
|
+
: originalPathname.slice(currentLocale.length + 1);
|
|
94
100
|
const newLocalePrefix = newLocale === defaultLocale ? '' : '/' + newLocale;
|
|
95
101
|
return `${newLocalePrefix.toLowerCase()}${pathnameWithoutLocale}`;
|
|
96
102
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@redocly/theme",
|
|
3
|
-
"version": "0.52.0
|
|
3
|
+
"version": "0.52.0",
|
|
4
4
|
"description": "Shared UI components lib",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"theme",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"react-calendar": "4.2.1",
|
|
89
89
|
"react-date-picker": "10.0.3",
|
|
90
90
|
"timeago.js": "4.0.2",
|
|
91
|
-
"@redocly/config": "0.
|
|
91
|
+
"@redocly/config": "0.23.0"
|
|
92
92
|
},
|
|
93
93
|
"scripts": {
|
|
94
94
|
"watch": "tsc -p tsconfig.build.json && (concurrently \"tsc -w -p tsconfig.build.json\" \"tsc-alias -w -p tsconfig.build.json\")",
|
|
@@ -33,7 +33,7 @@ export function NavbarItem({ navItem, className }: NavbarItemProps): JSX.Element
|
|
|
33
33
|
|
|
34
34
|
const item = navItem as ResolvedNavLinkItem;
|
|
35
35
|
const normalizedPath =
|
|
36
|
-
item.link && item.link !== '/' ? removeTrailingSlash(item.link) : item.link;
|
|
36
|
+
(item.link && item.link !== '/' ? removeTrailingSlash(item.link) : item.link) || '';
|
|
37
37
|
|
|
38
38
|
const isActive =
|
|
39
39
|
pathname ===
|
|
@@ -69,6 +69,7 @@ export function NavbarItem({ navItem, className }: NavbarItemProps): JSX.Element
|
|
|
69
69
|
separator={item.type === 'separator'}
|
|
70
70
|
separatorLine={item.separatorLine}
|
|
71
71
|
data-translation-key={item.labelTranslationKey}
|
|
72
|
+
external={item.external}
|
|
72
73
|
>
|
|
73
74
|
<NavbarIcon url={item.icon} />
|
|
74
75
|
<NavbarLabel data-translation-key={item.labelTranslationKey}>
|
|
@@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom';
|
|
|
3
3
|
import type { L10nConfig } from '@redocly/theme/core/types';
|
|
4
4
|
|
|
5
5
|
import { useThemeHooks } from '@redocly/theme/core/hooks';
|
|
6
|
-
import { getPathnameForLocale, withPathPrefix } from '@redocly/theme/core/utils';
|
|
6
|
+
import { getPathnameForLocale, withPathPrefix, withoutPathPrefix } from '@redocly/theme/core/utils';
|
|
7
7
|
|
|
8
8
|
export function useLanguagePicker(): {
|
|
9
9
|
currentLocale: L10nConfig['locales'][number] | undefined;
|
|
@@ -19,7 +19,7 @@ export function useLanguagePicker(): {
|
|
|
19
19
|
|
|
20
20
|
function setLocale(value: string) {
|
|
21
21
|
const newLangPathname = withPathPrefix(
|
|
22
|
-
getPathnameForLocale(location.pathname, defaultLocale, value, locales),
|
|
22
|
+
getPathnameForLocale(withoutPathPrefix(location.pathname), defaultLocale, value, locales),
|
|
23
23
|
);
|
|
24
24
|
|
|
25
25
|
const newUrlWithLanguage = `${newLangPathname}${location.search}${location.hash}`;
|
package/src/core/utils/menu.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { ResolvedNavItem, Version } from '@redocly/config';
|
|
2
2
|
import type { Location } from 'react-router-dom';
|
|
3
3
|
|
|
4
|
-
import { withPathPrefix } from '@redocly/theme/core/utils
|
|
5
|
-
import { getPathnameForLocale } from '@redocly/theme/core/utils';
|
|
4
|
+
import { getPathnameForLocale, withoutPathPrefix, withPathPrefix } from '@redocly/theme/core/utils';
|
|
6
5
|
import { type Locale, type TFunction, type ItemState } from '@redocly/theme/core/types';
|
|
7
6
|
import { MenuItemType } from '@redocly/theme/core/constants';
|
|
8
7
|
|
|
@@ -66,7 +65,12 @@ export const buildLanguagesGroup = (
|
|
|
66
65
|
.filter((locale) => locale.code !== currentLocale)
|
|
67
66
|
.map((locale) => {
|
|
68
67
|
const newLangPathname = withPathPrefix(
|
|
69
|
-
getPathnameForLocale(
|
|
68
|
+
getPathnameForLocale(
|
|
69
|
+
withoutPathPrefix(location.pathname),
|
|
70
|
+
defaultLocale,
|
|
71
|
+
locale.code,
|
|
72
|
+
locales,
|
|
73
|
+
),
|
|
70
74
|
);
|
|
71
75
|
const newUrlWithLanguage = `${newLangPathname}${location.search}${location.hash}`;
|
|
72
76
|
return {
|
package/src/core/utils/urls.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { Locale } from '@redocly/theme/core/types';
|
|
|
2
2
|
|
|
3
3
|
import { DEFAULT_LOCALE_PLACEHOLDER } from '@redocly/theme/core/constants';
|
|
4
4
|
|
|
5
|
-
export function combineUrls(baseURL: string, ...relativeURLs: string[]) {
|
|
5
|
+
export function combineUrls(baseURL: string, ...relativeURLs: (string | undefined)[]) {
|
|
6
6
|
let res = baseURL;
|
|
7
7
|
for (let relativeURL of relativeURLs) {
|
|
8
8
|
res = relativeURL
|
|
@@ -32,6 +32,10 @@ export function withoutHash(url: string | undefined | null): string | undefined
|
|
|
32
32
|
return url.split('#')[0];
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* @returns url with leading and without trailing slash or empty string, e.g. '/prefix'
|
|
38
|
+
*/
|
|
35
39
|
export function getPathPrefix() {
|
|
36
40
|
if (process.env.REDOCLY_PREFIX_PATHS) {
|
|
37
41
|
return normalizePathPrefix(process.env.REDOCLY_PREFIX_PATHS);
|
|
@@ -52,7 +56,6 @@ export function addLeadingSlash(url: string): string {
|
|
|
52
56
|
export function removeTrailingSlash(url: string): string {
|
|
53
57
|
return url.endsWith('/') ? url.substring(0, url.length - 1) : url;
|
|
54
58
|
}
|
|
55
|
-
|
|
56
59
|
export function removeLeadingSlash(url: string): string {
|
|
57
60
|
return url.startsWith('/') ? url.substring(1) : url;
|
|
58
61
|
}
|
|
@@ -85,7 +88,10 @@ export function addTrailingSlash(url: string): string {
|
|
|
85
88
|
|
|
86
89
|
/**
|
|
87
90
|
* Adds locale to pathname, or replaces current locale in pathname with a new one
|
|
88
|
-
*
|
|
91
|
+
* @param originalPathname - Pathname without api prefix
|
|
92
|
+
* @param defaultLocale - Default locale code
|
|
93
|
+
* @param newLocale - New locale code to apply
|
|
94
|
+
* @param allLocales - Array of all available locales
|
|
89
95
|
*/
|
|
90
96
|
export function getPathnameForLocale(
|
|
91
97
|
originalPathname: string,
|
|
@@ -93,18 +99,16 @@ export function getPathnameForLocale(
|
|
|
93
99
|
newLocale: string,
|
|
94
100
|
allLocales: Locale[],
|
|
95
101
|
) {
|
|
96
|
-
const pathnameWithoutPrefix = withoutPathPrefix(originalPathname);
|
|
97
|
-
|
|
98
102
|
const currentLocale = getLocaleFromPathname(originalPathname, defaultLocale, allLocales);
|
|
99
103
|
|
|
100
104
|
if (currentLocale === newLocale) {
|
|
101
|
-
return
|
|
105
|
+
return originalPathname;
|
|
102
106
|
}
|
|
103
107
|
|
|
104
108
|
const pathnameWithoutLocale =
|
|
105
109
|
currentLocale === defaultLocale
|
|
106
|
-
?
|
|
107
|
-
:
|
|
110
|
+
? originalPathname
|
|
111
|
+
: originalPathname.slice(currentLocale.length + 1);
|
|
108
112
|
|
|
109
113
|
const newLocalePrefix = newLocale === defaultLocale ? '' : '/' + newLocale;
|
|
110
114
|
|