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

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.
Files changed (34) hide show
  1. package/client/theme-blog/index.ts +1 -0
  2. package/client/theme-doc/Github.tsx +18 -0
  3. package/client/theme-doc/Head.tsx +72 -0
  4. package/client/theme-doc/LangSwitch.tsx +55 -0
  5. package/client/theme-doc/Layout.tsx +116 -0
  6. package/client/theme-doc/Logo.tsx +18 -0
  7. package/client/theme-doc/NavBar.tsx +27 -0
  8. package/client/theme-doc/Search.tsx +178 -0
  9. package/client/theme-doc/Sidebar.tsx +84 -0
  10. package/client/theme-doc/ThemeSwitch.tsx +60 -0
  11. package/client/theme-doc/Tip.tsx +5 -0
  12. package/client/theme-doc/Toc.tsx +63 -0
  13. package/client/theme-doc/VersionSwitch.tsx +5 -0
  14. package/client/theme-doc/components/Announcement.tsx +62 -0
  15. package/client/theme-doc/components/FeatureItem.tsx +48 -0
  16. package/client/theme-doc/components/Features.tsx +44 -0
  17. package/client/theme-doc/components/Hero.tsx +152 -0
  18. package/client/theme-doc/components/Message.tsx +57 -0
  19. package/client/theme-doc/context.ts +40 -0
  20. package/client/theme-doc/firefox-polyfill.css +31 -0
  21. package/client/theme-doc/icons/github.svg +1 -0
  22. package/client/theme-doc/icons/hero-bg.svg +1 -0
  23. package/client/theme-doc/icons/moon.png +0 -0
  24. package/client/theme-doc/icons/star.png +0 -0
  25. package/client/theme-doc/icons/sun.png +0 -0
  26. package/client/theme-doc/icons/umi.png +0 -0
  27. package/client/theme-doc/index.ts +8 -0
  28. package/client/theme-doc/tailwind.css +153 -0
  29. package/client/theme-doc/tailwind.out.css +2031 -0
  30. package/client/theme-doc/useLanguage.ts +69 -0
  31. package/dist/compiler.d.ts +1 -1
  32. package/dist/compiler.js +22 -11
  33. package/dist/index.js +18 -3
  34. package/package.json +23 -8
@@ -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;
@@ -1,5 +1,5 @@
1
1
  export declare function compile(opts: {
2
2
  content: string;
3
3
  }): Promise<{
4
- result: any;
4
+ result: string;
5
5
  }>;
package/dist/compiler.js CHANGED
@@ -14,20 +14,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.compile = void 0;
16
16
  // @ts-ignore
17
- const mdx_1 = __importDefault(require("@mdx-js/mdx"));
18
- const remark_slug_1 = __importDefault(require("remark-slug"));
17
+ const mdx_1 = require("../compiled/@mdx-js/mdx");
18
+ // @ts-ignore
19
+ const rehype_slug_1 = __importDefault(require("../compiled/rehype-slug"));
19
20
  function compile(opts) {
20
21
  return __awaiter(this, void 0, void 0, function* () {
21
- let result = yield (0, mdx_1.default)(opts.content, {
22
- remarkPlugins: [remark_slug_1.default],
23
- rehypePlugins: [],
24
- compilers: [],
22
+ const compiler = (0, mdx_1.createProcessor)({
23
+ jsx: true,
24
+ remarkPlugins: [],
25
+ rehypePlugins: [rehype_slug_1.default],
25
26
  });
26
- result = `
27
- import React from 'react';
28
- ${result}`;
29
- result = result.replace('/* @jsxRuntime classic */', '');
30
- result = result.replace('/* @jsx mdx */', '');
27
+ let result = String(yield compiler.process(opts.content));
28
+ result = result.replace('function MDXContent(props = {}) {', `
29
+ import { useEffect } from 'react';
30
+
31
+ function MDXContent(props = {}) {
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
+ `);
31
42
  return { result };
32
43
  });
33
44
  }
package/dist/index.js CHANGED
@@ -19,6 +19,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
19
  return result;
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
+ const bundler_utils_1 = require("@umijs/bundler-utils");
23
+ const utils_1 = require("@umijs/utils");
22
24
  const fs_1 = __importStar(require("fs"));
23
25
  const path_1 = require("path");
24
26
  const markdown_1 = require("./markdown");
@@ -55,6 +57,13 @@ exports.default = (api) => {
55
57
  }
56
58
  });
