From bd6ee2535628fa6213d6c6812c9070fac3bb68a1 Mon Sep 17 00:00:00 2001 From: shuai Date: Mon, 6 Jan 2025 14:08:18 +0800 Subject: [PATCH 1/3] fix: navbar support locales nav --- docusaurus.config.js | 6 ++++++ src/theme/NavbarItem/NavbarNavLink.tsx | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index 7e47091117..9dec0d5a0d 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -109,6 +109,9 @@ const config = { { label: 'Plugins', to: '/plugins', + tos: { + 'zh-CN': 'https://test.com' + } }, { label: 'Blog', @@ -130,6 +133,9 @@ const config = { { label: 'Q&A Community', to: 'https://meta.answer.dev', + tos: { + 'zh-CN': 'https://baidu.com', + } }, { label: 'Team', diff --git a/src/theme/NavbarItem/NavbarNavLink.tsx b/src/theme/NavbarItem/NavbarNavLink.tsx index 8e22a938f0..20b08c1be9 100644 --- a/src/theme/NavbarItem/NavbarNavLink.tsx +++ b/src/theme/NavbarItem/NavbarNavLink.tsx @@ -12,10 +12,16 @@ import isInternalUrl from '@docusaurus/isInternalUrl'; import {isRegexpStringMatch} from '@docusaurus/theme-common'; import IconExternalLink from '@theme/Icon/ExternalLink'; import type {Props} from '@theme/NavbarItem/NavbarNavLink'; +import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; + +interface SProps extends Props { + tos: Record +} export default function NavbarNavLink({ activeBasePath, activeBaseRegex, + tos, to, href, label, @@ -23,10 +29,13 @@ export default function NavbarNavLink({ isDropdownLink, prependBaseUrlToHref, ...props -}: Props): JSX.Element { +}: SProps): JSX.Element { // TODO all this seems hacky // {to: 'version'} should probably be forbidden, in favor of {to: '/version'} - const toUrl = useBaseUrl(to); + const { i18n: { currentLocale } } = useDocusaurusContext(); + // If to is a string, we assume it's a path that needs localization + const aliasTo = tos?.[currentLocale] || to; + const toUrl = useBaseUrl(aliasTo); const activeBaseUrl = useBaseUrl(activeBasePath); const normalizedHref = useBaseUrl(href, {forcePrependBaseUrl: true}); const isExternalLink = label && href && !isInternalUrl(href); From 43547eda6b06ba1f0e3bbbf411b566d6b4c9413e Mon Sep 17 00:00:00 2001 From: shuai Date: Mon, 6 Jan 2025 14:12:51 +0800 Subject: [PATCH 2/3] fix: delete test data --- docusaurus.config.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index 9dec0d5a0d..7e47091117 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -109,9 +109,6 @@ const config = { { label: 'Plugins', to: '/plugins', - tos: { - 'zh-CN': 'https://test.com' - } }, { label: 'Blog', @@ -133,9 +130,6 @@ const config = { { label: 'Q&A Community', to: 'https://meta.answer.dev', - tos: { - 'zh-CN': 'https://baidu.com', - } }, { label: 'Team', From a69b9ccc8f0f556f568f42e2613842a56d7eaf0d Mon Sep 17 00:00:00 2001 From: shuai Date: Mon, 13 Jan 2025 14:26:35 +0800 Subject: [PATCH 3/3] feat: default matches browser language #285 --- plugins/blog-plugin.js | 3 - .../blog/BlogCategoryPostsPage/index.tsx | 1 - src/components/blog/BlogDetailPage/index.tsx | 1 - src/components/blog/BlogHome/index.tsx | 1 - src/theme/DocSidebarItem/Category/index.tsx | 3 - src/theme/Layout/BrowserLanguage.tsx | 57 +++++++++++++++++++ src/theme/Layout/index.tsx | 3 +- src/theme/NavbarItem/NavbarNavLink.tsx | 8 +++ 8 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 src/theme/Layout/BrowserLanguage.tsx diff --git a/plugins/blog-plugin.js b/plugins/blog-plugin.js index fcd05dd90b..59689bc830 100644 --- a/plugins/blog-plugin.js +++ b/plugins/blog-plugin.js @@ -149,8 +149,6 @@ async function blogPluginExtended(...pluginArgs) { const { createData, addRoute } = actions; - // console.log('blogPosts', blogPosts[0]); - const removeDraftPosts = allBlogPosts.filter((post) => !post.metadata.frontMatter?.draft); // Fecommend posts list @@ -264,7 +262,6 @@ async function blogPluginExtended(...pluginArgs) { permalink: generateCategoryPath(category.label), }; - // console.log('categoryProp', categoryProp); const categoryPropPath = await createData( `${utils.docuHash(categoryProp.label)}.json`, JSON.stringify(categoryProp, null, 2), diff --git a/src/components/blog/BlogCategoryPostsPage/index.tsx b/src/components/blog/BlogCategoryPostsPage/index.tsx index 0c312f3515..3b8ab793cd 100644 --- a/src/components/blog/BlogCategoryPostsPage/index.tsx +++ b/src/components/blog/BlogCategoryPostsPage/index.tsx @@ -21,7 +21,6 @@ interface IProps extends Props { } const Index = (props: IProps) => { const { category, categoriyList, items, listMetadata, } = props - // console.log('...props', props); return ( ['onClick']; }) { - // console.log('categoryLabel', categoryLabel); return (