@umijs/plugin-docs 4.0.0-canary.20220429.2 → 4.0.0-canary.20220506.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,6 @@
1
+ export declare function compile(opts: {
2
+ content: string;
3
+ fileName: string;
4
+ }): Promise<{
5
+ result: string;
6
+ }>;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.compile = void 0;
7
+ const plugin_utils_1 = require("umi/plugin-utils");
8
+ const rehype_pretty_code_1 = __importDefault(require("rehype-pretty-code"));
9
+ // @ts-ignore
10
+ const mdx_1 = require("../compiled/@mdx-js/mdx");
11
+ // @ts-ignore
12
+ const rehype_slug_1 = __importDefault(require("../compiled/rehype-slug"));
13
+ // @ts-ignore
14
+ const remark_gfm_1 = __importDefault(require("../compiled/remark-gfm"));
15
+ // https://rehype-pretty-code.netlify.app
16
+ const rehypePrettyCodeOptions = {
17
+ theme: 'dark-plus',
18
+ onVisitLine(node) {
19
+ // Prevent lines from collapsing in `display: grid` mode, and
20
+ // allow empty lines to be copy/pasted
21
+ if (node.children.length === 0) {
22
+ node.children = [{ type: 'text', value: ' ' }];
23
+ }
24
+ },
25
+ // 允许高亮代码行
26
+ // 对于高亮的代码行,设置为 highlighted 样式表类
27
+ onVisitHighlightedLine(node) {
28
+ node.properties.className.push('highlighted');
29
+ },
30
+ // 允许高亮代码文字
31
+ // 对于高亮的代码文字,设置为 word 样式表类
32
+ onVisitHighlightedWord(node) {
33
+ node.properties.className = ['word'];
34
+ },
35
+ };
36
+ async function compile(opts) {
37
+ const compiler = (0, mdx_1.createProcessor)({
38
+ jsx: true,
39
+ remarkPlugins: [remark_gfm_1.default],
40
+ rehypePlugins: [rehype_slug_1.default, [rehype_pretty_code_1.default, rehypePrettyCodeOptions]],
41
+ });
42
+ try {
43
+ let result = String(await compiler.process(opts.content));
44
+ result = result.replace('function MDXContent(props = {}) {', `
45
+ import { useEffect } from 'react';
46
+
47
+ function MDXContent(props = {}) {
48
+
49
+ useEffect(() => {
50
+ if (window.location.hash.length !== 0) {
51
+ const hash = window.location.hash;
52
+ document.getElementById(hash.slice(1))?.scrollIntoView();
53
+ } else {
54
+ window.scrollTo(0, 0);
55
+ }
56
+ document.getElementById('active-nav-item')?.scrollIntoView({
57
+ behavior: 'smooth',
58
+ block: 'center'
59
+ });
60
+ }, []);
61
+
62
+ `);
63
+ return { result };
64
+ }
65
+ catch (e) {
66
+ plugin_utils_1.logger.error(e.reason);
67
+ plugin_utils_1.logger.error(`Above error occurred in ${opts.fileName} at line ${e.line}`);
68
+ plugin_utils_1.logger.error(opts.content
69
+ .split('\n')
70
+ .filter((_, i) => i == e.line - 1)
71
+ .join('\n'));
72
+ plugin_utils_1.logger.error(' '.repeat(e.column - 1) + '^');
73
+ return { result: '' };
74
+ }
75
+ }
76
+ exports.compile = compile;
@@ -0,0 +1,3 @@
1
+ import { IApi } from 'umi';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const bundler_utils_1 = require("@umijs/bundler-utils");
27
+ const utils_1 = require("@umijs/utils");
28
+ const fs_1 = __importStar(require("fs"));
29
+ const path_1 = require("path");
30
+ const markdown_1 = require("./markdown");
31
+ exports.default = (api) => {
32
+ // 把用户当前有设置在 docs/locales 下的语系放到变量 locales 中,方便后续使用
33
+ const locales = {};
34
+ const localesPath = (0, path_1.join)(api.cwd, 'docs/locales');
35
+ if ((0, fs_1.existsSync)(localesPath)) {
36
+ fs_1.default.readdirSync(localesPath).forEach((file) => {
37
+ if (file.endsWith('.json')) {
38
+ const filePath = (0, path_1.join)(localesPath, file);
39
+ const content = fs_1.default.readFileSync(filePath).toString();
40
+ const json = JSON.parse(content);
41
+ const localeName = file.replace('.json', '');
42
+ locales[localeName] = json;
43
+ }
44
+ });
45
+ }
46
+ api.modifyDefaultConfig((memo) => {
47
+ memo.conventionRoutes = {
48
+ ...memo.conventionRoutes,
49
+ base: (0, path_1.join)(api.cwd, 'docs'),
50
+ };
51
+ memo.mdx = {
52
+ loader: require.resolve('./loader'),
53
+ loaderOptions: {},
54
+ };
55
+ return memo;
56
+ });
57
+ api.addLayouts(() => {
58
+ return [
59
+ {
60
+ id: 'docs-layout',
61
+ file: withTmpPath({ api, path: 'Layout.tsx' }),
62
+ },
63
+ ];
64
+ });
65
+ api.onPatchRoute(({ route }) => {
66
+ if (route.__content) {
67
+ route.titles = (0, markdown_1.parseTitle)({
68
+ content: route.__content,
69
+ });
70
+ }
71
+ // 放在 docs/xxx.zh-CN.md 的文档,会被映射到 /zh-CN/docs/xxx 目录
72
+ if (route.file.match(/.[a-z]{2}-[A-Z]{2}.md$/)) {
73
+ route.path = route.path.replace(/(.*).([a-z]{2}-[A-Z]{2})$/, '$2/$1');
74
+ // 放在 docs/xxx/README.zh-CN.md 格式结尾的文档,会被映射到 /zh-CN/docs 目录
75
+ if (route.path.endsWith('README')) {
76
+ route.path = route.path.replace(/README$/, '');
77
+ }
78
+ }
79
+ });
80
+ // 检查路由是否存在其他语言,没有的话做 fallback 处理
81
+ api.modifyRoutes((r) => {
82
+ if (!locales)
83
+ return r;
84
+ for (const route in r) {
85
+ if (r[route].path.match(/^[a-z]{2}-[A-Z]{2}\/.*/))
86
+ continue;
87
+ const defaultLangFile = r[route].file.replace(/(.[a-z]{2}-[A-Z]{2})?.md$/, '');
88
+ Object.keys(locales).map((l) => {
89
+ if (r[defaultLangFile] && !r[defaultLangFile + '.' + l]) {
90
+ r[defaultLangFile + '.' + l] = {
91
+ ...r[defaultLangFile],
92
+ path: `/${l}/${r[defaultLangFile].path}`,
93
+ };
94
+ }
95
+ });
96
+ }
97
+ return r;
98
+ });
99
+ api.onGenerateFiles(() => {
100
+ var _a;
101
+ // theme path
102
+ let theme = ((_a = api.config.docs) === null || _a === void 0 ? void 0 : _a.theme) || require.resolve('../client/theme-doc/index.ts');
103
+ if (theme === 'blog') {
104
+ theme = require.resolve('../client/theme-blog/index.ts');
105
+ }
106
+ theme = (0, utils_1.winPath)(theme);
107
+ const themeConfigPath = (0, utils_1.winPath)((0, path_1.join)(api.cwd, 'theme.config.ts'));
108
+ const themeExists = (0, fs_1.existsSync)(themeConfigPath);
109
+ // 将 docs/locales 目录下的 json 文件注入到 themeConfig.locales 中
110
+ let injectLocale = `themeConfig.locales = ${JSON.stringify(locales)};`;
111
+ // exports don't start with $ will be MDX Component
112
+ const [_, exports] = (0, bundler_utils_1.parseModuleSync)({
113
+ content: (0, fs_1.readFileSync)(theme, 'utf-8'),
114
+ path: theme,
115
+ });
116
+ api.writeTmpFile({
117
+ path: 'index.ts',
118
+ content: `
119
+ export { ${exports
120
+ .filter((item) => !item.startsWith('$'))
121
+ .join(', ')} } from '${(0, utils_1.winPath)(require.resolve('../client/theme-doc/index.ts'))}';
122
+ `,
123
+ });
124
+ api.writeTmpFile({
125
+ path: 'Layout.tsx',
126
+ content: `
127
+ import React from 'react';
128
+ import { useOutlet, useAppData, useLocation, Link } from 'umi';
129
+ import { $Layout as Layout } from '${(0, utils_1.winPath)(require.resolve('../client/theme-doc/index.ts'))}';
130
+ ${themeExists
131
+ ? `import themeConfig from '${themeConfigPath}'`
132
+ : `const themeConfig = {}`}
133
+
134
+ ${injectLocale}
135
+
136
+ export default () => {
137
+ const outlet = useOutlet();
138
+ const appData = useAppData();
139
+ const location = useLocation();
140
+ return (
141
+ <Layout appData={appData} components={{Link}} themeConfig={themeConfig} location={location}>
142
+ <div>{ outlet }</div>
143
+ </Layout>
144
+ );
145
+ };
146
+ `,
147
+ });
148
+ });
149
+ };
150
+ function withTmpPath(opts) {
151
+ return (0, path_1.join)(opts.api.paths.absTmpPath, opts.api.plugin.key && !opts.noPluginDir
152
+ ? `plugin-${opts.api.plugin.key}`
153
+ : '', opts.path);
154
+ }
@@ -0,0 +1 @@
1
+ export default function (content: string): Promise<any>;
package/dist/loader.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const compiler_1 = require("./compiler");
4
+ async function default_1(content) {
5
+ // @ts-ignore
6
+ const filename = this.resourcePath;
7
+ // @ts-ignore
8
+ const callback = this.async();
9
+ try {
10
+ const { result } = await (0, compiler_1.compile)({
11
+ content,
12
+ fileName: filename,
13
+ });
14
+ return callback(null, result);
15
+ }
16
+ catch (e) {
17
+ const err = e;
18
+ e.message = `${filename}: ${e.message}`;
19
+ throw err;
20
+ }
21
+ }
22
+ exports.default = default_1;
@@ -0,0 +1,6 @@
1
+ export declare function parseTitle(opts: {
2
+ content: string;
3
+ }): {
4
+ level: number;
5
+ title: string;
6
+ }[];
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseTitle = void 0;
4
+ function parseTitle(opts) {
5
+ const lines = opts.content.split('\n');
6
+ let i = 0;
7
+ const ret = [];
8
+ while (i < lines.length) {
9
+ const line = lines[i].trim();
10
+ const match = line.match(/^(#+)\s+(.*)/);
11
+ if (match) {
12
+ ret.push({
13
+ level: match[1].length,
14
+ title: match[2],
15
+ });
16
+ }
17
+ i += 1;
18
+ }
19
+ return ret;
20
+ }
21
+ exports.parseTitle = parseTitle;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/plugin-docs",
3
- "version": "4.0.0-canary.20220429.2",
3
+ "version": "4.0.0-canary.20220506.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",
@@ -39,7 +39,7 @@
39
39
  "rehype-slug": "5.0.1",
40
40
  "remark-gfm": "^3.0.1",
41
41
  "tailwindcss": "^3.0.23",
42
- "umi": "4.0.0-canary.20220429.2"
42
+ "umi": "4.0.0-canary.20220506.1"
43
43
  },
44
44
  "publishConfig": {
45
45
  "access": "public"