@umijs/plugin-docs 4.2.6-alpha.6 → 4.2.6

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,113 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/compiler.ts
30
+ var compiler_exports = {};
31
+ __export(compiler_exports, {
32
+ compile: () => compile
33
+ });
34
+ module.exports = __toCommonJS(compiler_exports);
35
+ var import_rehype_pretty_code = __toESM(require("rehype-pretty-code"));
36
+ var import_plugin_utils = require("umi/plugin-utils");
37
+ var import_mdx = require("../compiled/@mdx-js/mdx");
38
+ var import_rehype_slug = __toESM(require("../compiled/rehype-slug"));
39
+ var import_remark_gfm = __toESM(require("../compiled/remark-gfm"));
40
+ var import_rehype_autolink_headings = __toESM(require("../compiled/rehype-autolink-headings"));
41
+ var rehypePrettyCodeOptions = {
42
+ theme: "dark-plus",
43
+ onVisitLine(node) {
44
+ if (node.children.length === 0) {
45
+ node.children = [{ type: "text", value: " " }];
46
+ }
47
+ },
48
+ // 允许高亮代码行
49
+ // 对于高亮的代码行,设置为 highlighted 样式表类
50
+ onVisitHighlightedLine(node) {
51
+ node.properties.className.push("highlighted");
52
+ },
53
+ // 允许高亮代码文字
54
+ // 对于高亮的代码文字,设置为 word 样式表类
55
+ onVisitHighlightedWord(node) {
56
+ node.properties.className = ["word"];
57
+ }
58
+ };
59
+ async function compile(opts) {
60
+ const compiler = (0, import_mdx.createProcessor)({
61
+ jsx: true,
62
+ remarkPlugins: [import_remark_gfm.default],
63
+ rehypePlugins: [
64
+ import_rehype_slug.default,
65
+ [import_rehype_pretty_code.default, rehypePrettyCodeOptions],
66
+ import_rehype_autolink_headings.default
67
+ ]
68
+ });
69
+ try {
70
+ let result = String(await compiler.process(opts.content));
71
+ result = result.replace(
72
+ "function MDXContent(props = {}) {",
73
+ `
74
+ import { useEffect } from 'react';
75
+
76
+ function MDXContent(props = {}) {
77
+
78
+ useEffect(() => {
79
+ if (window.location.hash.length !== 0) {
80
+ // 为了右侧内容区能正常跳转
81
+ const hash = decodeURIComponent(window.location.hash);
82
+ setTimeout(() => {
83
+ document.getElementById(hash.slice(1))?.scrollIntoView();
84
+ }, 100);
85
+ } else {
86
+ window.scrollTo(0, 0);
87
+ }
88
+ document.getElementById('active-nav-item')?.scrollIntoView({
89
+ behavior: 'smooth',
90
+ block: 'center'
91
+ });
92
+ }, []);
93
+
94
+ `
95
+ );
96
+ return { result };
97
+ } catch (e) {
98
+ import_plugin_utils.logger.error(e.reason);
99
+ import_plugin_utils.logger.error(`Above error occurred in ${opts.fileName} at line ${e.line}`);
100
+ import_plugin_utils.logger.error(
101
+ opts.content.split("\n").filter((_, i) => i == e.line - 1).join("\n")
102
+ );
103
+ import_plugin_utils.logger.error(" ".repeat(e.column - 1) + "^");
104
+ if (process.env.NODE_ENV === "production") {
105
+ throw new Error("compile error", { cause: e });
106
+ }
107
+ return { result: "" };
108
+ }
109
+ }
110
+ // Annotate the CommonJS export names for ESM import in node:
111
+ 0 && (module.exports = {
112
+ compile
113
+ });
@@ -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,172 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.ts
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ default: () => src_default
33
+ });
34
+ module.exports = __toCommonJS(src_exports);
35
+ var import_bundler_utils = require("@umijs/bundler-utils");
36
+ var import_utils = require("@umijs/utils");
37
+ var import_fs = __toESM(require("fs"));
38
+ var import_path = require("path");
39
+ var import_markdown = require("./markdown");
40
+ var src_default = (api) => {
41
+ const locales = {};
42
+ const localesPath = (0, import_path.join)(api.cwd, "docs/locales");
43
+ if ((0, import_fs.existsSync)(localesPath)) {
44
+ import_fs.default.readdirSync(localesPath).forEach((file) => {
45
+ if (file.endsWith(".json")) {
46
+ const filePath = (0, import_path.join)(localesPath, file);
47
+ const content = import_fs.default.readFileSync(filePath).toString();
48
+ const json = JSON.parse(content);
49
+ const localeName = file.replace(".json", "");
50
+ locales[localeName] = json;
51
+ }
52
+ });
53
+ }
54
+ api.modifyDefaultConfig((memo) => {
55
+ memo.conventionRoutes = {
56
+ ...memo.conventionRoutes,
57
+ base: (0, import_path.join)(api.cwd, "docs")
58
+ };
59
+ memo.mdx = {
60
+ loader: require.resolve("./loader"),
61
+ loaderOptions: {}
62
+ };
63
+ return memo;
64
+ });
65
+ api.addLayouts(() => {
66
+ return [
67
+ {
68
+ id: "docs-layout",
69
+ file: withTmpPath({ api, path: "Layout.tsx" })
70
+ }
71
+ ];
72
+ });
73
+ api.onPatchRoute(({ route }) => {
74
+ if (route.__content) {
75
+ route.titles = (0, import_markdown.parseTitle)({
76
+ content: route.__content
77
+ });
78
+ }
79
+ if (route.file.match(/.[a-z]{2}-[A-Z]{2}.md$/)) {
80
+ route.path = route.path.replace(/(.*).([a-z]{2}-[A-Z]{2})$/, "$2/$1");
81
+ if (route.path.endsWith("README")) {
82
+ route.path = route.path.replace(/README$/, "");
83
+ }
84
+ }
85
+ });
86
+ api.modifyRoutes((r) => {
87
+ if (!locales)
88
+ return r;
89
+ for (const route in r) {
90
+ if (r[route].path.match(/^[a-z]{2}-[A-Z]{2}\/.*/))
91
+ continue;
92
+ const defaultLangFile = r[route].file.replace(
93
+ /(.[a-z]{2}-[A-Z]{2})?.md$/,
94
+ ""
95
+ );
96
+ Object.keys(locales).map((l) => {
97
+ if (r[defaultLangFile] && !r[defaultLangFile + "." + l]) {
98
+ r[defaultLangFile + "." + l] = {
99
+ ...r[defaultLangFile],
100
+ path: `/${l}/${r[defaultLangFile].path}`
101
+ };
102
+ }
103
+ });
104
+ }
105
+ return r;
106
+ });
107
+ api.onGenerateFiles(() => {
108
+ var _a;
109
+ let theme = ((_a = api.config.docs) == null ? void 0 : _a.theme) || require.resolve("../client/theme-doc/index.ts");
110
+ if (theme === "blog") {
111
+ theme = require.resolve("../client/theme-blog/index.ts");
112
+ }
113
+ theme = (0, import_utils.winPath)(theme);
114
+ const themeConfigPath = (0, import_utils.winPath)((0, import_path.join)(api.cwd, "theme.config.ts"));
115
+ const themeExists = (0, import_fs.existsSync)(themeConfigPath);
116
+ let injectLocale = `themeConfig.locales = ${JSON.stringify(locales)};`;
117
+ const [_, exports] = (0, import_bundler_utils.parseModuleSync)({
118
+ content: (0, import_fs.readFileSync)(theme, "utf-8"),
119
+ path: theme
120
+ });
121
+ api.writeTmpFile({
122
+ path: "index.ts",
123
+ content: `
124
+ export { ${exports.filter((item) => !item.startsWith("$")).join(", ")} } from '${(0, import_utils.winPath)(
125
+ require.resolve("../client/theme-doc/index.ts")
126
+ )}';
127
+ `
128
+ });
129
+ api.writeTmpFile({
130
+ path: "Layout.tsx",
131
+ content: `
132
+ import React from 'react';
133
+ import { useOutlet, useAppData, useLocation, Link, history } from 'umi';
134
+ import { $Layout as Layout } from '${(0, import_utils.winPath)(
135
+ require.resolve("../client/theme-doc/index.ts")
136
+ )}';
137
+ ${themeExists ? `import themeConfig from '${themeConfigPath}'` : `const themeConfig = {}`}
138
+
139
+ ${injectLocale}
140
+
141
+ export default () => {
142
+ const outlet = useOutlet();
143
+ const appData = useAppData();
144
+ const location = useLocation();
145
+ return (
146
+ <Layout appData={appData} components={{Link}} themeConfig={themeConfig} location={location} history={history}>
147
+ <div>{ outlet }</div>
148
+ </Layout>
149
+ );
150
+ };
151
+ `
152
+ });
153
+ });
154
+ api.chainWebpack((memo) => {
155
+ if (api.env === "development") {
156
+ memo.plugin("fastRefresh").tap(([params]) => [
157
+ {
158
+ ...params,
159
+ include: /\.([cm]js|[jt]sx?|flow|md)$/i
160
+ }
161
+ ]);
162
+ }
163
+ return memo;
164
+ });
165
+ };
166
+ function withTmpPath(opts) {
167
+ return (0, import_path.join)(
168
+ opts.api.paths.absTmpPath,
169
+ opts.api.plugin.key && !opts.noPluginDir ? `plugin-${opts.api.plugin.key}` : "",
170
+ opts.path
171
+ );
172
+ }
@@ -0,0 +1 @@
1
+ export default function (content: string): Promise<any>;
package/dist/loader.js ADDED
@@ -0,0 +1,40 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/loader.ts
20
+ var loader_exports = {};
21
+ __export(loader_exports, {
22
+ default: () => loader_default
23
+ });
24
+ module.exports = __toCommonJS(loader_exports);
25
+ var import_compiler = require("./compiler");
26
+ async function loader_default(content) {
27
+ const filename = this.resourcePath;
28
+ const callback = this.async();
29
+ try {
30
+ const { result } = await (0, import_compiler.compile)({
31
+ content,
32
+ fileName: filename
33
+ });
34
+ return callback(null, result);
35
+ } catch (e) {
36
+ const err = e;
37
+ e.message = `${filename}: ${e.message}`;
38
+ throw err;
39
+ }
40
+ }
@@ -0,0 +1,6 @@
1
+ export declare function parseTitle(opts: {
2
+ content: string;
3
+ }): {
4
+ level: number;
5
+ title: string;
6
+ }[];
@@ -0,0 +1,45 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/markdown.ts
20
+ var markdown_exports = {};
21
+ __export(markdown_exports, {
22
+ parseTitle: () => parseTitle
23
+ });
24
+ module.exports = __toCommonJS(markdown_exports);
25
+ function parseTitle(opts) {
26
+ const lines = opts.content.replace(/{[\n\s\t]*\/\*[\s\S]*?\*\/[\n\s\t]*}/g, "").split("\n");
27
+ let i = 0;
28
+ const ret = [];
29
+ while (i < lines.length) {
30
+ const line = lines[i].trim();
31
+ const match = line.match(/^(#+)\s+(.*)/);
32
+ if (match) {
33
+ ret.push({
34
+ level: match[1].length,
35
+ title: match[2]
36
+ });
37
+ }
38
+ i += 1;
39
+ }
40
+ return ret;
41
+ }
42
+ // Annotate the CommonJS export names for ESM import in node:
43
+ 0 && (module.exports = {
44
+ parseTitle
45
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/plugin-docs",
3
- "version": "4.2.6-alpha.6",
3
+ "version": "4.2.6",
4
4
  "description": "@umijs/plugin-docs",
5
5
  "homepage": "https://github.com/umijs/umi/tree/master/packages/plugin-docs#readme",
6
6
  "bugs": "https://github.com/umijs/umi/issues",
@@ -33,7 +33,7 @@
33
33
  "rehype-slug": "5.0.1",
34
34
  "remark-gfm": "^3.0.1",
35
35
  "tailwindcss": "^3.2.4",
36
- "umi": "4.2.6-alpha.6"
36
+ "umi": "4.2.6"
37
37
  },
38
38
  "publishConfig": {
39
39
  "access": "public"