From 3097ec39e167fb9df9cb45505a745d0deaf3f967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Tue, 16 May 2023 08:42:18 +0800 Subject: [PATCH] fix: Site link auto switch by locale (#42381) * Revert "Revert "docs: auto trans link" (#42373)" This reverts commit fa90b25b3efb268a8b64ce02f49e6d85edd11576. * docs: site update link auto switch logic --- .dumi/rehypeAntd.ts | 6 ++- .dumi/theme/builtins/LocaleLink/index.tsx | 49 +++++++++++++++++++++++ .prettierrc | 1 + 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 .dumi/theme/builtins/LocaleLink/index.tsx diff --git a/.dumi/rehypeAntd.ts b/.dumi/rehypeAntd.ts index 89ae696ba2..9db6f65717 100644 --- a/.dumi/rehypeAntd.ts +++ b/.dumi/rehypeAntd.ts @@ -1,5 +1,5 @@ import assert from 'assert'; -import { type HastRoot, type UnifiedTransformer, unistUtilVisit } from 'dumi'; +import { unistUtilVisit, type HastRoot, type UnifiedTransformer } from 'dumi'; /** * plugin for modify hast tree when docs compiling @@ -60,6 +60,10 @@ function rehypeAntd(): UnifiedTransformer { if (!node.properties) return; node.properties.className ??= []; (node.properties.className as string[]).push('component-api-table'); + } else if (node.type === 'element' && (node.tagName === 'Link' || node.tagName === 'a')) { + const { tagName } = node; + node.properties.sourceType = tagName; + node.tagName = 'LocaleLink'; } }); }; diff --git a/.dumi/theme/builtins/LocaleLink/index.tsx b/.dumi/theme/builtins/LocaleLink/index.tsx new file mode 100644 index 0000000000..67b7e49e44 --- /dev/null +++ b/.dumi/theme/builtins/LocaleLink/index.tsx @@ -0,0 +1,49 @@ +import { Link } from 'dumi'; +import * as React from 'react'; +import useLocale from '../../../hooks/useLocale'; + +type LinkProps = Parameters[0]; + +export interface LocaleLinkProps extends LinkProps { + sourceType: 'a' | 'Link'; + children?: React.ReactNode; +} + +export default function LocaleLink({ sourceType, to, ...props }: LocaleLinkProps) { + const Component = sourceType === 'a' ? 'a' : Link; + + const [, localeType] = useLocale(); + + const localeTo = React.useMemo(() => { + if (!to || typeof to !== 'string') { + return to; + } + + // Auto locale switch + const cells = to.match(/(\/[^#]*)(#.*)?/); + if (cells) { + let path = cells[1].replace(/\/$/, ''); + const hash = cells[2] || ''; + + if (localeType === 'cn' && !path.endsWith('-cn')) { + path = `${path}-cn`; + } else if (localeType === 'en' && path.endsWith('-cn')) { + path = path.replace(/-cn$/, ''); + } + + return `${path}${hash}`; + } + + return to; + }, [to]); + + const linkProps: LocaleLinkProps = { + ...props, + } as LocaleLinkProps; + + if (to) { + linkProps.to = localeTo; + } + + return ; +} diff --git a/.prettierrc b/.prettierrc index 84d393d19b..9d5b9bf640 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,6 @@ { "singleQuote": true, + "jsxSingleQuote": false, "trailingComma": "all", "printWidth": 100, "proseWrap": "never",