@umijs/plugin-docs 4.0.0-beta.18 → 4.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ import { useThemeContext } from './context';
2
+
3
+ interface useLanguageResult {
4
+ isFromPath: boolean;
5
+ currentLanguage: { locale: string; text: string } | undefined;
6
+ languages: { locale: string; text: string }[];
7
+ switchLanguage: (locale: string) => void;
8
+ render: (key: string) => string;
9
+ }
10
+
11
+ function useLanguage(): useLanguageResult {
12
+ const { themeConfig, location } = useThemeContext()!;
13
+
14
+ const languages = themeConfig.i18n;
15
+ let currentLanguage: { locale: string; text: string } | undefined = undefined;
16
+ let isFromPath: boolean;
17
+
18
+ const s = location.pathname.split('/')[1];
19
+
20
+ // 用户当前访问的页面是否有在路径中指定语言
21
+ isFromPath = !!(s && s.match(/^[a-z]{2}-[A-Z]{2}$/));
22
+
23
+ if (isFromPath)
24
+ currentLanguage = languages?.find(
25
+ (item) => item.locale === location.pathname.split('/')[1],
26
+ );
27
+ else currentLanguage = languages && languages[0] ? languages[0] : undefined;
28
+
29
+ function switchLanguage(locale: string) {
30
+ if (!languages || languages.length === 0) return;
31
+
32
+ if (!languages.find((l) => l.locale === locale)) return;
33
+
34
+ // 切换到默认语言
35
+ if (locale === languages[0].locale && isFromPath) {
36
+ let p = location.pathname.split('/');
37
+ p.shift();
38
+ p.shift();
39
+ window.location.pathname = p.join('/');
40
+ return;
41
+ }
42
+
43
+ // 当前在默认语言,切换到其他语言
44
+ if (!isFromPath) {
45
+ window.location.pathname = locale + location.pathname;
46
+ return;
47
+ }
48
+
49
+ let p = location.pathname.split('/');
50
+ p[1] = locale;
51
+ window.location.pathname = p.join('/');
52
+ }
53
+
54
+ function render(key: string) {
55
+ if (!currentLanguage || !themeConfig.locales) return key;
56
+ if (!themeConfig.locales[currentLanguage.locale]) return key;
57
+ return themeConfig.locales[currentLanguage.locale][key] || key;
58
+ }
59
+
60
+ return {
61
+ isFromPath,
62
+ currentLanguage,
63
+ languages: languages || [],
64
+ switchLanguage,
65
+ render,
66
+ };
67
+ }
68
+
69
+ export default useLanguage;
package/dist/compiler.js CHANGED
@@ -24,10 +24,21 @@ function compile(opts) {
24
24
  compilers: [],
25
25
  });
26
26
  result = `
27
- import React from 'react';
27
+ import React, { useEffect } from 'react';
28
28
  ${result}`;
29
29
  result = result.replace('/* @jsxRuntime classic */', '');
30
30
  result = result.replace('/* @jsx mdx */', '');
31
+ result = result.replace('return <MDXLayout', `
32
+
33
+ useEffect(() => {
34
+ if (window.location.hash.length !== 0) {
35
+ const hash = window.location.hash;
36
+ window.location.hash = '';
37
+ window.location.hash = hash;
38
+ }
39
+ }, []);
40
+
41
+ return <MDXLayout`);
31
42
  return { result };
32
43
  });
33
44
  }
package/dist/index.js CHANGED
@@ -76,7 +76,7 @@ exports.default = (api) => {
76
76
  api.writeTmpFile({
77
77
  path: 'index.ts',
78
78
  content: `
79
- export { Message } from '${require.resolve('../client/theme-doc')}';
79
+ export { Message, Hero } from '${require.resolve('../client/theme-doc')}';
80
80
  `,
81
81
  });
82
82
  api.writeTmpFile({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/plugin-docs",
3
- "version": "4.0.0-beta.18",
3
+ "version": "4.0.0-rc.1",
4
4
  "description": "@umijs/plugin-docs",
5
5
  "homepage": "https://github.com/umijs/umi-next/tree/master/packages/plugin-docs#readme",
6
6
  "bugs": "https://github.com/umijs/umi-next/issues",
@@ -12,12 +12,14 @@
12
12
  "main": "dist/index.js",
13
13
  "types": "dist/index.d.ts",
14
14
  "files": [
15
- "dist"
15
+ "dist",
16
+ "client"
16
17
  ],
17
18
  "scripts": {
18
19
  "build": "pnpm tsc",
19
20
  "build:css": "tailwindcss -i ./client/theme-doc/tailwind.css -o ./client/theme-doc/tailwind.out.css",
20
21
  "build:deps": "pnpm esno ../../scripts/bundleDeps.ts",
22
+ "build:extra": "pnpm build:css",
21
23
  "dev": "pnpm build -- --watch"
22
24
  },
23
25
  "dependencies": {
@@ -29,7 +31,7 @@
29
31
  "@types/keymaster": "^1.6.30",
30
32
  "classnames": "^2.3.1",
31
33
  "tailwindcss": "^3.0.15",
32
- "umi": "4.0.0-beta.18"
34
+ "umi": "4.0.0-rc.1"
33
35
  },
34
36
  "publishConfig": {
35
37
  "access": "public"