57
59
  api.onGenerateFiles(() => {
60
+ var _a;
61
+ // theme path
62
+ let theme = ((_a = api.config.docs) === null || _a === void 0 ? void 0 : _a.theme) || require.resolve('../client/theme-doc/index.ts');
63
+ if (theme === 'blog') {
64
+ theme = require.resolve('../client/theme-blog/index.ts');
65
+ }
66
+ theme = (0, utils_1.winPath)(theme);
58
67
  const themeConfigPath = (0, path_1.join)(api.cwd, 'theme.config.ts');
59
68
  const themeExists = (0, fs_1.existsSync)(themeConfigPath);
60
69
  // 将 docs/locales 目录下的 json 文件注入到 themeConfig.locales 中
@@ -72,11 +81,17 @@ exports.default = (api) => {
72
81
  }
73
82
  });
74
83
  }
75
- // @TODO: 需要能够动态解析 theme 中导出的组件,现在是硬编码
84
+ // exports don't start with $ will be MDX Component
85
+ const [_, exports] = (0, bundler_utils_1.parseModuleSync)({
86
+ content: (0, fs_1.readFileSync)(theme, 'utf-8'),
87
+ path: theme,
88
+ });
76
89
  api.writeTmpFile({
77
90
  path: 'index.ts',
78
91
  content: `
79
- export { Message } from '${require.resolve('../client/theme-doc')}';
92
+ export { ${exports
93
+ .filter((item) => !item.startsWith('$'))
94
+ .join(', ')} } from '${require.resolve('../client/theme-doc/index.ts')}';
80
95
  `,
81
96
  });
82
97
  api.writeTmpFile({
@@ -84,7 +99,7 @@ export { Message } from '${require.resolve('../client/theme-doc')}';
84
99
  content: `
85
100
  import React from 'react';
86
101
  import { useOutlet, useAppData, useLocation, Link } from 'umi';
87
- import { Layout } from '${require.resolve('../client/theme-doc')}';
102
+ import { $Layout as Layout } from '${require.resolve('../client/theme-doc/index.ts')}';
88
103
  ${themeExists
89
104
  ? `import themeConfig from '${themeConfigPath}'`
90
105
  : `const themeConfig = {}`}
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.3",
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,29 +12,44 @@
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": {
24
- "@mdx-js/mdx": "1.6.22",
25
- "keymaster": "^1.6.2",
26
- "remark-slug": "6.1.0"
26
+ "keymaster": "1.6.2",
27
+ "react-helmet": "^6.1.0"
27
28
  },
28
29
  "devDependencies": {
30
+ "@mdx-js/mdx": "2.0.0",
29
31
  "@types/keymaster": "^1.6.30",
32
+ "@types/react-helmet": "^6.1.5",
30
33
  "classnames": "^2.3.1",
31
- "tailwindcss": "^3.0.15",
32
- "umi": "4.0.0-beta.18"
34
+ "rehype-slug": "5.0.1",
35
+ "tailwindcss": "^3.0.23",
36
+ "umi": "4.0.0-rc.3"
33
37
  },
34
38
  "publishConfig": {
35
39
  "access": "public"
36
40
  },
37
41
  "authors": [
38
42
  "chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
39
- ]
43
+ ],
44
+ "compiledConfig": {
45
+ "deps": [
46
+ "@mdx-js/mdx",
47
+ "rehype-slug"
48
+ ],
49
+ "externals": {},
50
+ "excludeDtsDeps": [
51
+ "@mdx-js/mdx",
52
+ "rehype-slug"
53
+ ]
54
+ }
40
55
  }