@umijs/plugin-docs 4.0.0-beta.17

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.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @umijs/plugin-docs
2
+
3
+ See our website [umijs](https://umijs.org) for more information.
@@ -0,0 +1,8 @@
1
+ export declare function compile(opts: {
2
+ content: string;
3
+ }): Promise<{
4
+ result: any;
5
+ meta: {
6
+ title: any;
7
+ };
8
+ }>;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.compile = void 0;
16
+ // @ts-ignore
17
+ const mdx_1 = __importDefault(require("@mdx-js/mdx"));
18
+ function compile(opts) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ let result = yield (0, mdx_1.default)(opts.content, {
21
+ remarkPlugins: [],
22
+ rehypePlugins: [],
23
+ compilers: [],
24
+ });
25
+ result = `
26
+ import React from 'react';
27
+ ${result}`;
28
+ result = result.replace('/* @jsxRuntime classic */', '');
29
+ result = result.replace('/* @jsx mdx */', '');
30
+ const title = result.match(/<h1>{`(.*?)`}<\/h1>/)[1];
31
+ result = `${result}\nMDXContent.title = '${title}';`;
32
+ return {
33
+ result,
34
+ meta: {
35
+ title,
36
+ },
37
+ };
38
+ });
39
+ }
40
+ 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,105 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ const fs_1 = __importStar(require("fs"));
23
+ const path_1 = require("path");
24
+ const markdown_1 = require("./markdown");
25
+ exports.default = (api) => {
26
+ api.modifyDefaultConfig((memo) => {
27
+ memo.conventionRoutes = Object.assign(Object.assign({}, memo.conventionRoutes), { base: (0, path_1.join)(api.cwd, 'docs') });
28
+ memo.mdx = {
29
+ loader: require.resolve('./loader'),
30
+ loaderOptions: {},
31
+ };
32
+ return memo;
33
+ });
34
+ api.addLayouts(() => {
35
+ return [
36
+ {
37
+ id: 'docs-layout',
38
+ file: withTmpPath({ api, path: 'Layout.tsx' }),
39
+ },
40
+ ];
41
+ });
42
+ api.onPatchRoute(({ route }) => {
43
+ if (route.__content) {
44
+ route.titles = (0, markdown_1.parseTitle)({
45
+ content: route.__content,
46
+ });
47
+ }
48
+ // 放在 docs/xxx.zh-CN.md 的文档,会被映射到 /zh-CN/docs/xxx 目录
49
+ if (route.file.match(/.[a-z]{2}-[A-Z]{2}.md$/)) {
50
+ route.path = route.path.replace(/(.*).([a-z]{2}-[A-Z]{2})$/, '$2/$1');
51
+ // 放在 docs/xxx/README.zh-CN.md 格式结尾的文档,会被映射到 /zh-CN/docs 目录
52
+ if (route.path.endsWith('README')) {
53
+ route.path = route.path.replace(/README$/, '');
54
+ }
55
+ }
56
+ });
57
+ api.onGenerateFiles(() => {
58
+ const themeConfigPath = (0, path_1.join)(api.cwd, 'theme.config.ts');
59
+ const themeExists = (0, fs_1.existsSync)(themeConfigPath);
60
+ // 将 docs/locales 目录下的 json 文件注入到 themeConfig.locales 中
61
+ let injectLocale = 'themeConfig.locales = {};';
62
+ const localesPath = (0, path_1.join)(api.cwd, 'docs/locales');
63
+ if ((0, fs_1.existsSync)(localesPath)) {
64
+ fs_1.default.readdirSync(localesPath).forEach((file) => {
65
+ if (file.endsWith('.json')) {
66
+ const filePath = (0, path_1.join)(localesPath, file);
67
+ const content = fs_1.default.readFileSync(filePath).toString();
68
+ const json = JSON.parse(content);
69
+ const localeName = file.replace('.json', '');
70
+ injectLocale += `themeConfig.locales['${localeName}'] = ${JSON.stringify(json)};
71
+ `;
72
+ }
73
+ });
74
+ }
75
+ api.writeTmpFile({
76
+ path: 'Layout.tsx',
77
+ content: `
78
+ import React from 'react';
79
+ import { useOutlet, useAppData, useLocation, Link } from 'umi';
80
+ import { Layout } from '${require.resolve('../client/theme-doc')}';
81
+ ${themeExists
82
+ ? `import themeConfig from '${themeConfigPath}'`
83
+ : `const themeConfig = {}`}
84
+
85
+ ${injectLocale}
86
+
87
+ export default () => {
88
+ const outlet = useOutlet();
89
+ const appData = useAppData();
90
+ const location = useLocation();
91
+ return (
92
+ <Layout appData={appData} components={{Link}} themeConfig={themeConfig} location={location}>
93
+ <div>{ outlet }</div>
94
+ </Layout>
95
+ );
96
+ };
97
+ `,
98
+ });
99
+ });
100
+ };
101
+ function withTmpPath(opts) {
102
+ return (0, path_1.join)(opts.api.paths.absTmpPath, opts.api.plugin.key && !opts.noPluginDir
103
+ ? `plugin-${opts.api.plugin.key}`
104
+ : '', opts.path);
105
+ }
@@ -0,0 +1 @@
1
+ export default function (content: string): Promise<any>;
package/dist/loader.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const compiler_1 = require("./compiler");
13
+ function default_1(content) {
14
+ return __awaiter(this, void 0, void 0, function* () {
15
+ // @ts-ignore
16
+ const callback = this.async();
17
+ const { result } = yield (0, compiler_1.compile)({
18
+ content,
19
+ });
20
+ return callback(null, result);
21
+ });
22
+ }
23
+ 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 ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@umijs/plugin-docs",
3
+ "version": "4.0.0-beta.17",
4
+ "description": "@umijs/plugin-docs",
5
+ "homepage": "https://github.com/umijs/umi-next/tree/master/packages/plugin-docs#readme",
6
+ "bugs": "https://github.com/umijs/umi-next/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/umijs/umi-next"
10
+ },
11
+ "license": "MIT",
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "pnpm tsc",
19
+ "build:css": "tailwindcss -i ./client/theme-doc/tailwind.css -o ./client/theme-doc/tailwind.out.css",
20
+ "build:deps": "pnpm esno ../../scripts/bundleDeps.ts",
21
+ "dev": "pnpm build -- --watch"
22
+ },
23
+ "dependencies": {
24
+ "@mdx-js/mdx": "1.6.22",
25
+ "classnames": "^2.3.1"
26
+ },
27
+ "devDependencies": {
28
+ "tailwindcss": "^3.0.15",
29
+ "umi": "4.0.0-beta.17"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "authors": [
35
+ "chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
36
+ ]
37
+ }