Skip to content

Commit

Permalink
feat(umi-plugin): suppor to configure father-doc via umi or plugin api (
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Nov 24, 2019
1 parent 20841d5 commit 08ac62d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
24 changes: 21 additions & 3 deletions packages/umi-plugin-father-doc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import path from 'path';
import { IApi } from 'umi-types';
import { IApi, IRoute } from 'umi-types';
import getRouteConfig from './routes/getRouteConfig';
import getMenuFromRoutes from'./routes/getMenuFromRoutes';

export default function (api: IApi) {
const routeConfig = getRouteConfig(api.paths);
export interface IFatherDocOpts {
name?: string;
logo?: URL;
include?: string[];
routes?: {
path: IRoute['path'];
component: IRoute['component'];
redirect: IRoute['redirect'];
[key: string]: any;
}[];
}

export default function (api: IApi, opts: IFatherDocOpts) {
// apply default options
opts = Object.assign({
name: require(path.join(api.paths.cwd, 'package.json')).name,
include: ['docs'],
}, (api.config as any).doc, opts);

const routeConfig = getRouteConfig(api.paths, opts);

api.registerPlugin({
id: require.resolve('umi-plugin-react'),
Expand Down
34 changes: 26 additions & 8 deletions packages/umi-plugin-father-doc/src/routes/getRouteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,40 @@ import path from 'path';
import { IApi, IRoute } from 'umi-types';
import getFrontMatter from './getFrontMatter';
import getRouteConfigFromDir from './getRouteConfigFromDir';
import { IFatherDocOpts } from '../index';

export default (paths: IApi['paths']): IRoute[] => {
export default (paths: IApi['paths'], opts: IFatherDocOpts): IRoute[] => {
const config = [{
path: '/',
component: path.join(__dirname, '../themes/default/layout.js'),
routes: [],
}];
const docsPath = path.join(paths.cwd, 'docs');

// find routes from docs path
if (fs.existsSync(docsPath) && fs.statSync(docsPath).isDirectory()) {
config[0].routes.push(...getRouteConfigFromDir(docsPath));
}
if (opts.routes) {
// only apply user's routes if there has routes config
config[0].routes = opts.routes.map(({ component, ...routeOpts }) => ({
component: (
path.isAbsolute(component as string)
? component
: path.join(paths.absPagesPath, component as string)
),
...routeOpts,
}));
} else {
// generate routes automatically if there has no routes config

// find routes from include path
opts.include.forEach((item) => {
const docsPath = path.isAbsolute(item) ? item : path.join(paths.cwd, item);

// find routes from source path
config[0].routes.push(...getRouteConfigFromDir(paths.absPagesPath));
if (fs.existsSync(docsPath) && fs.statSync(docsPath).isDirectory()) {
config[0].routes.push(...getRouteConfigFromDir(docsPath));
}
});

// find routes from source path
config[0].routes.push(...getRouteConfigFromDir(paths.absPagesPath));
}

// read yaml config for all routes
config[0].routes.forEach((route) => {
Expand Down

0 comments on commit 08ac62d

Please sign in to comment.