Skip to content

Commit 785be80

Browse files
authored
refactor: normalizeThemeConfig code style (#2481)
1 parent 1e456e1 commit 785be80

File tree

4 files changed

+56
-67
lines changed

4 files changed

+56
-67
lines changed

packages/core/src/node/runtimeModule/i18n.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import { RuntimeModuleID, type VirtualModulePlugin } from './types';
77
const require = createRequire(import.meta.url);
88
const DEFAULT_I18N_SOURCE = join(process.cwd(), 'i18n.json');
99

10-
export function getI18nData(docConfig: UserConfig) {
10+
export function getI18nData(
11+
docConfig: UserConfig,
12+
): Record<string, Record<string, string>> {
1113
const { i18nSourcePath = DEFAULT_I18N_SOURCE } = docConfig;
1214
try {
1315
// require.cache is an API in Rslib.

packages/core/src/node/runtimeModule/siteData/normalizeThemeConfig.ts

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type NavItemWithLink,
77
type NormalizedDefaultThemeConfig,
88
type NormalizedSidebarGroup,
9+
normalizeHref,
910
type Sidebar,
1011
type SidebarDivider,
1112
type SidebarGroup,
@@ -33,7 +34,7 @@ export function normalizeThemeConfig(
3334
const locales = siteLocales ?? (themeConfig?.locales || []);
3435
const i18nTextData = getI18nData(docConfig);
3536
// In following code, we will normalize the theme config reference to the pages data extracted from mdx files
36-
const normalizeLinkPrefix = (link = '', currentLang = '') => {
37+
const normalizeLinkPrefix = (link: string, currentLang: string) => {
3738
const normalizedLink = slash(link);
3839
if (
3940
!currentLang ||
@@ -51,14 +52,24 @@ export function normalizeThemeConfig(
5152
: `/${currentLang}${addLeadingSlash(normalizedLink)}`;
5253
};
5354

54-
const getI18nText = (key = '', currentLang = '') => {
55+
const getI18nText = (key: string, currentLang: string) => {
5556
const text = i18nTextData[key]?.[currentLang];
5657
return text || key;
5758
};
59+
60+
const cleanUrls = docConfig.route?.cleanUrls ?? false;
61+
const transformLink = (link: string, currentLang: string) => {
62+
return normalizeHref(normalizeLinkPrefix(link, currentLang), cleanUrls);
63+
};
64+
65+
const textReplace = (text: string, currentLang: string) => {
66+
return applyReplaceRules(getI18nText(text, currentLang), replaceRules);
67+
};
68+
5869
// Normalize sidebar
5970
const normalizeSidebar = (
60-
sidebar?: DefaultThemeConfig['sidebar'],
61-
currentLang = '',
71+
sidebar: DefaultThemeConfig['sidebar'],
72+
currentLang: string,
6273
): NormalizedDefaultThemeConfig['sidebar'] => {
6374
const normalizedSidebar: NormalizedDefaultThemeConfig['sidebar'] = {};
6475
if (!sidebar) {
@@ -78,24 +89,22 @@ export function normalizeThemeConfig(
7889

7990
// Meet the section header, return i18n text
8091
if (typeof item === 'object' && 'sectionHeaderText' in item) {
81-
item.sectionHeaderText = applyReplaceRules(
82-
getI18nText(item.sectionHeaderText, currentLang),
83-
replaceRules,
92+
item.sectionHeaderText = textReplace(
93+
item.sectionHeaderText,
94+
currentLang,
8495
);
8596
return item;
8697
}
8798

8899
if (typeof item === 'object' && 'items' in item) {
89100
return {
90101
...item,
91-
text: applyReplaceRules(
92-
getI18nText(item.text, currentLang),
93-
replaceRules,
94-
),
95-
link: normalizeLinkPrefix(item.link),
102+
text: textReplace(item.text, currentLang),
103+
...('link' in item && item.link
104+
? { link: transformLink(item.link, currentLang) }
105+
: {}),
96106
collapsed: item.collapsed ?? false,
97107
collapsible: item.collapsible ?? true,
98-
tag: item.tag,
99108
items: item.items.map(subItem => {
100109
return normalizeSidebarItem(subItem) as
101110
| NormalizedSidebarGroup
@@ -106,12 +115,8 @@ export function normalizeThemeConfig(
106115

107116
return {
108117
...item,
109-
text: applyReplaceRules(
110-
getI18nText(item.text, currentLang),
111-
replaceRules,
112-
),
113-
link: normalizeLinkPrefix(item.link),
114-
tag: item.tag,
118+
text: textReplace(item.text, currentLang),
119+
link: transformLink(item.link, currentLang),
115120
};
116121
};
117122

@@ -131,43 +136,29 @@ export function normalizeThemeConfig(
131136
};
132137

133138
const normalizeNav = (
134-
nav?: DefaultThemeConfig['nav'],
135-
currentLang?: string,
139+
nav: DefaultThemeConfig['nav'] | undefined,
140+
currentLang: string,
136141
) => {
137142
if (!nav) {
138143
return [];
139144
}
140-
const transformNavItem = (navItem: NavItem): NavItem => {
141-
const text = applyReplaceRules(
142-
getI18nText(navItem.text, currentLang),
143-
replaceRules,
144-
);
145-
if ('link' in navItem) {
146-
return {
147-
...navItem,
148-
text,
149-
link: normalizeLinkPrefix(navItem.link, currentLang),
150-
};
151-
}
152-
153-
if ('items' in navItem) {
154-
return {
155-
...navItem,
156-
text,
157-
items: navItem.items.map((item: NavItemWithLink) => {
158-
return {
159-
...item,
160-
text: applyReplaceRules(
161-
getI18nText(item.text, currentLang),
162-
replaceRules,
163-
),
164-
link: normalizeLinkPrefix(item.link, currentLang),
165-
};
166-
}),
167-
};
168-
}
169-
170-
return navItem;
145+
const transformNavItem = <T extends NavItem>(navItem: T): T => {
146+
return {
147+
...navItem,
148+
...(navItem.text
149+
? { text: textReplace(navItem.text, currentLang) }
150+
: {}),
151+
...('link' in navItem
152+
? { link: transformLink(navItem.link, currentLang) }
153+
: {}),
154+
...('items' in navItem
155+
? {
156+
items: navItem.items.map((item: NavItemWithLink) => {
157+
return transformNavItem(item);
158+
}),
159+
}
160+
: {}),
161+
};
171162
};
172163

173164
if (Array.isArray(nav)) {
@@ -200,7 +191,7 @@ export function normalizeThemeConfig(
200191
return {
201192
lang: currentLang,
202193
label,
203-
...(localeInThemeConfig || {}),
194+
...localeInThemeConfig,
204195
sidebar: normalizeSidebar(
205196
localeInThemeConfig?.sidebar ?? themeConfig.sidebar,
206197
currentLang,
@@ -212,8 +203,8 @@ export function normalizeThemeConfig(
212203
};
213204
});
214205
} else {
215-
themeConfig.sidebar = normalizeSidebar(themeConfig?.sidebar);
216-
themeConfig.nav = normalizeNav(themeConfig?.nav);
206+
themeConfig.sidebar = normalizeSidebar(themeConfig?.sidebar, '');
207+
themeConfig.nav = normalizeNav(themeConfig?.nav, '');
217208
}
218209
return themeConfig as NormalizedDefaultThemeConfig;
219210
}

packages/theme-default/src/components/Overview/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ export function Overview(props: {
138138
}, []);
139139

140140
const subFilter = (link: string) =>
141-
// sidebar items link without base path
142-
// pages route path with base path
143141
link.startsWith(routePath.replace(/overview$/, '')) &&
144142
!isEqualPath(link, routePath);
145143
const getChildLink = (

packages/theme-default/src/components/Overview/utils.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import type {
2-
NormalizedSidebarGroup,
3-
SidebarDivider,
4-
SidebarItem,
5-
SidebarSectionHeader,
1+
import {
2+
type NormalizedSidebarGroup,
3+
normalizeHref,
4+
type SidebarDivider,
5+
type SidebarItem,
6+
type SidebarSectionHeader,
67
} from '@rspress/shared';
78
import { isSidebarDivider, isSidebarSectionHeader } from '../Sidebar/utils';
89

910
function removeIndex(link: string) {
10-
if (link.endsWith('/index')) {
11-
return link.slice(0, -5);
12-
}
13-
return link;
11+
return normalizeHref(link, true);
1412
}
1513

1614
/**

0 commit comments

Comments
 (0)