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}
0 commit comments