UNPKG

@alauda/doom

Version:

Doctor Doom making docs.

59 lines (58 loc) 2.21 kB
function removeIndex(link) { if (link.endsWith('/index')) { return link.slice(0, -5); } return link; } export const isSidebarDivider = (item) => { return 'dividerType' in item; }; export const isSidebarSectionHeader = (item) => { return 'sectionHeaderText' in item; }; export const isSingleFile = (item) => !('items' in item) && 'link' in item; /** * @zh_CN 如果 index 在 sidebar items 中, 则返回所有平级 item, 如果 index 在 dir 上, 则返回 items * @example */ export function findItemByRoutePath(items, routePath) { function isRoutePathMatch(item) { if (isSidebarDivider(item) || isSidebarSectionHeader(item)) { return false; } const removeIndexUrl = removeIndex(item.link || '/'); const removeBackSlashedRoutePath = routePath.replace(/\/$/, ''); return (removeIndexUrl === routePath || removeIndexUrl === removeBackSlashedRoutePath); } const matchRoutePathItemIndex = items.findIndex((item) => { return isRoutePathMatch(item); }); if (matchRoutePathItemIndex === -1) { return items .map((item) => { if (!('items' in item)) { return []; } return findItemByRoutePath(item.items, routePath); }) .flat(); } const matchRoutePathItem = items[matchRoutePathItemIndex]; const isArray = (i) => Array.isArray(i) && i.length >= 1; // 1. if isDir(item) return item.items if ('items' in matchRoutePathItem && isArray(matchRoutePathItem.items)) { // 2. if isDir(item) && item.items is all files, style is different if (matchRoutePathItem.items.every((item) => !('items' in item))) { return [matchRoutePathItem]; } return matchRoutePathItem.items.filter((item) => !isSidebarDivider(item)); } // 3. if matchRoutePathItem is a item, return other items in same level (/api/index.md is in the child sidebar) const result = [...items]; if (!('items' in matchRoutePathItem)) { result.splice(matchRoutePathItemIndex, 1); } const res = result.filter((item) => !isSidebarDivider(item)); return res; }