dumi-theme-nocobase
Version:
<h1 align="center">dumi-theme-nocobase</h1>
229 lines • 10.5 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import { useFullSidebarData, useLocation, useSidebarData } from 'dumi';
import { useMemo } from 'react';
import ExternalLink from "../common/ExternalLink";
import { removeTitleCode } from "../utils";
import useAdditionalThemeConfig from "./useAdditionalThemeConfig";
import { jsx as ___EmotionJSX } from "@emotion/react";
var useMenu = function useMenu() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _useLocation = useLocation(),
pathname = _useLocation.pathname,
search = _useLocation.search;
var sidebarData = useSidebarData();
var _useAdditionalThemeCo = useAdditionalThemeConfig(),
sidebarGroupModePath = _useAdditionalThemeCo.sidebarGroupModePath,
_useAdditionalThemeCo2 = _useAdditionalThemeCo.sidebarEnhance,
sidebarEnhance = _useAdditionalThemeCo2 === void 0 ? {} : _useAdditionalThemeCo2;
var before = options.before,
after = options.after;
var fullSidebarData = useFullSidebarData();
var linkTitleMap = useMemo(function () {
return Object.values(fullSidebarData).reduce(function (res, sidebar) {
var sidebarItems = sidebar.map(function (item) {
return item.children;
}).flat();
sidebarItems.forEach(function (item) {
res[item.link] = item.title;
});
return res;
}, {});
}, [fullSidebarData]);
var currentSidebarEnhanceData = useMemo(function () {
var currentLink = Object.keys(sidebarEnhance).find(function (link) {
return pathname.startsWith(link);
});
if (!currentLink) return undefined;
return sidebarEnhance[currentLink];
}, [pathname, sidebarEnhance]);
var sidebarEnhanceMenuItems = useMemo(function () {
var isItemMenu = function isItemMenu(v) {
return v && _typeof(v) === 'object' && 'link' in v && 'title' in v;
};
var isGroupMenu = function isGroupMenu(v) {
return v && _typeof(v) === 'object' && v.type === 'group';
};
var isSubMenu = function isSubMenu(v) {
return v && _typeof(v) === 'object' && 'children' in v;
};
function processMenu(menu, parentKey) {
if (typeof menu === 'string') {
// menu: '/introduction'
return {
key: menu,
label: ___EmotionJSX(ExternalLink, {
to: "".concat(menu).concat(search)
}, before, linkTitleMap[menu], after)
};
}
if (isGroupMenu(menu)) {
// menu: { type: 'group', title: '组件', children: ['/components/button', { title: 'Installation', children: ['/aaa'] }] }
return {
type: 'group',
label: menu.title,
key: parentKey ? "".concat(parentKey, "-").concat(menu.title) : menu.title,
children: menu.children.map(function (item) {
return processMenu(item, menu.title);
})
};
}
if (isSubMenu(menu)) {
var _menu$children;
return {
key: parentKey ? "".concat(parentKey, "-").concat(menu.title) : menu.title,
label: menu.subTitle ? ___EmotionJSX("span", {
style: {
lineHeight: 1.2,
display: 'block',
paddingTop: 3
}
}, ___EmotionJSX("span", null, menu.title), menu.subTitle && ___EmotionJSX("span", {
style: {
opacity: 0.5,
display: 'block',
fontSize: '0.8em',
paddingTop: 3
},
className: "sub-title"
}, menu.subTitle)) : ___EmotionJSX("span", null, menu.title),
children: menu.children.map(function (item) {
return processMenu(item, menu.title);
}),
disabled: menu.disabled || !((_menu$children = menu.children) !== null && _menu$children !== void 0 && _menu$children.length)
};
}
if (isItemMenu(menu)) {
// { title: 'aaa', link: '/xxx' }
return {
label: ___EmotionJSX(ExternalLink, {
to: menu.link
}, menu.subTitle ? ___EmotionJSX("span", {
style: {
lineHeight: 1.2,
display: 'block',
paddingTop: 3
}
}, ___EmotionJSX("span", null, menu.title), menu.subTitle && ___EmotionJSX("span", {
style: {
opacity: 0.5,
display: 'block',
fontSize: '0.8em',
paddingTop: 3
},
className: "sub-title"
}, menu.subTitle)) : menu.title),
key: menu.link,
disabled: menu.disabled || menu.link === '#'
};
}
return null;
}
if (!currentSidebarEnhanceData) return undefined;
return currentSidebarEnhanceData.map(function (item) {
return processMenu(item);
});
}, [after, before, currentSidebarEnhanceData, linkTitleMap, search]);
var menuItems = useMemo(function () {
var _sidebarItems$reduce;
var sidebarItems = _toConsumableArray(sidebarData !== null && sidebarData !== void 0 ? sidebarData : []);
return (_sidebarItems$reduce = sidebarItems === null || sidebarItems === void 0 ? void 0 : sidebarItems.reduce(function (result, group) {
if (group !== null && group !== void 0 && group.title) {
// sideBar menu group 模式, 默认以非 group 模式渲染
var isSideBarGroupMode = sidebarGroupModePath === true ? true : (sidebarGroupModePath !== null && sidebarGroupModePath !== void 0 ? sidebarGroupModePath : []).filter(function (rule) {
return typeof rule === 'string' ? pathname.startsWith(rule) : rule.test(pathname);
}).length > 0;
if (isSideBarGroupMode) {
var _group$children;
result.push({
type: 'group',
label: group === null || group === void 0 ? void 0 : group.title,
key: group === null || group === void 0 ? void 0 : group.title,
children: (_group$children = group.children) === null || _group$children === void 0 ? void 0 : _group$children.map(function (item) {
return {
label: ___EmotionJSX(ExternalLink, {
to: "".concat(item.link).concat(search)
}, before, ___EmotionJSX("span", {
key: "english"
}, removeTitleCode(item === null || item === void 0 ? void 0 : item.title)), item.frontmatter && ___EmotionJSX("span", {
className: "chinese",
key: "chinese"
}, removeTitleCode(item.frontmatter.subtitle)), after),
key: item.link.replace(/(-cn$)/g, '')
};
})
});
} else {
var childrenGroup = group.children.reduce(function (childrenResult, child) {
var _child$frontmatter$ty, _child$frontmatter;
var nextChildrenResult = childrenResult;
var type = (_child$frontmatter$ty = child === null || child === void 0 || (_child$frontmatter = child.frontmatter) === null || _child$frontmatter === void 0 ? void 0 : _child$frontmatter.type) !== null && _child$frontmatter$ty !== void 0 ? _child$frontmatter$ty : 'default';
if (!nextChildrenResult[type]) {
nextChildrenResult[type] = [];
}
nextChildrenResult[type].push(child);
return nextChildrenResult;
}, {});
var childItems = [];
childItems.push.apply(childItems, _toConsumableArray(childrenGroup.default.map(function (item) {
return {
label: ___EmotionJSX(ExternalLink, {
to: "".concat(item.link).concat(search)
}, before, removeTitleCode(item === null || item === void 0 ? void 0 : item.title), after),
key: item.link.replace(/(-cn$)/g, '')
};
})));
Object.entries(childrenGroup).forEach(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
type = _ref2[0],
children = _ref2[1];
if (type !== 'default') {
childItems.push({
type: 'group',
label: type,
key: type,
children: children === null || children === void 0 ? void 0 : children.map(function (item) {
return {
label: ___EmotionJSX(ExternalLink, {
to: "".concat(item.link).concat(search)
}, before, removeTitleCode(item === null || item === void 0 ? void 0 : item.title), after),
key: item.link.replace(/(-cn$)/g, '')
};
})
});
}
});
result.push({
label: group === null || group === void 0 ? void 0 : group.title,
key: group === null || group === void 0 ? void 0 : group.title,
children: childItems
});
}
} else {
var list = group.children || [];
// 如果有 date 字段,我们就对其进行排序
if (list.every(function (info) {
var _info$frontmatter;
return info === null || info === void 0 || (_info$frontmatter = info.frontmatter) === null || _info$frontmatter === void 0 ? void 0 : _info$frontmatter.date;
})) {
list.sort(function (a, b) {
var _a$frontmatter, _b$frontmatter;
return (a === null || a === void 0 || (_a$frontmatter = a.frontmatter) === null || _a$frontmatter === void 0 ? void 0 : _a$frontmatter.date) > (b === null || b === void 0 || (_b$frontmatter = b.frontmatter) === null || _b$frontmatter === void 0 ? void 0 : _b$frontmatter.date) ? -1 : 1;
});
}
result.push.apply(result, _toConsumableArray(list.map(function (item) {
return {
label: ___EmotionJSX(ExternalLink, {
to: "".concat(item.link).concat(search)
}, before, removeTitleCode(item === null || item === void 0 ? void 0 : item.title), after),
key: item.link.replace(/(-cn$)/g, '')
};
})));
}
return result;
}, [])) !== null && _sidebarItems$reduce !== void 0 ? _sidebarItems$reduce : [];
}, [sidebarData, sidebarGroupModePath, pathname, search, before, after]);
return [sidebarEnhanceMenuItems || menuItems, pathname];
};
export default useMenu;