Browse Source

docs: auto trans link (#42311)

* docs: auto trans link

* chore: force CI
pull/42323/head
二货爱吃白萝卜 2 years ago
committed by GitHub
parent
commit
e28c9237d6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      .dumi/rehypeAntd.ts
  2. 41
      .dumi/theme/builtins/LocaleLink/index.tsx
  3. 2
      package.json

5
.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,9 @@ function rehypeAntd(): UnifiedTransformer<HastRoot> {
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')) {
node.tagName = 'LocaleLink';
node.properties.sourceType = node.tagName;
}
});
};

41
.dumi/theme/builtins/LocaleLink/index.tsx

@ -0,0 +1,41 @@
import { Link } from 'dumi';
import * as React from 'react';
import useLocale from '../../../hooks/useLocale';
type LinkProps = Parameters<typeof Link>[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]);
return <Component to={localeTo} {...props} />;
}

2
package.json

@ -157,7 +157,7 @@
},
"devDependencies": {
"@ant-design/tools": "^17.0.0",
"@antv/g6": "^4.8.5",
"@antv/g6": "^4.8.13",
"@argos-ci/core": "^0.8.0",
"@babel/eslint-plugin": "^7.19.1",
"@dnd-kit/core": "^6.0.7",

Loading…
Cancel
Save